Bits of Bytes.co

IGCSE / GCSE Computer Science

  • Home
  • 1 - Theory of Computer Science
  • 2 - Practical Problem Solving and Programming
  • Pre-Release Material
    • 2018 June >
      • 22 >
        • 22 - Cow Task
        • 22 - Task 1
        • 22 - Task 2
        • 22 - Task 3
  • 1 - Computer Systems (2023)
Picture
MENU
  • Home
  • 1 - Theory of Computer Science
  • 2 - Practical Problem Solving and Programming
  • Pre-Release Material
    • 2018 June >
      • 22 >
        • 22 - Cow Task
        • 22 - Task 1
        • 22 - Task 2
        • 22 - Task 3
  • 1 - Computer Systems (2023)
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.

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
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:
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.
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.
Counting 3s

    


Vertical Divider

Totaling


Totaling works in a very similar war to counting, however rather than adding 1 to a variable you will be adding a number of un linked values to find the sum of them.

Totaling is usually used with iteration.  on each repetition a value will be added to the total variable - essentially keeping a running total.

​The following sample of code demonstrates how you would add 10 user entered nmbers together using totaling:
Adding 10 numbers

    
Line 5 is the important line here.  This is the line that achieves the totaling.  On each repetition, the total variable will become itself plus the new number that has been input.

Just like with counting, Totaling is flexible and can be manipulated with logic.  Selection statements can be incorporated to determine conditions where you may or may not add a value to the total e.g. you may only want to add a value to the total if it is greater than 100.
Est. 2015 - Copyright © 2020