Systematic and Structured Programming
Step One - Simple Action Statements
The Problem to Be Solved
The beginning of the process is to identify the problem and try to elicit all the important actions that your program is required to do. Here is an example program specification:
- The program solves for the roots of a quadratic equation
- For the following equation, it prompts the user for, and acquires, the x2 coefficient (A), the x coefficient (B), and the constant coefficient (C), for the following equation:
- Finally, the program provides the roots of the equation to the user as a displayed output. The first step is to identify the main parts of the program and write them in as comments. This program will only require a few simple statements. The following statements are provided with explanations, and then the code itself is provided.
Ax2 + Bx + C = 0
Initialize the program
- there will always be program initialization actions. Examples are:
- declaring and/or defining variables
- initializing or seeding a random number generator that is used elsewhere in the program
- setting the title for the program
Get the coefficients from the user
- this is pretty straight forward; many programs, although not all, will need some kind of user input to get started
Process the quadratic roots
- also very common, although in some programs calculations may be made at different points
Display the roots
- when the calculations are complete, the user input and the resulting data can be displayed in some formatted or easily understandable way
End the program
- there will always be some kind of program shut down process. Examples are:
- holding the program until the user closes it
- shutting down I/O interfaces if any special ones are used
- returning memory to the operating system
The main function code for step 1 would look like the following:
int main()
{
// initialize program
// get coefficients from user
// process quadratic roots
// display results
// end program
return 0;
}
The program would be set up as follows:
#include <stdio.h>
int main()
{
// initialize program
// get coefficients from user
// process quadratic roots
// display results
// end program
return 0;
}
As you may notice, it wouldn't take more than a few minutes to write this, and while it may seem trivial, it is very important because you now have a feeling for how the program will flow. Even more importantly, you have broken the program down into smaller, more manageable problems.
Some notes:
- The comments should be placed into your basic program file, and saved in QuadProg_StepOne. Note that the program name is provided, then the step number, and optionally a version number (e.g., QuadProg_StepOne_v1). Your versions do not commonly change for steps one and two, but as your programs evolve, you may find that you have more versions of steps three and four. For purposes of this tutorial, version numbers are not provided in order to reduce complexity in the learning activities
- Note that there are fewer than ten (10) primary actions in the main function. If any function, main or otherwise, has too many primary or major actions (e.g., more than 8 to 10), the function must be broken down into smaller steps
- It is also important that your program compiles without any errors or warnings at this point. This may sound trivial, and it is, because your program will only be a shell of its future self. It will have the fundamental program components, which include initialization, input, processing, output, and program completion, it will have the main function, and it will otherwise mostly be comments. However, you need to make sure that these fundamental program components are all working now so they don't pile up as a longer list of problems later
- Finally, this chapter is written like a tutorial so that you can follow the process of developing a program from beginning to end. Make sure you follow the process and practice writing the code as you go through it
To see the step one process in action, watch this video; then develop your step one code with or without the video as needed in your own enviroment.