1. Consider the lab problem below and develop an algorithm to solve it based on the information provided. Be sure to follow the first three steps in the software development process as you develop your algorithm.
THE SOFTWARE DEVELOPMENT PROCESS
a) Specify the problem requirements
b) Analyze the problem
c) Design an algorithm to solve the problem and tests to demonstrate the algorithm is working correctly
d) Implement the algorithm in a programming language
e) Test and verify that the implemented algorithm (program) is working correctly
f) Maintain and update the program
Write a summary of your algorithm development, showing each step in the process and making sure to include all of the items listed below. You algorithm development should be typed in a text or word processor file. You may add hand drawn diagrams or hand written equations to your printed solution. Your discussion of algorithm development should include each of the following:
a) Statement of the problem
b) A list of inputs, and a list of outputs along with a list of any mathematical relations demonstrating the relations between the inputs and the outputs
c) A series of hand examples, one for the common case and one for each of the special cases you identify
d) An algorithm based on the hand example for the common case
e) A refined algorithm to include your first special case
f) A final refined algorithm containing all your special cases
g) A test plan for testing the algorithm or the implemented program
2. Make a directory to hold all your files for this lab
a) Log in to a Linux machine (or a Cygwin window if you are working at home)
b) Open a terminal window
c) Make a directory for this weeks lab
d) Move into that directory
3. Write the program to implement your algorithm. You have two choices,
EITHER
a) Start kate by typing kate followed by <enter>
b) Enter some code
c) Select ‘save as’ from the file menu and save your new code to file in ;your directory for this lab with filename integreSums.c
d) Continue developing your code using kate, until you can save the final version of quadraticSolver.c and submit your solution.
OR
a) Make. a copy of an existing program in your new directory (expansion c might be a good choice ). Rename the copy so it is called quadraticSolver.c
b) Load the copy of the existing program into kate ( kate quadraticSolver.c )
c) Update the existing code so it is applicable to this problem.
d) Debug your code and submit the resulting quadraticSolver.c file as your implemented solution.
e) Remember that each time you make changes to your file you will need to save them (by selecting ‘save’ in the file menu) and reload the file before trying to compile or run the program.
f) Remember the commands to compile and link your program
gcc –Wall –c –o quadraticSolver.o
quadraticSolver.c
gcc ntegerSums.o -lm -o quadraticSolver.o
OR
gcc -Wall -lm -o quadraticSolver quadraticSolver.c
g) Remember that the command to run your code is
./quadraticSolver.c
4. When you have a completed executable file submit it to the submission server. Remember you need to submit a zipped tar file. To use tar and zip visit the instructions for using tar and zip.
5. Print a copy of the C text file containing your program (quadraticSolver.c), a copy of the algorithm development for your program containing all the information requested above. and a copy of the lab exercise cover sheet from the class website. Fill out and sign the cover sheet. Be sure that all members in the group must sign the cover sheet. Hand in the hard copy of your lab to the CMPT 102 drop box. The hard copy of this lab problem includes a listing of your C text file, your algorithm development, and a signed cover sheet.
LAB PROBLEM: Write a C program to find the real roots of the quadratic equation coeffA*x*x + coeffB*x + coeffC = 0. A root of the equation is a value of the variable x that makes the equation true. A quadratic equation can have up to two roots. The roots will depend on the values of the three coefficients of the equitation (coeffA, coeffB, and coeffC). The C code to solve this problem should be stored in a file called quadraticSolver.c.
To refresh your memory one would use the quadratic equation, given below, to find the value of x, given the values of the coefficients.
.
The following standard math library functions will be useful to you as you implement your algorithm
double sqrt(double x); This function takes the square root of a positive number. You cannot take the square root of a negative number, so you must structure your program to take a square root of a number only after you have checked that it is a positive number.
double fabs(double
x); This
function calculates the absolute value of its argument.
· Your implemented program should comply with the following requirements:
- The variable identifiers for the coefficients of the quadratic equation should be coeffA, coeffB and coeffC and should be of type double within your program.
- The coefficients will be read into your program from the command line. You should prompt for each coefficient using the format shown below. At the end of each prompt line your program should stop and wait for the user to enter the value of the coefficient. There should be a space at the end of each prompt line. There should be no new line at the end of each prompt line.
- Enter the value of the coefficient of the second
order (x squared) term:
- Enter the value of the coefficient of the first
order (x) term:
Enter the value of the constant term:
- Print two blank lines after completing the input of the coefficients
- After your program has read in the values of the coefficients it should print the values of the coefficients to the console. .The message printing out the coefficients should print the values of the coefficients in floating point form with three digits after the decimal point. The message should be identical (except of course for the actual values of the coefficients which are given only as an example) to the message below. Note that there are spaces on either side of each = , the first line ends with a colon, and the second and third lines end with a comma.
- The coefficients of the
quadratic equation are:
The coefficient of the second order term is 1.323,
The coefficient of the first order term is 3.645,
The constant term is 2.152
- print three blank lines after printing the coefficients
- determine the roots of the quadratic equation
- depending on the type of roots found for the quadratic equation print the roots of the quadratic equation in ONE of the following four forms (numbers are samples only not the solution for the example given above`). If there are no roots print the first message. If there is a single root print the second message. If there are two different roots print the third message. If there are two roots with the same value print the third message.
- There are no real
roots.
There is one real root, root1 = 2.00.
There are two real roots, root = -1.000 and root2 = 3.00.
There is one double root, root1 = root2 = 4.00.
- Your program should print results with two decimal places.
- All of the values should be stored in variables of type double.
- Here are some things to keep in mind:
- You'll need to #include <math> to get the standard math library which includes sqrt() function and the fabs () function
- You can't take the square root of a negative number. You must make sure the number is non-negative before you try to take its root.
- Deciding which of these three things to print will require conditional operations. Try to be efficient and use the smallest number of if statements.
- Remember that the program’s user can input any number of the right type as the value of a coefficient, this includes 0. Therefore it is possible that our quadratic equation may be linear (coeffA=0) or even a constant (coeffA and CoeffB both 0). We must also consider these cases in our algorithm design and program implementation.
- Your code should be easy to read and understand.
- Your code should be clearly commented
- You don't have to check the user's input to assure it is the correct type (int, float …). For the moment, you can assume it is. This means that marks will not be deducted if your program fails when character input (not numbers) are entered by the user as values of the coefficients.
- If required by the algorithm you should check that the values input to your program are acceptable values to the program
Be sure to test your program extensively using the test plan you developed earlier
PRACTICE PROBLEM:
Enter your program into the source window, Your program should
a) Prompt the user for, and read in a percentage grade between 0.0 and 100.0
b) Calculate the corresponding letter grade according to the following scale
A 79.5<grade <=100.0
B 64.5<grade<=79.5
C 55.5<grade<=64.5
D 49.5<grade<=55.5
F grade <=49.5
c) When you run your program the output should look like
Enter the student’s percentage grade:
66.5
This student has a numerical grade of 66.5%
This student has earned a B
If you want more practice try some of the end of chapter problems in your textbook.