OpenOffice.org
Writer). See one of us or one of the lab assistants if
you need help using one a word processor.
Please be aware that your lab report will not (in general) contain the same sections as this one. Structure your report logically with sections appropriate to your lab assignment.
The theme the author chose in this particular assignment was the simplicity of the code needed to accomplish a seemingly complex task.
See this must read for detailed suggestions on how to write lab reports in computer science courses.
Chris Cross
MCS-177 Lab 1
February 31, 2008
Counting Real Roots of Quadratics
Introduction
------------
Computing the real roots of a quadratic equation on a computer can be
done using basic high school mathematics techniques. In this lab, we
demonstrate the method by computing the real roots of a quadratic
equation in Scheme. The code is surprisingly simple, even though
most students find the process rather complex when they first learn
it.
Part 1 describes the code, part 2 discusses my testing and lastly
there are some concluding remarks.
Part 1: Number of real roots of a quadratic
-------------------------------------------
In this part of the lab, I designed a procedure to compute how many
real roots a quadratic equation has. Remembering that this can be
determined by computing the discriminant of the equation and then
testing that discriminant, I broke the problem into those two parts:
(define num-real-roots
;; How many real roots does ax^2+bx+c=0 have?
(lambda (a b c)
(num-real-roots-from-discriminant (discriminant a b c))))
The two auxiliary routines used by the above both straightforwardly
reflect my memories from high school:
(define discriminant
(lambda (a b c) ;; a, b and c are coefficients of a polynomial.
(- (square b) (* 4 (* a c)))))
(define num-real-roots-from-discriminant
(lambda (d)
(cond ((< d 0) 0)
((= d 0) 1)
(else 2))))
Part 2: Testing
----------------
The procedure square used above is from the book. A sample call to
square is shown here:
(square 7) ==> 49
I tested the real-roots procedure using one simple example falling
into each of the three cases. Note that I took care to use examples
in which the three arguments were different from one another, to
improve the chances of catching bugs in which, say, variable
b is used where c should have been.
My procedure worked in all three cases, as shown below:
Input Result
--------------- ------
(num-real-roots 1 2 3) 0
(num-real-roots 1 10 2) 2
(num-real-roots 2 16 32) 1
Conclusion
----------
It's a common perception that "useful" programs have to be long.
Determining the number of real roots of a quadratic equation is an
example of an unusual category of computations: those that are complex
enough to be worth automating, yet involve only a fixed amount of
computation. As this lab demonstrated, programs which mirror routine
mathematical calculations are quite straightforward to write.