before printing/ outputting anything to the user we need to set words in the
algorithm to store what the user enters or the calculations we want done
To set words, we declare them:
Number: We declare the word “number” because we expect the user to type in
numbers. We could’ve used any word like photosynthesis even, but we used a
word relating to the task we want done.
Sum: We declare the word sum because we need to add all the numbers the
user will enter, add it together then store that result as “sum”.
Average: We declare the word “average” because all the numbers the user
enter will be added together then divided by the amount of numbers the user
entered.
Count: we declared count because we will keep a record of how many numbers
the user entered and store it as something.
Initialisation
Initialisation here is basically setting the words we declared to 0 or whatever
value we want the declared words/variable to be but in this case it will be 0 since
there is no number to count as yet or any numbers to sum/add as yet. But if the
algorithm was about Jasmaine and one of the declared words was “age” we could
put age = 14 since we know Jasmaine’s age is 14.
Whenever we want something to be outputted to the user for them to read or
any instructions to them to do we use the word print and the instructions/Words
in exclamation marks that we want the user to see. So:
Algorithm is:
print “Please enter positive integers one at a time and “1-” to indicate no further
input.”
What the user will see:
Please enter positive integers one at a time and “1-” to indicate no further input.
Exam question:
Algorithm:
Once the user see the print message to enter numbers they will then enter a
number, so read number tells the computer/algorithm to receive the user input
and store it as number so if the user entered the number 3, the algorithm will
change the value of number from 0 that we initialised it to, to 3.
This is what we call a loop, you can use while loop, for loop or IF loop.
The ! mean not so the above loops is saying while number does not equal -1 do
this
This is what the loop will do until number is equal to -1.
Sum= sum+number This is re-initialising the word sum, so in the beginning
sum was = 0 but once the user enters their first number then sum will = 0+
number the person entered.
Lets say the first number the user enters is 3 it will be sum = 0 + 3 so after this
instead of sum = 0 sum is now 3. Then when the user enters their second
number, lets say 2 then it will be sum = 3+2 so the value of sum will change to 5
and it continues until -1 is entered.
Count=count+1 This is re-initialising the word or value of count, so in the
beginning before any numbers were entered, the value of count started as 0 but
once a number is entered then count= 0 + 1. The value of count will then
change to 1 and once a second number is entered it will be count= 1 (count)+ 1
then the value of count will change to 2 and it continues until -1 is entered.
Read number This is supposed to read the next number the user will enter to
change the value of number from the first number, and this will continue until -1
is entered.
NOW I DONT LIKE HOW THE ALGORITHM WAS WRITTEN
So on the next page is how i would write the algorithm instead but
before going to that I want you to write your own algorithm and
compare it to what I have.
Begin
Declare number, sum, average, count
Count= 0
Sum= 0
Print “Please enter positive integers one at a time and “1-” to indicate no further
input.”
Read number
While (number != -1)
Sum=sum+number
Count=count+1
Print “Please enter a next number”
Read number
End while
Average = sum/count
Print “Your total is:”, sum
Print “Your average is”, average
END