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

Username:   Password: 
 RegisterRegister   
 Arrays Question
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Luffy123




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: 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:
Turing:
for i : 0 .. 35
    price (1988 + i div 12, 1 + i rem 12) := 50 - i
end for

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:
Turing:

var myPrimes : array 1..* of int := init (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37)

I didn't count the number of primes I entered, but Turing can count for me. Smile (It should create an array 1..12)

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:
Turing:
fcn largestElement (integers : array  1 .. * of int) : int
    var maxSoFar: int := 0
    for i : 1.. upper (integers)
        maxSoFar := max (integers (i), maxSoFar)
    end for
    result maxSoFar
end largestElement


This function can be used for an array of integers of any size.

Hope this helps.
Untouchable221




PostPosted: 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?
TokenHerbz




PostPosted: Fri Dec 18, 2015 6:10 am   Post subject: RE:Arrays Question

code:

%%lower is set compile time, change that 1 to a zero or a 3 and run it.
var a : array 1 .. 5 of int
%%upper can be set the same way, but it can be flexible.
put lower(a)
put upper(a)
put "*"

%%here the lower value is actually 1, again, (its where the array starts)
%% and when you run this, zero is the upper (confuzing?!)
var b : flexible array 1 .. 0 of int

put lower(b)
put upper(b)

put "*"
%%setting a new upper limit, say we spawn some bullets in! pewpew
new b, upper(b) + 2 %%take the ZERO bullets we have, and add 2 :)

put lower(b)
put upper(b)

%%turing is really bad with handling arrays imo, but there are work arounds
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: