2  Practical Problem Solving & Programming / Counting & Totaling


Counting & Totaling
1,2,3,4  Simple right... we all know how to count.
2 + 2 =??? = 4 Well done!
Hopefully we are all already familiar with the idea of counting and totaling in real life, but how do we achieve this in coding. These are very important concepts to understand as they are key to most programming solutions.
2 + 2 =??? = 4 Well done!
Hopefully we are all already familiar with the idea of counting and totaling in real life, but how do we achieve this in coding. These are very important concepts to understand as they are key to most programming solutions.
Counting
Being able to keep count of things in coding is very important. You may wish to keep count of data, items or even the number or repetitions that can or will take place in a loop.
The idea being basic counting is that you start with a number e.g 0 and then you continue to add 1 to it over and over this will produce a sequential count e.g.
0 + 1 = 1
1 + 1 = 2
2 + 1 = 3
3 + 1 = 4
4 + 1 = 5
The idea being basic counting is that you start with a number e.g 0 and then you continue to add 1 to it over and over this will produce a sequential count e.g.
0 + 1 = 1
1 + 1 = 2
2 + 1 = 3
3 + 1 = 4
4 + 1 = 5
Below is a simple coded example of counting.
Counting example
In the above example you will see that we have a variable called 'number' that has been assigned the integer value 0.
On line 2 we have started a loop that will repeat until number = 11.
Line 3 will output the value of number
Line 4 will add 1 to the current value of number
The important line here that achieves the counting effect is line 4. Line 4 has been set to add 1 to the number variable, this means that on each repetition of the loop, number will increase by 1.
The output of this program would look like this:
On line 2 we have started a loop that will repeat until number = 11.
Line 3 will output the value of number
Line 4 will add 1 to the current value of number
The important line here that achieves the counting effect is line 4. Line 4 has been set to add 1 to the number variable, this means that on each repetition of the loop, number will increase by 1.
The output of this program would look like this:
Counting Output
Alternative counting
Non uniform counting
As well as counting by 1 e.g. 1,2,3,4 it s also possible to count int 2s, 3s or whatever number your require. to achieve this you would modify the counting line.
Instead of adding 1 e.g.
number = number + 1
you would adjust the 1 to the desired number:
number = number + 2
This would produce a count increasing by 2 each repetition i.e. 2,4,6,8...
Negative Counting
As well as counting positive numbers it is also possible to count backwards or to count negative numbers. To achieve this you simply have to change the + into a . This will result in the value decreasing by 1 on each repetition e.g.
As well as counting by 1 e.g. 1,2,3,4 it s also possible to count int 2s, 3s or whatever number your require. to achieve this you would modify the counting line.
Instead of adding 1 e.g.
number = number + 1
you would adjust the 1 to the desired number:
number = number + 2
This would produce a count increasing by 2 each repetition i.e. 2,4,6,8...
Negative Counting
As well as counting positive numbers it is also possible to count backwards or to count negative numbers. To achieve this you simply have to change the + into a . This will result in the value decreasing by 1 on each repetition e.g.
Negative counting example
Negative counting output
Counting specific things
Sometimes you may not simply want to count from one number to another, your count may be actually counting the occurrence of something.
Example 
A Program will continually ask the user to enter a number. The program must count the number of times that the number 3 is entered. The program ends when the user types 1.
To achieve this we will need to use selection (IF). Only if the input is 3 will we add 1 to the count variable... see the example code below.
Example 
A Program will continually ask the user to enter a number. The program must count the number of times that the number 3 is entered. The program ends when the user types 1.
To achieve this we will need to use selection (IF). Only if the input is 3 will we add 1 to the count variable... see the example code below.
Counting 3s
Vertical Divider