Help with loops and outputting different variables
Author |
Message |
pleb
|
Posted: Tue May 26, 2020 10:10 pm Post subject: Help with loops and outputting different variables |
|
|
I'm having trouble figuring out how to use loops to print variables. I'm working on an assignment which asks me to get user input for five full names than output them in reverse order. I have to use put, get, loop, and variables. I understand how to incorporate all of them except for loops. Any ideas on where or how I can use a loop? I'm using Turing 4.1.1. Heres what I've come up with so far.
var name1: string
put "Enter the name of the First student"
get name1 : *
var name2: string
put "Enter the name of the Second student"
get name2 : *
var name3: string
put "Enter the name of the Third student"
get name3 : *
var name4: string
put "Enter the name of the Fourth student"
get name4 : *
var name5: string
put "Enter the name of the Fifth student"
get name5 : * |
|
|
|
|
|
Sponsor Sponsor
|
|
|
scholarlytutor
|
Posted: Thu May 28, 2020 11:09 pm Post subject: Re: Help with loops and outputting different variables |
|
|
Did you do arrays yet in your class? I ask because having to create five separate variables for names is the main problem here. An array can hold all five names in them.
var names : array 1 .. 5 of string
This array has five spaces in memory to hold names. If you wanted the user to input in memory slot 1, then you type
get names (1)
and the name will be held in memory slot one of your names array.
As this is homework, I don't want to give away the whole answer. I'll just say how a loop works and then let you figure out the rest.
loop
put "hi"
end loop
This is an infinite loop. it is worth typing into Turing just to see what it does. The problem is that it never ends. It needs a way to count how many times it has looped, and it needs an exit statement. So you need to make a counter.
var counter : int := 0
loop
exit when counter = 5
counter += 1
put counter
end loop
I recommend typing this into Turing as well to see what it does.
I hope all this helps! Feel free to post an update. |
|
|
|
|
|
|
|