Developing a non-trivial program can be a major undertaking. To make the work as painless as possible, it is best to have a method of attack. There are two techniques which are commonly used: bottom-up and top-down.
Both require pre-planning. This is best done by creating pseudocode.
Typically, when a problem is posed, you will have an idea of what the output should be like. From there you determine what sort of information is required to develop the output. Then you can fill in the middle of your algorithm -- how to process the input so you can produce the output.
Alternatively, you can brainstorm the kinds of tasks that will be required, making a list as they come to mind. These can then be organized into a coherent algorithm, thus producing the requisite pseudocode.
Once the pseudocode has been written, it will be much easier to write the individual functions to perform the required tasks.
For the purposes of this discussion, let us consider the overworked area-of-a-triangle program. Here is simple pseudocode:
We will be developing the program discussed in Functions II. There you will find a complete presentation of the parameters used.
To begin we will write a simple program which calls a function to print out the results. (The main line of this simple program is know as the driver.) The function needs the values for the base, height and area. The values do not need to be altered inside the function, so their values, and not their addresses, will be passed to the PrintingResults(). To test the function, we will hard-set the three variables in the main line.
#include <stdio.h> /* standard I/O header file */
/* function prototypes */
void PrintingResults(int, int, float);
void main()
{
int base;
int height;
float Area;
base = 7;
height = 5;
Area = 17.5;
PrintingResults(base, height, Area);
}
void PrintingResults(int b, int h, float A)
{
printf("\n");
printf("The area of a triangle with a base of %d units and\n", b);
printf("a height of %d units is %.1f square units.\n", h, A);
} /* PrintingResults */
Next, we will write the function to calculate the area. This function will not alter the variables for base and height, but the area will be changed, since that is the purpose of the function. Thus b and h will be used to access values on the stack and A will be the address of Area.
Since we are calculating a value for the area, only base and height will be hard-set in the main line. To ensure that the values for the base and height have not been altered and that the correct value has been calculated for the area, their values are printed in the main line after the function call.
#include <stdio.h> /* standard I/O header file */
/* function prototypes */
void Calculation(int, int, float *);
void main()
{
int base;
int height;
float Area;
base = 7;
height = 5;
Calculation(base, height, &Area);
printf("base = %d\n", base);
printf("height = %d\n", height);
printf("Area = %.1f\n", Area);
}
void Calculation(int b, int h, float *A)
{
*A = b * h / 2.0;
} /* Calculation */
Alternatively, we could add the definition of Calculation() to the previous program (thus building the final product step by step) and use the following main line:
void main()
{
int base;
int height;
float Area;
base = 7;
height = 5;
Calculation(base, height, &Area);
PrintingResults(base, height, Area);
}
It is now time to create the function for inputting data. Both the base and height of the triangle have heretofore been hard-set in the main line. Since their values will now be obtained from the user of the program, the parameters for the function need to be the addresses of base and height.
#include <stdio.h> /* standard I/O header file */
/* function prototypes */
void InputtingData(int *, int *);
void main()
{
int base;
int height;
float Area;
InputtingData(&base, &height);
printf("base = %d\n", base);
printf("height = %d\n", height);
}
void InputtingData(int *b, int *h)
{
printf("What is the length of the triangle's base? ");
scanf("%d", b);
printf("What is the triangle's height? ");
scanf("%d", h);
} /* InputtingData */
Once we are certain that this small test program works, we can add the InputtingData() function to our growing program and use the following main line to test it.
void main()
{
int base;
int height;
float Area;
InputtingData(&base, &height);
Calculation(base, height, &Area);
PrintingResults(base, height, Area);
}
All that remains is for us to write the Introduction() function and add a call to it at the beginning of the main line. The end result of our programming exercise is the program listing given below. A discussion of this program from the perspective of passing values and addresses can be found in Functions II.
#include <stdio.h> /* standard I/O header file */
/* function prototypes */
void Introduction();
void InputtingData(int *, int *);
void Calculation(int, int, float *);
void PrintingResults(int, int, float);
void main()
{
int base;
int height;
float Area;
Introduction();
InputtingData(&base, &height);
Calculation(base, height, &Area);
PrintingResults(base, height, Area);
}
void Introduction()
{
printf("This program calculates and prints the area of a triangle\n");
printf("after you enter its dimensions.\n");
printf("When asked to, type in a dimension and hit the RETURN key.\n");
printf("\n");
} /* Introduction */
void InputtingData(int *b, int *h)
{
printf("What is the length of the triangle's base? ");
scanf("%d", b);
printf("What is the triangle's height? ");
scanf("%d", h);
} /* InputtingData */
void Calculation(int b, int h, float *A)
{
*A = b * h / 2.0;
} /* Calculation */
void PrintingResults(int b, int h, float A)
{
printf("\n");
printf("The area of a triangle with a base of %d units and\n", b);
printf("a height of %d units is %.1f square units.\n", h, A);
} /* PrintingResults */
The techniques discussed above are not used to the exclusion of others. In fact, the method outlined on the Top-Down page is frequently utilized to create an overview of the whole project, after which individual procedures are developed. This combined approach is especially important when a group of programmers is working on a single project.
© 2001 DFStermole
Created: 1 Dec 01
Last modified: 3 Dec 01