Problem Solving
Program Design
Problem Specification - Mean 1
A program is required whose purpose is to receive four numbers, and to write out the mean value.
First stage is to ensure the specification is clearly understood. e.g. with numbers they could be:
- positive
- negative
- integers
- real numbers
What is a number? How are the numbers to be input? Electronic media or keyboard input? How are they to be displayed? Print out or screen?
Any specification must make these things clear.
A top level design for this would be:
- read in the four numbers
- calculate the mean of the four numbers
- write out the mean
The machine must store the numbers as variables with each assigned an identifier. Variables can either remain the same as the program advances or it could be changed as in a counting or adding instruction. Possible variables for Mean 1 could be:
- FirstNumber
- SecondNumber
- ThirdNumber
- FourthNumber
- MeanNumber
A data table should be made out to show the the type of data, (Integer) its identifier (Name) and a description of it's role. Refining the top level design brings out the individual steps needed.
- Read in the numbers
- 1.1 Read in FirstNumber
- 1.2 Read in SecondNumber
- 1.3 Read in ThirdNumber
- 1.4 Read in FourthNumber
Notation for assignment of value to variables is FirstNumber <— 3.142. where the identifier is on the left and is assigned the value on the right. the assignment does not have to be an actual number, it could be another variable identifier as in FirstNumber <— SecondNumber. Or even a composite of other variables e.g. FirstNumber <— (SecondNumber + 3.142) * 99.9.
Completing the design will introduce more steps.
- Read in the numbers
- 1.1.1 write out "Enter the first number: "
- 1.1.2 Read in FirstNumber
- 1.2.1 write out "Enter the second number: "
- 1.2.2 Read in SecondNumber
- 1.3.1 write out "Enter the third number: "
- 1.3.2 Read in ThirdNumber
- 1.4.1 write out "Enter the fourth number: "
- 1.4.2 Read in FourthNumber
- 1 MeanNumber<—(FirstNumber + SecondNumber + ThirdNumber + FourthNumber)/4
- 1 Write out "The mean value is "
- 3.2 write out MeanNumber
Coding a design in its final form should be straightforward. there should be a single sequence of steps that mirror the design to produce a sequence of statements in C++ code.
//---------------------------------------------------------------------------
#include <vcl.h>
#include "MT262io.h"
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
int First;
int Second;
int Third;
int Fourth;
float Mean;
First = ReadIntPr("Enter first number: ");
Second = ReadIntPr("Enter second number: ");
Third = ReadIntPr("Enter third number: ");
Fourth = ReadIntPr("Enter fourth number: ");
Mean = (First + Second + Third + Fourth)/4;
WriteFloatPr("The mean value is ", Mean);
getchar();
return 0;
}
//---------------------------------------------------------------------------
The first four lines of code define the variables as integers with their identifiers.
int First; int Second; int Third; int Fourth;
The fifth line defines the variable as a float with its identifier.
float Mean;
The line below reads as Read an Integer value from the keyboard Prompt.
ReadIntPr
The use of the equal sign in this code does not mean equal! C++ assigns variable values using the equal (=) sign. C++ uses a double equals sign (==) for equals.
Comments, suggestions, ideas to
Stuart Banner
