Questions
Author |
Message |
yorusmile
|
Posted: Tue Dec 21, 2010 3:58 pm Post subject: Questions |
|
|
%Write a program that announces at each repetition of a loop the number of times it has executed the loop. Have it stop at each execution with the message "Type 'more' to continue."
loop
var reply: string
var number: int:= 0
put "Loop execution number ", number+1
put "Type 'more' to continue."
get reply
exit when reply="stop"
end loop
%I don't know what to do.. resulting in '1'.
%I should add 1 to somewhere, but I don't know where to put
%Write a program that reads a positive integer and then finds the smallest power of two greater than or equal to the number that was read.
loop
var number: int
put "Enter a positive integer: "..
get number
if number<=0 then
put "Invalid Input"
else
put ""
end if
exit when number>0
end loop
%I don't understand the part:
%"the smallest power of two greater than or equal to the number that was read"
%Can anyone explain it?
%as well as how to solve for it.
Pls help me! T^T
get struck on it for several weeks = = |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
TerranceN
|
Posted: Tue Dec 21, 2010 4:14 pm Post subject: Re: Questions |
|
|
For the first problem:
If you declare a variable inside a loop, every time it reaches the end of the loop that variable goes "out of scope" meaning the program deletes the variable. Next iteration of the loop, the variable is declared again, back to its default starting value. In order to keep the value of a variable through multiple iterations of a loop, it must be declared outside the loop. Then inside the loop you can use number := number + 1, or the shorthand form, number += 1, to increase the value number has by 1. Also, you stop the program when the user enters stop, but it should be when the user does not enter "more". In other words, exit when reply ~= "more", ~= means not equal in Turing, in case you did not know.
For the second program:
Try solving this on paper first. Lets say the number is 5. Lets try the first power of two, 2^0. 2^0 = 1, is 1 greater than or equal to 5? No, so lets try the next power of two, 2^1. 2^1 = 2, is 2 greater than or equal to 5? No, so you would keep repeating this process until you found a power of two that is greater than the number the user inputted. |
|
|
|
|
![](images/spacer.gif) |
yorusmile
|
Posted: Tue Dec 21, 2010 6:11 pm Post subject: RE:Questions |
|
|
Thanks a lot ;D
but still i'm not sure of the second program
is it like that, i guess?
var number, answer: int
var counter: int:=0
put "Enter a positive integer: "..
get number
if number<=0 then
put "Invalid Input"
else
loop
counter:=counter+1
answer:= 2*counter
put answer
exit when answer>=number
end loop
end if |
|
|
|
|
![](images/spacer.gif) |
TerranceN
|
Posted: Tue Dec 21, 2010 6:31 pm Post subject: RE:Questions |
|
|
Two things:
First, 2 * counter is multiplication, what you want is 2 to the power of counter. In Turing you can use the ** operator, so it would be 2 ** counter.
Second, you want it to output the power that was greater than or equal to number, so only output counter, and only at the end.
Other than that it seems good. |
|
|
|
|
![](images/spacer.gif) |
yorusmile
|
Posted: Wed Dec 22, 2010 3:16 pm Post subject: RE:Questions |
|
|
thanks.
I have one final question. Would you like to give me some suggestions.
%Write a program that will ask the user for an integer and then create a triangle of shape. Ask the user for the character or symbol they would like to make a triangle out of.
var numberOfShape: int
var shape: char
put "Enter the maximun number of stars: "..
get numberOfShape
put "Enter the character: "..
get shape
const row :string:= shape
for line : 1 .. numberOfShape
put row (1 .. line)
end for
%What should i do for this program?
%There is a message at the bottom part:
%"Right bound of substring is greater than length of string" |
|
|
|
|
![](images/spacer.gif) |
|
|