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

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




PostPosted: Sat Mar 01, 2014 8:10 pm   Post subject: need help with array program

What is it you are trying to achieve?
write a program that asks the user for the numbers of values they want(between 5-20), and then print the values out, including the average of the values and the largest and smallest values. the result would look like this

how many numbers would you like
5
Enter value #1 of 5
10
Enter value #2 of 5
20
Enter value #3 of 5
30
Enter value #4 of 5
40
Enter value #5 of 5
50

the mean is 30

the smallest value is 10 and the largest value is 50


What is the problem you are having?
I dont know how to declare a array that can change depends on the user and i dont know how to print them out.


Describe what you have tried to solve this problem
I made a variable called ValueNo and did array 1 .. ValueNo of int and that doesnt seem to work and now im stuck


Please specify what version of Turing you are using
4.05
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Mar 01, 2014 8:14 pm   Post subject: RE:need help with array program

Does the size of the array have to change?
bigpotato




PostPosted: Sat Mar 01, 2014 8:17 pm   Post subject: Re: need help with array program

it depends on the user but it doesnt change after the user has selected the size
Raknarg




PostPosted: Sat Mar 01, 2014 8:42 pm   Post subject: RE:need help with array program

Not a very nice way to do it, but you could do this:

Turing:

var numElements : int

put "How many elements would you like?"
get numElements

var arr : array 1 .. numElements of int

put "Your array has ", upper(arr), " elements."


numElements is just a number. This is totally valid.
Insectoid




PostPosted: Sat Mar 01, 2014 9:08 pm   Post subject: RE:need help with array program

You know there's never more than 20 elements, so you can just use an array of size 20 every time. You don't have to use every index. If there's only 5 elements, then you only use the first 5 indices of the array.
bigpotato




PostPosted: Sat Mar 01, 2014 9:25 pm   Post subject: Re: need help with array program

thank you, one more question, how can i make the program say "invalid input" if the number they enter is less than 5 and greater than 20, and make it keep saying it until they enter a valid input
Insectoid




PostPosted: Sat Mar 01, 2014 9:33 pm   Post subject: RE:need help with array program

Quote:
if the number they enter is less than 5 and greater than 20


Convert that sentence directly to code

Quote:
keep saying it until they enter a valid input


Do something multiple times. You'll want a loop for that.
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  [ 7 Posts ]
Jump to:   


Style:  
Search: