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

Username:   Password: 
 RegisterRegister   
 help with arrays
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
willyp798




PostPosted: Sat Mar 22, 2014 4:34 pm   Post subject: help with arrays

What is it you are trying to achieve?
<Replace all the <> with your answers/code and remove the <>>
Im trying to initialize the variables in the array and im not sure how to do it.

What is the problem you are having?
<Answer Here>
compile time expression expected

Describe what you have tried to solve this problem
<Answer Here> I've tried moving the array around but, i really dont know what to do.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


<Add your code here>
%variables
var userName1:string
var userName2:string
var userName3:string
var userName4:string
var userName5:string

%retreiving variables
get userName1
get userName2
get userName3
get userName4
get userName5

%array
var userNames:array 1..5 of string:=init(userName1,userName2,userName3,userName4,userName5)


Please specify what version of Turing you are using
<Answer Here>
version 4.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Mar 22, 2014 4:55 pm   Post subject: RE:help with arrays

You have all of thee variables named 'thing1', 'thing2', etc. The whole point of arrays is to avoid doing this. You've defeated the purpose of having an array.

Forget the init command. You almost never need it.

Arrays and loops go together like PB & J. If you want to fill an array of size 5 with user input, you need to get user input 5 times. How do you do the same thing multiple times? With a loop of course!

Array elements are accessed in the format array(N), where N is the index of the array. If you want to fill array (1) first, then array (2), etc, you need N to increase. What construct gives you a number that increases? A for loop! For loops include a variable that increases (or decreases, if you want). You can use that variable as your array index.

Here's an example.

code:

var foo : array 1..5 of int

for i : 1..5
    get foo (i)
end for


This does basically the same thing as your code, but it gives us extra power. What happens if we want to get 10 items instead of 5? With your code, we now need double the variables, double the gets and double the size of the init statement. Basically, you're doubling your code. With a for loop, all you need to do is change the number 5 to 10 (and this is made easier if you make the array size a variable, so you literally only need to change 1 number to get as much input as you want).
Raknarg




PostPosted: Sat Mar 22, 2014 11:02 pm   Post subject: RE:help with arrays

Just to be clear, you are not allowed to put variables into your init command, only hard coded values or constants.
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  [ 3 Posts ]
Jump to:   


Style:  
Search: