Systematic and Structured Programming, Part 2
Step Two - Expanding the Solution
As you found before, your step one action was pretty easy, and you have accomplished an important thing. Now that you have the overall flow of the program fleshed out, almost all of the remaining programming work will involve "filling in the blanks" in a kind of ever-expanding strategy that includes both a top-down and an iterative refinement process.
If you are working through this with the web page, you should create a new QuadProg_StepTwo.c and copy your QuadProg_StepOne.c work into it now so that you don't overwrite your first step.
Consider the following items from the previous step:
Initialize the program
- You have specified initialization of the variables, which you know you will always do
- Now you can expand this a little bit by adding a title to the program. There is not much more to do to expand on this part
- The pseudocode for this part would look like the following:
// initialize program
// initialize variables
// show title
Get the coefficients from the user
- You should now specify what getting the coefficients means. For this program, it means the following:
- get the coefficient A from the user
- get the coefficient B from the user
- get the coefficient C from the user
- The pseudocode for this part would look like the following:
// get coefficients from user
// get coefficient A
// get coefficient B
// get coefficient C
Process the quadratic roots
- You will now expand on what calculations must be accomplished
- The solution for the roots is shown here:
- To conduct this operation in an appropriately modular way, you will implement the following solution activities:
- you must calculate the value under the radical, called the discriminant
- you must calculate the square root of the discriminant
- you must calculate the denominator
- finally, with all the components or modules calculated, you will be able to calculate root one and root two of the equation; note that this component is broken down to another level of abstraction
- The pseudocode for this part would look like the following:
// process the quadratic roots
// calculate the discriminant
// calculate the discriminant square root
// calculate the denominator
// calculate roots
// calculate root one
// calculate root two
Display the Roots
- This process should be easy, but you still have to consider displaying:
- the original user input
- root one
- root two
- The pseudocode for this part would look like the following:
// display roots
// display user input values
// display root one
// display root two
End the program
- For this program, the following needs to happen:
- some indicator of the completed program should be implemented. For this one, we can simply display the "end program" text
- all C programs need to have a return 0 at the end to indicate to the operating system that the program was successful; this will be added here
- The pseudocode for this part would look like the following:
// end program
// display program end
// return success to the operating system
return 0;
The Whole Schlemiel
- When all of the pseudocode pieces shown above are put together in a program, the main function code for step two would look like the following:
int main()
{
// initialize program
// initialize variables
// show title
// get coefficients from user
// get coefficient A
// get coefficient B
// get coefficient C
// process the quadratic roots
// calculate the discriminant
// calculate the discriminant square root
// calculate the denominator
// calculate roots
// calculate root one
// calculate root two
// display roots
// display user input values
// display root one
// display root two
// end program
// display program end
// return success to the operating system
return 0;
}
This again does not take very long to write, but you have expanded what the main part of your program will do now. You have prepared yourself for the next part of the programming process, which is to break the problem down into manageable problem-solving components.
Some notes:
- In order to conduct this part of the process, you must remember to save the contents of your QuadProg_StepOne.c file into a new file names QuadProg_StepTwo.c before you make any changes. You want to keep your step one program exactly as it was so that if there are questions in the future about your overall strategy, you can go back and review the code.
- As mentioned previously, you are now involved in a process called iterative refinement. This means that you will repeat the same cycles through the code and descriptions you have generated, but make small improvements during each repetition. For example, your first pass (step one) through this program generated a short program with only a few lines of actual program statements. Now you have gone back through the same program in step two, and refined it by adding and expanding detailed elements of each of the major steps. Your whole process of program development will continue like this; that is, you will go back through the same program code adding and improving to each step in a repetitive action, adding, expanding, and improving your program by a little bit until it is finished
- Remember as you type in your expansion comments, you must still make sure the program compiles without errors or warnings by compiling after at least every two or three lines you write. This continues to be trivial because you are not adding any code. However, it is still important that no unknown issues pop up right now, and besides, it is a great habit to get into (that you will be using a lot later on)
- Note again that even though you have extended your code, you still don't have more than ten (10) actions at any level of the program (i.e., at any indented level). This continues to be critical because if you see this happening, you will recognize that you are trying to solve too big a problem, and that it is time to break the problem down some more. If you are ready to continue, you can move on as instructed next
To see the step two process in action, watch this video; then develop your step two code with or without the video as needed.