Posted: 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
Insectoid
Posted: 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
Posted: 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
Posted: 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
numElements is just a number. This is totally valid.
Insectoid
Posted: 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
Posted: 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
Posted: 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.