Systematic and Structured Programming
Step Three - Identifying and Locating the Functions
Once again, step two was not difficult. In fact, between writing steps one and two, you probably still don't have a whole 20 minutes invested in this process. That's just one of the benefits here.
Once again (again), you will also find that step three is just an expansion of step two, although you will be taking some new actions here. Mostly however, you will just be filling in areas that are guided by what you wrote in your previous steps.
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_StepThree. You must then copy the contents of the main function in QuadProg_StepTwo into the main function of your QuadProg_StepThree. At this point, you want to save what was stored as step two for your future review.
Now, you must work your way through the program from top to bottom following the iterative refinement process. This time, you will be identifying the need for, and the use of, functions in your program, but because you are not required to develop your own functions, you will not be creating any new functions in this activity. You will only use those developed by others.
Start here (iteratively)
Initialize the program
// initialize program
// initialize variables
// show title
- There is still very little to do here, but showing the title does require the use of the C language output process.
- Note for all the code components you don't recognize right now, you will be learning about in later topics. For the time being, you only need to see them used, and when the lesson comes along, you will already be familiar, and you will know how to integrate your learning into a program.
- So, you will specify the use of the output operation under the "show title" statement, and your code will look like the following:
// initialize program
// initialize variables
// show title
// function: printf
- You have specified that you want to print the title for the user. This will require a new function from the stdio.h library called printf. This function will display the title but the library must be identified using an include statement near the top of the program. It will go right under the Header Files comment as shown here
// Header Files
#include <stdio.h>
Get the coefficients from the user
// get coefficients from user
// get coefficient A
// get coefficient B
// get coefficient C
- You have specified that you want to get the three coefficients from the user. This will require a new function from the stdlib.h library called scanf. This function will acquire a double value and since you have already set the library previously when you were printing the title, you don't have to do anything else here. Just be aware that any tools you use that you do not create yourself will come from libraries, and you will always be responsible for including the appropriate libraries
// Header Files
#include <stdio.h>
#include <stdlib.h>
// get coefficients from user
// get coefficient A
// function: printf, scanf
// get coefficient B
// function: printf, scanf
// get coefficient C
// function: printf, scanf
Process the quadratic roots
// Process the quadratic roots
// calculate the discriminant
// calculate the square root of the discriminant
// calculate the denominator
// calculate roots
// calculate root one
// calculate root two
- With one exception, each of the actions required here is "just" a math operation. Later on, it will be interesting to create functions for these, but since you are not at that point yet, you will plan on using mathematical operations to conduct these specified operations. For that reason, there is no need to create any functions here
- However, there will be a need for calculating the square root which requires another function that can be found in a library. The sqrt function is found in the math.h library so the program will be updated in two places. First, you must note that you are using the square root function in the design statements, as follows:
// Process the quadratic roots
// calculate the discriminant
// calculate the square root of the discriminant
// function: sqrt
// calculate the denominator
// calculate roots
// calculate root one
// calculate root two
- The math.h library must be included in the header files block as was the stdio.h library. This will be placed at the top of the file with the other header file as shown.
// Header Files
#include <stdio.h>
#include <stdio.h>
#include <math.h>
Display the roots
// display roots
// display user input
// display root one
// display root two
- The actions are also pretty easy for output display, but you will be required to use the printf operations for this.
- Here are your output statements with the functions specified and located.
// display roots
// display user input
// function: printf
// display root one
// function: printf
// display root two
// function: printf
End the program
// end program
// display program end
// return function success
- With the ending display, printf must be used one more time. The function component is shown below.
// end program
// display program end
// function: printf
// return function success
Completing Step Three
- You have now finished specifying the requirements and locations of the program functions that you will need. The design part of step three is complete.
- Since you are not required to create any of your own functions, you don't have anything more to do in step three. Later, when you are creating your own functions, there will be a little more to do at this point
Some more notes:
- Without your own function development, this step still does not take very long to write. However, you have now prepared yourself for the fourth and last development step of this program
- Reminder if you haven't already done it: In order to conduct this part of the process, you must create a new program file named in your Project_1 file structure. This program file will be named QuadProg_StepThree. You must then copy the contents of the main function in QuadProg_StepTwo into the main function of your QuadProg_StepThree. You want to keep your step two 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 your function locations and identify your header files, you must still make sure the program compiles without errors or warnings (i.e., there are no warnings or errors displayed when you compile). This continues to be trivial because you are not adding any code although you did add some include statements. From here, you can move ahead to writing the step four code.
To see the step three process in action, watch this video; then develop your step three code with or without the video as needed in your own editor.