Finding the Largest Member in an Array
As an example on program development using Structured Programming techniques, we will create an algorithm for finding the largest number in an array of numbers.
The first step is to create a flowchart. Pseudocode can be used instead of flowcharts or they can be used together.
Flowcharts are a visual way of showing the flow of control within a program.
Pseudocode is a way of communicating the same process using natural language.
In either case, you want to use as little jargon as possible. Also, the algorithm is presented in a way that is independent of the computer programming language to be used.
Remember, code comes last.
To begin the process, we demonstrate the use of a WHILE-DO control structure as follows:
Your flowchart will not appear as such. You will modify the text in the control boxes to reflect the problem being tackled.
In the next flowchart, for demonstration purposes only, I show the text on the right which will be inserted into the control box on the left.
For every situation, there may be some special cases. We would like algorithms to work for all cases. We need to clearly identify such special cases so that our algorithm works for both general and special cases.
For example, will the algorithm work if there are no numbers in the array?
Does the index of the array begin at 0 or at 1?
Here is the updated flowchart, pseudocode, and C code for this task:
It is imperative that the algorithm as shown is correct. If what is shown is incorrect then our code will also be incorrect.
Once we can ascertain that the algorithm is correct, we can proceed to producing code.
Every control structure or line of pseudocode ought be transferable into programming code, almost on a one-to-one basis.
Code
For the purpose of this demonstration, I will use the C programming language. In this example I do not use some obvious optimization of C code in order convey the intention of the code more clearly.
There are a number of ways to implement iteration. Any one of the following can be used:
Here is the same task written with FOR-NEXT iteration:
Coding Style
Consistent personal coding style can go a long way in producing readable and error free code.
Pay attention to the following:
As an example on program development using Structured Programming techniques, we will create an algorithm for finding the largest number in an array of numbers.
The first step is to create a flowchart. Pseudocode can be used instead of flowcharts or they can be used together.
Flowcharts are a visual way of showing the flow of control within a program.
Pseudocode is a way of communicating the same process using natural language.
In either case, you want to use as little jargon as possible. Also, the algorithm is presented in a way that is independent of the computer programming language to be used.
Remember, code comes last.
To begin the process, we demonstrate the use of a WHILE-DO control structure as follows:
Your flowchart will not appear as such. You will modify the text in the control boxes to reflect the problem being tackled.
In the next flowchart, for demonstration purposes only, I show the text on the right which will be inserted into the control box on the left.
For every situation, there may be some special cases. We would like algorithms to work for all cases. We need to clearly identify such special cases so that our algorithm works for both general and special cases.
For example, will the algorithm work if there are no numbers in the array?
Does the index of the array begin at 0 or at 1?
Here is the updated flowchart, pseudocode, and C code for this task:
It is imperative that the algorithm as shown is correct. If what is shown is incorrect then our code will also be incorrect.
Once we can ascertain that the algorithm is correct, we can proceed to producing code.
Every control structure or line of pseudocode ought be transferable into programming code, almost on a one-to-one basis.
Code
For the purpose of this demonstration, I will use the C programming language. In this example I do not use some obvious optimization of C code in order convey the intention of the code more clearly.
There are a number of ways to implement iteration. Any one of the following can be used:
- IF-THEN-ELSE
- WHILE-DO
- DO-WHILE
- REPEAT-UNTIL
- FOR-NEXT
Code (Text):
- // make sure to document all assumptions
- // assume the following:
- // array is not empty
- // number N of elements in the array is known and declared globally
- // array A[] is declared globally
- // index of the array begins with 0
- // assume declared globally in main.c
- int N = 10;
- int A[10];
- int Largest(void)
- { // start of Largest()
- int largest;
- int i;
- i = 0;
- largest = A[i];
- i = i + 1;
- while ( i <= N)
- { // start of WHILE-DO
- if ( A[i] > largest )
- {
- largest = A[i];
- }
- i = i + 1;
- } // end of WHILE-DO
- return (largest);
- } // end of Largest()
Here is the same task written with FOR-NEXT iteration:
Code (Text):
- // make sure to document all assumptions
- // assume the following:
- // array is not empty
- // number N of elements in the array is known and declared globally
- // array A[] is declared globally
- // index of the array begins with 0
- // assume declared globally in main.c
- int N = 10;
- int A[10];
- int Largest(void)
- { // start of Largest()
- int largest;
- int i;
- i = 0;
- largest = A[i];
- for ( i = 1; i < N; i++ )
- { // start of FOR-NEXT
- if ( A[i] > largest )
- {
- largest = A[i];
- }
- } // end of FOR-NEXT
- return (largest);
- } // end of Largest()
Coding Style
Consistent personal coding style can go a long way in producing readable and error free code.
Pay attention to the following:
- Choice of constant and variable names to make your code readable, example, days_in_month
- All caps for constants, example, NUMBER_OF_DAYS
- Capitalize first letter for global variables, example, Day_Of_Week
- Lower case for local variables, example, number_of_hours
- Put comments in opening { and closing } for very large structures. See code examples.
- Indent code blocks to reflect the control structure