Computer Science Canada Arrays Question |
Author: | Luffy123 [ Sun May 27, 2012 3:50 pm ] |
Post subject: | Arrays Question |
I'm reading the doc on arrays and they show this example. var price : array 1988 .. 1990, 1 .. 12 of int var sum : int := 0 for year : 1988 .. 1990 % For each year for month : 1 .. 12 % For each month sum := sum + price (year, month) end for end for but when I put this in turing.. it doesn't work.. says variable has no value and is referring to price(year,month) can someone explain why and how to fix it? and also what does array 1 .. * of int mean? what does the * do? |
Author: | Dreadnought [ Sun May 27, 2012 4:26 pm ] | ||||||
Post subject: | Re: Arrays Question | ||||||
Notice that the example in the Turing documentation has '...' in one line meaning that they are excluding some code. This code would set the values of the array. Without such code your asking the program to take the sum of a blank array. You could replace the ... by this for example:
This will set the value of the price for the first month to 50 and reduce it by 1 each month. The * is used when you create an array and give it values immediately but may not know how many values you will give it. The * means that Turing will look at the values and determine the appropriate size for the array. Note that you can only do this for the upper bound of the array. For example:
I didn't count the number of primes I entered, but Turing can count for me. ![]() The * is much more useful for procedures and functions that will take an array as argument, but not know the size of the array beforehand. For example:
This function can be used for an array of integers of any size. Hope this helps. |
Author: | Untouchable221 [ Thu Dec 17, 2015 11:09 pm ] |
Post subject: | RE:Arrays Question |
I need help for a school assignment. Pls I need it answered as soon as possible. How do set a lower and upper bounds for arrays? |
Author: | TokenHerbz [ Fri Dec 18, 2015 6:10 am ] | ||
Post subject: | RE:Arrays Question | ||
|