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

Username:   Password: 
 RegisterRegister   
 adding numbers in a for statement
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Velocity




PostPosted: Wed Nov 16, 2011 4:52 pm   Post subject: adding numbers in a for statement

What is it you are trying to achieve?

I want to add the numbers of the provided values in my for statement.

What is the problem you are having?

cant find out how to for example say
var firstnumber, secondnumber : int
put "whats your first number?" ..
get firstnumber
%for example the first number is 5
put "whats your second number?" ..
get secondnumber
%for example the second number will be 15
% I want to add all the numbers in between 5 and 15 together and the output will be the value of all those given numbers.
for k : firstnumber .. secondnumber
put k
end for
%how do i do that in the for statement?

Describe what you have tried to solve this problem

If i dont know how to do it, then how can i try? I mean the only possible thing i can think off is adding a "+" somewhere. I have no clue where.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

N/A

Turing:


var startingValue, stoppingValue : int
var output : real

put "Please enter a starting value: " ..
get startingValue
put "Please enter a stopping value: " ..
get stoppingValue

for k : startingValue .. stoppingValue
put k
end for



Please specify what version of Turing you are using
4.1.1a
Sponsor
Sponsor
Sponsor
sponsor
Aange10




PostPosted: Wed Nov 16, 2011 6:02 pm   Post subject: RE:adding numbers in a for statement

Velocity wrote:

If i dont know how to do it, then how can i try? I mean the only possible thing i can think off is adding a "+" somewhere. I have no clue where.


The main thing people get help on is tools. For instance, if you and I were wood workers, I wood teach you how to use a screw driver, and a screw. I would not build you a birdhouse, I would not make a shed for your backyard, and I would not build you a computer desk. I would teach you how.


My reason for stating this is you have to learn to try things your self. If you dont know how to do it, sit down and think about it. Write it on a paper. Write out the Algorithm. Think about what you're telling the computer. What do you want to happen?


Myself and the others here at Compsci.ca will assist you when you need help. However, you should try to figure it out by yourself; our ultimate goal is to teach you how to do it. (Albiet us enjoying the process asw ell - at least I do.)

Quote:

% I want to add all the numbers in between 5 and 15 together and the output will be the value of all those given numbers.


Lets break it down!:

I want to add

Turing:

+


all the numbers in between 5 and 15 together

Turing:

5 .. 15 % Hmm, isn't this something we've seen before?


and the output will be the value of all those given numbers

Turing:

put value



Now, notice how thinking can get us where we want to be?

Now this is the part where I stop helping you build a birdhouse, and I watch you use the tools you have.

Turing Walkthrough if you need help with any specific context.


HINT ** for i : 10 .. 11 will start at 10 and end at 11.
Dreadnought




PostPosted: Wed Nov 16, 2011 6:02 pm   Post subject: Re: adding numbers in a for statement

Well, you have a variable for output, that's a start, although it doesn't need to be a real number. You're on the right track, the for loop will go through all the numbers that you want to sum.

Now you need to, as you speculated, add the values one by one. You can do this using your output variable, which you aren't using right now.

Try reading the variable section in http://compsci.ca/v3/viewtopic.php?t=9634 and see if it helps.
Aange10




PostPosted: Wed Nov 16, 2011 6:03 pm   Post subject: RE:adding numbers in a for statement

carpenters not wood workers**
Velocity




PostPosted: Wed Nov 16, 2011 7:12 pm   Post subject: RE:adding numbers in a for statement

@ Aange oh really? lol so what your saying is

put value + firstnumber .. secondnumber?
thats how you add every single number in a for statement?
Velocity




PostPosted: Wed Nov 16, 2011 7:33 pm   Post subject: Re: adding numbers in a for statement

omg you people are not getting what im saying... ill just give you the question that i am being asked:

Write a program that asks the user to input a starting value and a stopping value and then counts by ones from the starting value to the stopping value (output the counting to the screen) and then prints the sum of the whole numbers from the starting value to the stopping value. HINT : the user could enter a start value higher than the stopping value.

For example for the input Starting value: 1
Stopping value: 4

Output: 1
2
3
4
Total: 10 ( 1 + 2 + 3 +4)

Make more sense of whati am trying to do here now?
Velocity




PostPosted: Wed Nov 16, 2011 9:47 pm   Post subject: RE:adding numbers in a for statement

Still looking for assistance :O im so dull (i have a feeling the answer is right there, i just dont see it)
[Gandalf]




PostPosted: Wed Nov 16, 2011 10:02 pm   Post subject: RE:adding numbers in a for statement

Don't triple post, it won't get you help any faster...
code:
var num : int := 0
var one : int := 1
var two : int := 2
put num
num += one
put num
num += two
put num

Does this help?
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: