Computer Science Canada help with arrays |
Author: | willyp798 [ 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>
Please specify what version of Turing you are using <Answer Here> version 4.1 |
Author: | Insectoid [ 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.
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). |
Author: | Raknarg [ 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. |