Objectives: Students should be able to —
- 1 Use the standard method of Counting.
- 2 Use the standard method of Totalling.
- 3 Find maximum, minimum and average values.
- 4 Search using a Linear search.
- 5 Sort using a Bubble sort.
Counting, Totalling, Maximum, Minimum & Average
The total weight is stored in a variable Total, the number of baskets in BasketCount, the maximum in Max, the minimum in Min and the average in Avg.
(a) Totalling:
- Initialize the variable Total with value 0, to start totalling from it (like, Total ← 0).
- Totalling is done by adding new weight of the basket to the previous old total weight and storing it in the same variable Total, replacing its previous value, during the process of inputting the weight.
Example: Total ← Total + Weight
Counting:
- Initialize the variable BasketCount with value 0, to start counting from it (like, BasketCount ← 0).
- Counting is done by adding 1 to the previous number of baskets and storing it in the same variable BasketCount, replacing its previous value, during the process of inputting the weight.
Example: BasketCount ← BasketCount + 1
(b) Maximum:
- Initialize the variable Max to the lowest possible value (like, Max ← 0).
- Check if the current new weight of the basket is greater than the previous maximum value; if so, store the new weight in the same variable Max, replacing its previous value, during the process of inputting the weight.
Example: IF Weight > Max THEN Max ← Weight
Minimum:
- Initialize the variable Min to the highest possible value (like, Min ← 1000).
- Check if the current new weight of the basket is less than the previous minimum value; if so, store the new weight in the same variable Min, replacing its previous value, during the process of inputting the weight.
Example: IF Weight < Min THEN Min ← Weight
(c) Average:
- Calculate the running total of the weight and count the number of baskets during the process of inputting the weight.
- To calculate average, divide the total weight by the number of baskets (outside the loop).
Example: Avg ← Total / BasketCount
Given pseudocode:
| Operation | Line No. | Statement |
|---|---|---|
| Totalling | Line 12 | C ← C + D |
| Counting | Line 13 | A ← A + 1 |
| Range check | Line 07 | UNTIL D > 0 AND D < 100 |
| Calculating the average | Line 15 | B ← C / A |
(a) Find highest and lowest mark of 15 students:
(b) Count students sharing the highest and lowest marks:
Variables to use:
- Temp — to input and store the temperature.
- X — counter variable for the loop structure.
- MaxTemp, MinTemp and Avg — to find/calculate the maximum, minimum and average temperature.
- CountTemp — to count the temperature readings less than or equal to 0.
Linear Search
Linear Search: The method of checking each item of the list in turn to see if the item matches the value searched for.
How it works: A flag variable Found tracks whether the name has been matched. The loop walks the array one element at a time until either the name is matched or the end of the array is reached.
This uses a linear traversal of the array with a counter that increments only when each mark satisfies the pass condition (StdMark[Counter] >= 60).
By storing the index (position) of the highest and lowest marks in HighIndex and LowIndex, we can later look up the matching name in the parallel StdName[] array.
Bubble Sort
- Bubble Sort is an algorithm for arranging a series of numbers or other elements in the correct order.
- The method works by comparing each set of adjacent elements of the entire list, from left to right, swapping their positions if they are out of order.
- The algorithm then repeats this process until it can run through the entire list without swapping any elements.
(a) Sort 25 integers in ascending order:
(b) Changes to sort in descending order:
Change the conditional statement —
IF Num[Y+1] < Num[Y]
to
IF Num[Y+1] > Num[Y]
which will swap the numbers only if the next number is greater than the present number.
This swapping of numbers has to be done repeatedly until no further swapping is needed, to sort it in descending order.
(a) Sort names in ascending order:
(b) Initial contents of PeopleName[]:
| Index | [1] | [2] | [3] | [4] | [5] |
|---|---|---|---|---|---|
| Value | Daniel | Alex | Jose | Bob | Monty |
Trace table (each pass through the inner loop):
(" means the value is unchanged from the previous row.)
| Loop Counter | [1] | [2] | [3] | [4] | [5] | Temp | Swap |
|---|---|---|---|---|---|---|---|
| Daniel | Alex | Jose | Bob | Monty | — | 0 | |
| 1 | Alex | Daniel | " | " | " | Daniel | 1 |
| 2 | " | " | " | " | " | " | " |
| 3 | " | " | Bob | Jose | " | Jose | 1 |
| 4 | " | " | " | " | " | " | " |
| Alex | Daniel | Bob | Jose | Monty | " | 0 | |
| 1 | " | " | " | " | " | " | " |
| 2 | " | Bob | Daniel | " | " | Daniel | 1 |
| 3 | " | " | " | " | " | " | " |
| 4 | " | " | " | " | " | " | " |
| Alex | Bob | Daniel | Jose | Monty | " | 0 | |
| 1 | " | " | " | " | " | " | " |
| 2 | " | " | " | " | " | " | " |
| 3 | " | " | " | " | " | " | " |
| 4 | " | " | " | " | " | " | " |
Content of array PeopleName[] after sorting:
| Index | [1] | [2] | [3] | [4] | [5] |
|---|---|---|---|---|---|
| Value | Alex | Bob | Daniel | Jose | Monty |
(c) Changes to sort in descending order:
Change the conditional statement —
IF PeopleName[X+1] < PeopleName[X]
to
IF PeopleName[X+1] > PeopleName[X]
which will swap the names only if the next name is greater than the present name.
This swapping of names has to be done repeatedly until no further swapping is needed, to sort it in descending order.
Revision: Statements and Key Computing Terms
| Statement | Key Term |
|---|---|
| The method of keeping a running sum of values as they are entered. | Totalling |
| The method of keeping a running count of how many items have been entered. | Counting |
| The process of finding the largest value in a list of values. | Finding Maximum |
| The process of finding the smallest value in a list of values. | Finding Minimum |
| The sum of all values divided by the number of values. | Average (Mean) |
| A variable used to signal whether a condition (e.g. "found") is TRUE or FALSE. | Flag |
| A method of checking each item of a list in turn to see if it matches the value searched for. | Linear Search |
| An algorithm that arranges elements by comparing adjacent pairs and swapping them if they are out of order. | Bubble Sort |
| The act of exchanging the values of two variables (often using a temporary variable). | Swapping |
| The position number of an element within an array. | Index |
| A single run through a loop's body. | Iteration (Pass) |