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

Username:   Password: 
 RegisterRegister   
 Starting value. to an ending value.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MossyMossy




PostPosted: Tue Nov 10, 2009 9:37 am   Post subject: Starting value. to an ending value.

I need to create a program that prompts the user for 2 values a start value and a end value. The program must print all the numbers from the start value to the end, with it increasing by 2's The program must employ a loop structure of some sort.

Turing:


var number : int
var endnumber : int

put "Please enter a value"
get number
put "Please enter an ending value"   
get endnumber

loop

put number + 2

    exit when number <= endnumber

   
end loop





im stuck, please help.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Nov 10, 2009 10:27 am   Post subject: RE:Starting value. to an ending value.

Well, you've got all the basic elements. You have a variable to take each of the two user-supplied values. You have a loop with an output and an exit. All you need now is a little bit of correction on what you do have.

First, (number+2) is an expression that adds 2 to the value of number and returns that - it doesn't increase the value in number by 2. To do that, you want:

Turing:

number := number + 2  % "Number now equals its old value plus 2"

% OR...

number += 2  % "Number increases by 2"



This means that the output statement should probably just output the current value of number. You can figure this one out yourself, it's pretty basic.

Lastly, you need to fix your exit condition. You want to exit when your counter (which you called number) is greater than or equal to the end number (helpfully called endnumber). Currently you have "less than or equal to", which won't work.


Note: The above all assumes that the start number is less than end number. Otherwise, this misbehaves pretty badly.
belarussian




PostPosted: Tue Nov 17, 2009 9:41 am   Post subject: Re: Starting value. to an ending value.

YOU COULD JUST DO THIS


for number 0..100 by 2
put number
end for
put "That was easy"

THERE ALL DONE
B-Man 31




PostPosted: Tue Nov 17, 2009 9:53 am   Post subject: Re: Starting value. to an ending value.

belarussian @ Tue Nov 17, 2009 9:41 am wrote:
YOU COULD JUST DO THIS


for number 0..100 by 2
put number
end for
put "That was easy"

THERE ALL DONE


except thats not what he wants, he want the users to select their own value, for loops are not user controlled therefore that is not what he wants. like DemonWasp said, hes got the basics but he needs to go just a little further.
Tony




PostPosted: Tue Nov 17, 2009 12:10 pm   Post subject: Re: Starting value. to an ending value.

B-Man 31 @ Tue Nov 17, 2009 9:53 am wrote:
for loops are not user controlled

Could you elaborate on such a claim?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
belarussian




PostPosted: Wed Nov 18, 2009 7:28 pm   Post subject: Re: Starting value. to an ending value.

ok Fine then do this


var usernumber : int

get usernumber

cls
for number : 0 .. usernumber by 2
put number
end for
belarussian




PostPosted: Wed Nov 18, 2009 7:43 pm   Post subject: Re: Starting value. to an ending value.

or if that dont work then

you could do the simplest thing ever

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


var startvalue, endvalue : int


put "please enter the start value:" ..
get startvalue
put "please enter the end value:" ..
get endvalue
loop
put startvalue
startvalue := startvalue + 2
exit when startvalue >= endvalue
end loop
B-Man 31




PostPosted: Wed Nov 18, 2009 7:43 pm   Post subject: Re: Starting value. to an ending value.

Tony @ Tue Nov 17, 2009 12:10 pm wrote:
B-Man 31 @ Tue Nov 17, 2009 9:53 am wrote:
for loops are not user controlled

Could you elaborate on such a claim?


ok, what i said is generally true, unfortunately i was wrong in this case as i figured out but let me still explain to you what i mean.

When a person decides to use a for loop the programmer decides exactly when to exit, after x amount of repetitions, so it makes no sense to use it when you want it to be user controlled, for instance, the user wants to click something as many times as they want, and it will only exit when finished. you need a user controlled loop (AKA regular loop) rather than a programmer controlled loop (for loop). I hope that clears thing up Very Happy
Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Wed Nov 18, 2009 7:58 pm   Post subject: Re: Starting value. to an ending value.

B-Man 31 wrote:
When a person decides to use a for loop the programmer decides exactly when to exit, after x amount of repetitions, so it makes no sense to use it when you want it to be user controlled

code:

var x : int
put "Enter a value greater than 1..."
get x

for i : 1 .. x
put "User defined for loops are AWESOME!"
end for


So long as either you or the user knows how many times it will run through (for example counting to a certain user inputted value), for loops will work just as well as a standard loop, and will even automate the exit condition. The programmer might decide when it will exit, but they don't have to use a constant value.
B-Man 31




PostPosted: Wed Nov 18, 2009 9:59 pm   Post subject: Re: Starting value. to an ending value.

yeah i definitely understand that but i as just saying, in general it is better to have loops if it is user defined and for loops if it is not.
Tony




PostPosted: Thu Nov 19, 2009 1:34 am   Post subject: Re: Starting value. to an ending value.

B-Man 31 @ Wed Nov 18, 2009 7:43 pm wrote:
the user wants to click something as many times as they want, and it will only exit when finished. you need a user controlled loop (AKA regular loop)

code:

for ( ;; ){
   if (user_done_clicking)
      break;
}

Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
belarussian




PostPosted: Thu Nov 19, 2009 10:10 am   Post subject: RE:Starting value. to an ending value.

ya but you could also do a for loop and it will be user controled

May not be 100% correct i am doing this in a sec
%%%%%%

for endclicks : 0..amountofclicks
WILL FINISH LATER
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  [ 12 Posts ]
Jump to:   


Style:  
Search: