Docsity

Laboratory Exercise 4: Problem Solving with a Computer | CSC 145, Lab Reports of Computer Science

Toggle sidebar
bg1
CSC 145 - Laboratory Exercise 4: Problem Solving with a Computer
The development of a computer program to solve a problem can be divided into a number of
clearly defined tasks. In this exercise you will be guided through these tasks as you develop a
computer program to find the roots of a quadratic equation.
Preparation: This activity is to be completed before entering the laboratory. Use the textbook
and lecture notes to answer the questions below.
The quadratic formula gives the value(s) of x which solve expressions of the form
ax bx c
2
0
1
where a,b and c are constants. The quadratic formula is given by,
xb b ac
a
2
4
2
2
When the radical (square-root term) is zero, there is one real value of x (called the root) which
will solve the quadratic equation. When the radical is positive there are two real roots and when
the radical is negative there are no real roots for the quadratic expression.
Use an example to show that expression 2 gives the root(s) to a quadratic equation.
Example quadratic equation
a = ___________________ b = _____________________ c = ___________________
Solve for x
xb b ac
a
2
4
2
xb b ac
a
2
4
2
Develop an Ada program that accepts the values of the three coefficients a, b and c of a
quadratic equation of the form given by Eq.1. Your program should compute the root(s) of this
expression, if any, and display them with an appropriate message.
pf3
pf4

Partial preview of the text

Download Laboratory Exercise 4: Problem Solving with a Computer | CSC 145 and more Lab Reports Computer Science in PDF only on Docsity!

CSC 145 - Laboratory Exercise 4: Problem Solving with a Computer The development of a computer program to solve a problem can be divided into a number of clearly defined tasks. In this exercise you will be guided through these tasks as you develop a computer program to find the roots of a quadratic equation. Preparation : This activity is to be completed before entering the laboratory. Use the textbook and lecture notes to answer the questions below. The quadratic formula gives the value(s) of x which solve expressions of the form ax bx c 2    0 1 where a,b and c are constants. The quadratic formula is given by, x b b ac a    2  4 2

When the radical (square-root term) is zero, there is one real value of x (called the root) which will solve the quadratic equation. When the radical is positive there are two real roots and when the radical is negative there are no real roots for the quadratic expression. Use an example to show that expression 2 gives the root(s) to a quadratic equation. Example quadratic equation a = ___________________ b = _____________________ c = ___________________ Solve for x x b b ac a    2  4 2 x b b ac a    2  4 2 Develop an Ada program that accepts the values of the three coefficients a, b and c of a quadratic equation of the form given by Eq.1. Your program should compute the root(s) of this expression, if any, and display them with an appropriate message.

Define the Problem - The first step in program development is to gain a clear understanding of the problem being solved.

  1. The root(s) of a quadratic expression are used to factor the expression into two linear terms as shown in the example below. Example: The expression (^) x^2  x  2  0 has roots x=1 and x=-2. These roots can also be written as x-1=0 and x+2=0. These roots are the factors of this equation,

 x  1   x  2   0

If the factors are the same there is only one root, and if the expression cannot be factored there are no real roots. Generate three test cases (sample quadratic equations) for your program which will illustrate each of the three possible cases. Two Roots One Root No Real Roots Design an Algorithm - The next step in software development is the functional decomposition of the problem. In this case it is sufficient to define the input , process and output operations.

  1. Make a list of the input parameters needed for your program. That is, what data will be entered by the user?
  2. Since the value of the radical (the term under the square-root) will determine the number of solutions, you will want to calculate it separately. Give an Ada assignment statement to compute this term.
  3. If the radical is negative your program should print the statement "There are no real roots for this quadratic expression". If the radical is zero then your program should compute it and print it in a statement such as "There is one real root x = " and then print the solution. Finally, when the radical is positive your program should compute both roots and print them using an appropriate statement. Write code for this control structure. (You may use a case , if_then or if_then_elsif statement to control your program's operations.)
  1. Once all the syntax and run-time errors are eliminated, your executable program must be correct. Agree or disagree with this statement and explain your answer.



  1. Submit a printed copy of your corrected source code with this assignment. Verification and Validation - In more complex programming tasks, you would be required to develop verification test sets for each functional module of your program. In this example, you have a set of validation tests to demonstrate the overall correctness of your code. Exercise these tests now and record the results below: Test Case 1: Test Case 2: Test Case 3: Analysis : This activity is to be completed after the laboratory exercise. All laboratory worksheets are due at the beginning of the next class meeting.
  2. Do you believe that your program is correct? ______________________________________
  3. Other than running more test cases, how might you ensure the correctness of your... a. algorithm?___________________________________________________________________

b. executable program? __________________________________________________________