Algorithm:
- Read 5 numbers from the user
- Find the smallest and the greatest
- Output the smallest and greatest number
Some comments
- To achieve better readability, it may be advisable to explicitly write down your variables and possibly also comment on what variable is used for which purpose.
- Repeated actions can be abstracted to some extent. In this case we have described finding the maximum value as a repeated action from the previous column (finding minimum), also stating the differences compared to it.
- The numbers are stored in an array. Arrays are indexed and the indexes start from zero.
- In the algorithm I have shown both the human-readable text and the lines that one could use to implement this in code (e.g. i = 0, i++, i < 5, largestNum = num[i] etc.). It’s however more important to have the textual explanations that another human can read, indicating the purpose of each action. The code lines are just for help.
- The algorithm principle is that before doing any comparisons, we pick one number that we say is the smallest / greatest number. Only after that we can start to compare the rest of the array to see if any other number is greater or smaller than the one we picked. If we happen to find such a number, we will replace what we had stored so far with the new lowest / highest number and continue comparing with that.
- In this solution we assumed that the first element of the array (index 0) was the smallest / largest number from that array before checking the others. This is why the loops for the comparison start from the second element (i = 1) – we don’t need the compare the number we picked as the initial assumption. However it should be noted that other loops that we use for input and output will still need to start from the first element (i = 0), as in there we are addressing the entire array
- Common mistake: The smallest / largest element is fixed with some number in code that has nothing in common with the array. E.g. We are looking for the biggest element in the array and we initialize the value of our maximum to 0, assuming that everything will be larger than that. The makes our program fail to find the correct answer if all numbers are negative. The initial value for the minimum and maximum must always be picked from the array
Recommendations on writing the code:
- Try to figure out which parts from the UML are the loop conditions and which are just normal conditionals (just if statements). How to identify those?
- If statement does not need an else statement to accompany it. If there is no reason to do so, don’t write one.
- Tips for the advanced task: if both parts of the division are integers, the answer will be an integer.
Code sample
This is the base code written during the class. It contains reading of the array, finding the smallest element and its index and outputing those results and the initial array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
/** * File: extremum_basecode.c * Author: Risto Heinsar * Created: 11.09.2014 * Modified 27.09.2017 * * Description: Asks user for 5 numbers find the smallest number and its index * and repeats the original array back to the user. */ // includes the standard input output library that allows us to use text input and output // from keyboard and much more. #include <stdio.h> // this is a preprocessor directive or more simply - a macro. This value // can be used to avoid hard-to-understand values within the coder later #define ARR_LEN 7 int main(void) { // declare an array, member count ARR_LEN is defined previously int arr[ARR_LEN]; // also declares 3 different values needed within the program, integers int i, minIndex, minValue; // get the array from user input for (i = 0; i < ARR_LEN; i++) { // i + 1 in printf doesn't change the value of i permanently because // i'm not assigning it back, it's temporary and for user friendlyness only printf("Enter num %d of %d: ", i + 1, ARR_LEN); // we need to use & before the variable name only when reading numbers! scanf("%d", &arr[i]); } // initialize the variables before searching - assume that the first member is the smallest minValue = arr[0]; minIndex = 0; // start the loop to look for new smallest, if there is any. Starts from // 1 because we assumed that 0 was already the smallest for (i = 1; i < ARR_LEN; i++) { // check if the current element is smaller than what we already know if (minValue > arr[i]) { // stores both the value and the index minValue = arr[i]; minIndex = i; } } // print the original array printf("The input array: "); for (i = 0; i < ARR_LEN; i++) { printf("%d ", arr[i]); } printf("\nThe smallest element was %d with an index of %d\n", minValue, minIndex); // returning 0 from main() means that the program exited successfully. There // are other codes (numbers) to indicate failure. return 0; } |