Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 array!! plz help!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bgmrk




PostPosted: Mon Dec 12, 2005 10:12 pm   Post subject: array!! plz help!

I must hav read over that darn tutorial like a hundred times but i just can'y seem to get it. could some1 plz make like an idiots guide to arrays and flexible arrays. PLZ it would be a great help.
Sponsor
Sponsor
Sponsor
sponsor
Geminias




PostPosted: Mon Dec 12, 2005 10:23 pm   Post subject: (No subject)

okay an array is just another way of saying a group of variables of the same type.

an array is no different from a variable.

you know a variable can be called "apple"
well, if you had 10 apples, and you had to differentiate between them, instead of writing out the code:

var apple1, apple2, apple3, apple4 .. apple10 : int

you could just say:

var apples : array 1 .. 10 of int

now when you say: apples (1) you are referring to the variable apples (1) and it can be changed like any other variable of type int. The great thing about arrays is you can use for loops to edit them.

for instance.

code:

var apples : array 1 .. 10 of int

for i : 1 .. 10
   apples (i) :=  i
end for

for i : 1 .. 10
  put apples (i)
end for

OUTPUT:

1
2
3
4
5
etc


if this made sense to you reply back and i'll talk about flexible arrays.[/code]
bgmrk




PostPosted: Tue Dec 13, 2005 5:06 pm   Post subject: (No subject)

ok i got it

i've been practising a bit.
seems pretty easy.

i think i ready for flexible array
GhostCobra




PostPosted: Sat Dec 17, 2005 12:42 am   Post subject: (No subject)

code:

import GUI

var w1 : int := Window.Open ("graphics:400;120,nobuttonbar")

colourback (gray)
cls

var maxItems : int := 1
var addItemBox, addItemTextBox, showItemTextBox, itemBox, choseItemBox, quitButton : int

var font1 : int := Font.New ("Arial:10:Bold")
var font2 : int := Font.New ("Arial:8")

var textBox : int
var items : flexible array 1 .. maxItems of string
var ch : string := ""

for i : 1 .. maxItems
    items (i) := ""
end for

proc addItem (text : string)
    var skipit : boolean := false
    if text = "" then
        skipit := true
    else
        for w : 1 .. maxItems
            if text = items (maxItems) then
                skipit := true
            else
            end if
        end for
        if skipit = false then
            var file : int
            open : file, "saveditems.txt", put, mod, seek
            seek : file, *
            put : file, text
            close : file
            GUI.AddLine (textBox, text)
            new items, maxItems + 1
            maxItems := maxItems + 1
            items (maxItems) := text
            GUI.SetText (addItemTextBox, "")
        else
        end if
    end if
end addItem

proc openItems
    var file : int
    var next : int := 1
    if File.Exists ("saveditems.txt") then
        open : file, "saveditems.txt", get
        loop
            exit when eof (file)
            get : file, items (maxItems) : *
            GUI.AddLine (textBox, items (maxItems))
            new items, maxItems + 1
            maxItems := maxItems + 1
        end loop
        close : file
    else
        maxItems := 0
    end if
end openItems

proc ChoseLine (line : int)
    var font1 : int := Font.New ("Arial:10")
    var text : string
    if line not= 0 then
        text := "You chose " + items (line) + "."
        GUI.SetText (showItemTextBox, text)
    else
    end if
end ChoseLine

proc null (text : string)
end null

addItemTextBox := GUI.CreateTextField (195, 90, 180, "", addItem)
showItemTextBox := GUI.CreateTextField (195, 40, 180, "", null)

Font.Draw ("Item:", 160, 94, font1, black)
Font.Draw ("Chose:", 148, 44, font1, black)

textBox := GUI.CreateTextBoxChoice (10, 10, 120, maxy - 25, 0, 0, ChoseLine)
itemBox := GUI.CreateLabelledFrame (2, 2, 137, maxy - 5, GUI.LINE, "Items")

choseItemBox := GUI.CreateLabelledFrame (maxx - 2, 70, 137, 27, GUI.LINE, "Chosen Suitcase Item")
addItemBox := GUI.CreateLabelledFrame (maxx - 2, maxy - 5, 137, 80, GUI.LINE, "Add Items to the Suitcase")

quitButton := GUI.CreateButton (maxx - 57, 1, 40, "Quit", GUI.Quit)

GUI.Disable (showItemTextBox)

openItems

GUI.SetActive (addItemTextBox)

loop
    exit when GUI.ProcessEvent
end loop

Window.Close (w1)


Some initialized flexible arrays.

(btw didn't realize how hard it would be to understand, just pay attention to the flexible array setup and how I execute new for it.) It's a school program with GUI, so dont be shocked when you run it and go wth Shocked
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: