Help with arrays in Turing!! Grade 10 programming.
Author |
Message |
Smarties102
|
Posted: Wed May 18, 2011 6:34 am Post subject: Help with arrays in Turing!! Grade 10 programming. |
|
|
What is it you are trying to achieve?
Basically this is the outline of the program..
Write a program which allows the user to create, store, and retrieve values in an array. The program will first ask the user for the length of the array (make sure you check if the length is an integer). Then, you will display a menu for the user to make modifications to the array. The commands are ?set?, ?get?, ?count?, and ?replace?:
Menu
1. Set value
2. Get value
3. Count frequency of value
4. Replace value
Case 1: Set value. Ask the user for a value (any value), and an index (make sure it is an integer) to store that value into. Reprint the array using braces and commas, for example: {1, 2, 3, 4} Replacing one thing at a time.
Case 2: Get value. Ask the user for an index (make sure it is an integer), and output the value in the array at that index.
Case 3: Count frequency of value. Ask the user for a value, and output how many times that value appears in the array.
Case 4: Replace value. Ask the user for a value to replace, and a value to replace it. Your program will replace any and all instances in the array with the new value.Reprint the array using braces and commas, for example: {1, 2, 3, 4} Replacing all.
What is the problem you are having?
I don't understand how to do the cases.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
[syntax="turing"]
% Table.t
% Asks the user for the length of the array.It displays a menu for the user to make modifications to the array.
% The commands are 'set', 'get', 'count', and 'replace'.
% This is what I have so far....
% State the variables.
var choice, value, nIndex, frequency, value2, lenngth, arrayValue : int
var input: string
var valueO : int := 0
% Get information from user.
put "Hello! Welcome to the table program."
put "============================================================"
put "Please input the length of the array:"..
loop
get input
exit when strintok (input)
put "Not a number. Please enter an integer: " ..
end loop
lenngth := strint (input)
delay (200)
cls
% Declare an array, sentence, of size "count", to store a list of strings
var intArray : array 1 .. lenngth of int
% Get numbers from user and store in array
put "Enter ", lenngth, " numbers:" ..
put " "
for i : 1 .. lenngth
get intArray (i)
end for
put "--------------------------------------"
%Display menu.
put "Menu"
put "1.Set value"
put "2.Get value"
put "3.Count frequency of value"
put "4.Replace value"
% Ask for choice
put "Please choice 1,2,3 or 4"
get choice
delay (200)
cls
% Determine the outcome once user chooses a choice.
case choice of
label 1 : %Option number 1.
put "Please enter a value:"
get value
put "Please enter an index:"
get nIndex
label 2 : %Option number 2.
put "Please enter an index:"
get nIndex
label 3 : %Option number 3.
put "Please enter a value:"
get valueO
valueO := valueO + 1 % Count the number of times it appears.
put "This value appears", valueO, "times."
label 4 : % Option number 4.
put "Please enter a value to be replaced:"
get value
put "Please enter a value to replace it:"
get value2
end case
Please specify what version of Turing you are using
Turing 4.1.1
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
mirhagk
|
Posted: Wed May 18, 2011 4:48 pm Post subject: RE:Help with arrays in Turing!! Grade 10 programming. |
|
|
Just a question, by any value, would it mean just integers or letters too?
And basically just work each of those 4 examples out, you have the structure done.
How would you retrieve the value of a piece of the array?
How would you set a value of a piece of the array?
How would you count how many there are of a value in the array? (think about how you would do this in real life, it's the same way)
How would you replace a value in the array? (Again think about what steps there are to this) |
|
|
|
|
![](images/spacer.gif) |
Smarties102
|
Posted: Thu May 19, 2011 7:23 am Post subject: Re: Help with arrays in Turing!! Grade 10 programming. |
|
|
Thank you! I got it after thinking about it, but I would like to know if its better to use an if statement or case? |
|
|
|
|
![](images/spacer.gif) |
HRI
|
Posted: Fri May 20, 2011 8:07 am Post subject: RE:Help with arrays in Turing!! Grade 10 programming. |
|
|
If statements should be used for quick comparisons, but if there's quite a few specific things the condition could be then use a case. Basically if you look in your code and see:
Turing: |
if ...
then
...
elsif ...
then
...
elsif ...
then
...
elsif ...
then
...
else
...
end if
|
A case would sum that up quite nicely.
Turing: |
case <condition> of
label <first possible value>:
<first if stuff>
label <second possible value>
<first elsif stuff>
label :
<else stuff>
end case
|
|
|
|
|
|
![](images/spacer.gif) |
|
|