Computer Science Canada

really simple array problem

Author:  shoobyman [ Sat Feb 11, 2006 7:33 pm ]
Post subject:  really simple array problem

i am teh noob of teh forums, so can u guys help me?
i am trying to learn arrays, but no matter how much i try to call up the variable, it will not display it.
i can make it appear, but only if i use the "init" command.
this is my code, and each time i run it, it says that variable has no value.

code:
var number: array 1..10 of int
put number (2)

Author:  Cervantes [ Sat Feb 11, 2006 7:47 pm ]
Post subject: 

If it says the variable has no value, it probably has a pretty good reason for saying so.

number (2) does not mean the integer, 2. It refers to the second element of an array of variables.

In this case, you have not assigned the second element (or any elements, for that matter) a value. To do so, you'd do this:
code:

number (2) := 10473

Or perhaps we could do this:
code:

for i : 1 .. 10
    number (i) := i ** 2
end for

Author:  shoobyman [ Sat Feb 11, 2006 7:53 pm ]
Post subject: 

oooooooooooo, it doesn't mean integer 2, that makes more sense i just gotta declare what number (2) is, thanx! lol me is noob Laughing

Author:  MysticVegeta [ Sat Feb 11, 2006 11:38 pm ]
Post subject: 

or..
code:
var nums : array 1..5 of int := init (1, 2, 3, 4, 5)
put nums(2)


Now, you wont get an error because the corresponding element at the index of 2 is 2.

Author:  Martin [ Sun Feb 12, 2006 12:04 pm ]
Post subject:  Re: really simple array problem

shoobyman wrote:
i am teh noob of teh forums, so can u guys help me?
i am trying to learn arrays, but no matter how much i try to call up the variable, it will not display it.
i can make it appear, but only if i use the "init" command.
this is my code, and each time i run it, it says that variable has no value.

code:
var number: array 1..10 of int
put number (2)


What your code does is creates a set of 10 integer variables.

Your code is the equivalent of:
code:
var number1: int
var number2: int
var number3: int
...
var number10: int
put number2

Author:  shoobyman [ Sun Feb 12, 2006 6:01 pm ]
Post subject: 

thank you all, very helpful, i now completely understand arrays, (it wasn't nearly as hard as i thought) Very Happy Laughing .


: