Finishing the Program with Four Steps
Once again, step three was not difficult. In fact, between writing steps one, two, and three, you probably still don't have a whole 30 minutes invested in this process. Again, that's just one of the benefits.
During most of an introductory programming course, you will be using functions to develop your programs. However, for the first few weeks before you learn about creating functions, the Six Step Programming Process will essentially end at this step (four); without further function development, steps five and six are not necessary. So, for a program development process that does not involve creating functions, this ends here for the time being.
If you are working through this with the web page, you must create a new program file named in your Project_1 structure. This program file will be named QuadProg_StepFour. You must then copy the contents of the main function in QuadProg_StepThree into the main function of your QuadProg_StepFour. At this point, you want to save what was stored as step three for your future review. Now you must again work your way through the program from top to bottom in the iterative refinement process. This time, and under the circumstances that you will not be creating functions, you will simply be following your previous instructions to write the code for your program.
Initialize the program
// initialize program
// initialize variables
// show title
// function: printf
- You will be initializing variables as you write the program, but since you do not know at present which variables you might need, you will not be attending to that issue now
- However, you do need to output the title here, so you must write the code needed to do this, and it is obviously pretty easy
// initialize program
// initialize variables
// show title
// function: printf
printf( "QUADRATIC ROOTS PROGRAM\n" ); printf( "=======================\n\n" );
- To create the title, use the printf function to print the title and the underline, and then provide an extra endline character ( '\n') to the underline so it will make an extra space between the title and the input which occurs next.
Get the coefficients from the user
// get coefficients from user
// get coefficient A
// function: printf, scanf
// get coefficient B
// function: printf, scanf
// get coefficient C
// function: printf, scanf
- This operation is simple in the sense that it is one pair of actions repeated three times, once for each coefficient. However, you will need three variables to hold the double input data from the user. So before the input process is developed, declare the three needed variables, as shown here
// initialize program
// initialize variables
double coef_A, coef_B, coef_C;
- Remember to compile and check for errors or warnings after declaring these variables so no problems show up later
- Now you may prompt the user for the double input values, as shown here. Remember to check for errors or warnings after typing each line. Don't sweat it if some of the code is not clear right now. Again, you have not been exposed to all of these code operations before, but when you do start working with them, you will be using appropriate structure
// get coefficients from user
// get coefficient A
// function: printf, scanf
printf( "Enter coefficient A: " );
scanf( "%lf", &coef_A );
// get coefficient B
// function: printf, scanf
printf( "Enter coefficient B: " );
scanf( "%lf", &coef_B );
// get coefficient A
// function: printf, scanf
printf( "Enter coefficient C: " );
scanf( "%lf", &coef_C );
- This will take care of the input operations, however there are a couple of things to note. First, there is no '\n' in the prompt. This means the user will enter the input data on the same line as the prompt, which is the professional practice. Also there is an ampersand '&' to the left of the coef identifiers. This is necessary for the scanf to work and you will learn more about this later on.
Process the quadratic roots
// Process the quadratic roots
// calculate the discriminant
// calculate the square root of the discriminant // function: Math.sqrt
// calculate the denominator
// calculate root one
// calculate root two
- There are several actions to be taken here, but that just provides an opportunity to practice with the code. The important thing about your present actions is that your design process has been completed. You now have only to follow the instructions you have provided for yourself
- For the first operation ("calculate the discriminant"), you have specified that you want to calculate the discriminant. This means you need to calculate the value under the radical. Since you will be calculating a discriminant, you must create the variable discriminant, as shown here
Update the variables
- You will need the discriminant variable, so it gets declared now.
// initialize program
// initialize variables
double coef_A, coef_B, coef_C;
double discriminant;
- Compile after adding to this line. If everything is okay, move on to the next step
- Calculating the discriminant is a simple process, and is shown here
// Process the quadratic roots
// calculate the discriminant
discriminant = coef_B * coef_B - ( 4 * coef_A * coef_C );
- After you type in this code, remember to check for errors or warnings
- For the next operation ("calculate the square root of the discriminant"), the square root of the discriminant must be found. You will need a variable for this, so sqrtOfDisc is declared (above)
- Note that sqrtOfDisc must be a double value because the value returned from the sqrt function you will use is supposed to be a double. Compile after you add this variable to make sure everything is okay
// initialize program
// initialize variables
double coef_A, coef_B, coef_C;
double discriminant, sqrtOfDisc;
- Now that you have created a variable for acquiring the square root, go ahead and type the operational code, compiling immediately after
// calculate the square root of the discriminant
// function: sqrt
sqrtOfDisc = sqrt( discriminant );
- For the next operation ("calculate the denominator"), you have specified that you want to calculate the denominator of the quadratic equation. This means you will need to create the denominator variable, as shown
// initialize program
// initialize variables
double coef_A, coef_B, coef_C;
double discriminant, sqrtOfDisc, denominator;
- The denominator calculation is also pretty simple, and is shown here. Remember to compile after typing the line
// calculate the denominator
denominator = 2 * coef_A;
- Finally, for the last root processing operations ("calculate root one" and "calculate root two"), you have specified that you want to calculate the two roots of the equation using the solution components you have already generated (i.e., the square root of the discriminant and the denominator). You must create the two root variables as shown here, remembering to compile after adding these variables
// initialize program
// initialize variables
double coef_A, coef_B, coef_C;
double discriminant, sqrtOfDisc, denominator;
double rootOne, rootTwo;
- Note again that since the sqrtOfDisc and the other variables are double values, the two root variables need to be doubles since they will be acquiring data calculated using the sqrtOfDisc variable
- The operations for calculating the roots are shown here with the discriminant square root being added to one calculation and subtracted from the other. Compile after typing each of these statements
// calculate root one
rootOne = ( -coef_B + sqrtOfDisc ) / denominator;
// calculate root two
rootTwo = ( -coef_B - sqrtOfDisc ) / denominator;
- This will complete your root processing actions
Display the roots
// display user input
// function: printf
// display root one
// function: printf
// display root two
// function: printf
- The actions are also pretty easy for output display, so you only have to output the descriptive text, the user input, and the root results, as shown
// display user input
// function: printf
printf( "For coefficients of %4.2f", %4.2f", and %4.2f\n",
coef_A, coef_B, coef_C );
// display root one
// function: printf
printf( "root one is: %4.2f", rootOne );
// display root two
// function: printf
printf( "and root two is: %4.2f"\n, rootTwo );
- For this action, you did not create any new variables, so no other action needs to be conducted. Again however, make sure you compile after you have written this code
- Notice that the second output statement for rootTwo ends with '\n' when the code for rootOne does not. As mentioned previously, the '\n' ends the line which did not need to happen until rootTwo was printed
End the program
// end program
// display program end
// function: printf
// return function success
- This is easily resolved by placing the output statement, as shown here
// end program
// display program end
// function: printf
printf( "\nEnd Program" );
// return function success
return 0;
- Note in this case the end line character ( '\n' ) prior to the "End" text. This is the same as pressing ENTER on your keyboard and makes an extra vertical space before printing "End Program". It keeps the end of the program from being jammed up against the program output
- As previously stated, when you are writing programs without functions (which won't last very long in most introductory programming courses), your work ends at step four
- step one is very short and only asks you to show a brief high-level overview of your program
- step two is also pretty short, but includes your expansion of step one with specific actions that will create your solution; nevertheless, it is still very important that you have created all the steps for solving the problem before you leave this step. When you are done here, you will usually have a comment line for every line of code you will write; this leads to just filling in one line of code at a time when the time comes
- step three is also pretty short, but you must still identify places where you will be using functions, and you will have to add include statements as needed to provide access to external utilities
- in step four, you actually write out all the code required of the program; step four is used to write the main function code whether you create your own functions or not. As you have seen, step four is pretty easy because you already have instructions as to what you need to do; you just have to follow them
- Once you have finished step four, you will have a working program; you only need to run it and verify its correctness. In the future, when you get to step four, you will still implement or write the code for the main function exactly like you did here, but you will still need to design and implement the code for your supporting functions, which is what you will be doing in steps five and six
- It is pretty impressive to just tackle these little parts of the program one at a time, and when you are done, have a working program. However, it is not magic. It is simply good quality, well-organized, and structured programming. This will make your programs better, and it will make your programming process faster and easier
Some more notes:
- This is not difficult. It is structured programming. And every time you practice it, you will get better and faster at it
- Reminder if you haven't already done it: In order to conduct this part of the process, you must create a new class named in your Project_1 structure. This class will be named QuadProg_StepFour. You must then copy the contents of the main function in QuadProg_StepThree into the main function of your QuadProg_StepFour. You want to keep your step three 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.
- Remember as you type in any parts of your code, you must still make sure the program compiles without errors or warnings. Your programming will go much faster if you frequently compile. The only thing that can make this process take too long is if you write several lines of code at once without checking for errors or warnings, and then spend way too long trying to figure out what all the errors mean
- One more little issue: Apparently in the most recent versions of Linux, they are not including the math library (i.e., "math.h"). This means that depending on your system, some students may have to add a -lm ("dash, ell, em") to the end of their compiling process, as follows: gcc -Wall QuadProg_StepFour.c -lm -o QuadProgram.exe. If you see a compiler issue related to your math.h file, add this extra switch into your compiling and that should take care of it.
To see the step four process in action, watch this video; then develop your step four code with or without the video as needed in your own editor.