Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
The Question:

Write an algorithm that will find the sum of ten numbers entered by a user and output the result. Use pseudo-code. Include comments to explain how the code works. (6 marks).

The Asnwer:

SET total TO 0 #The variable 'total is used to store the sum of numbers.
FOR goes FROM 1 TO 10 DO #A loop is set up to run from 1 to 10 as there are 10 numbers.
  SEND 'Please enter a number.' TO KEYBOARD #The user is asked to enter a number.
  RECEIVE number FROM (INTEGER) KEYBOARD
  SET total TO total + number #The total is calculated.
END FOR #This closes the 'for' loop.
SEND total TO PRINTER #The sum of the numbers is output.


What I have tried:

I have re-read it, but I don't understand how this code adds up all the numbers, the FROM 1 TO 0 DO part, the total part and how it all works. Can someone please explain how this works and why's what/that's there. Thank you.
Posted
Updated 14-Feb-18 0:46am
v2

The FOR loop executes its body then times, you know 1 TO 10 is a way to say 1,2,3,4,5,6,7,8,9,10.
In the loop body the use is asked for a number and the entered number is add up to the total. Again, you know SET total TO total + number is a way to say: "take the current value of the total variable, add to it the current value of the number variable and eventually assign the result (the sum) to total variable" (and computers do actually understand that!).
In other words: evaluate the expression (total + number) and then assign the result to total.
 
Share this answer
 
Comments
tgspython 14-Feb-18 7:08am    
Thank you so much!
CPallini 14-Feb-18 7:58am    
You are welcome.
You wrote this: so you should understand it. That is part of your homework, after all.

But it's pretty obvious: the FOR means "do this stuff 10 times". The stuff it does ten times is read a number from the user, and add it into a total.

To be honest, if you wrote this and don't understand it then there is something very wrong with you.
If you didn't write it, then you are going to fail your course unless you turn your ideas around: finding stuff to hand in may say you time and effort now, but it doesn't mean you can do anything at all when the final exam comes round and you can't get access to the internet to copy from...
 
Share this answer
 
Comments
tgspython 14-Feb-18 6:02am    
Thank you for your help. For your information, no this is not my homework and no I did not write it myself. This is taken from a revision guide where it tells you the question and the answer. We have just started learning this therefore I am new to this and was not sure how the code got there. And I am very well aware you can't use the internet or cheat in any exams. Instead of moving on, I put the effort in and came here for help so I understand it. I appreciate and am very thankful you answered my question as now it makes sense!
tgspython 14-Feb-18 6:03am    
(rating 5 stars)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900