EECS169:Lab2
| Navigation |
|---|
| Home |
| Information |
| Classwork |
Objectives
- Troubleshoot a program with syntax and logic errors.
- Write a simple program that solves quadratic equations.
Getting Started
- Log in and start a terminal window.
- Create a directory called lab02 (using the mkdir command). This directory will contain all the files you make for this lab. For the rest of the semester, you should continue to make directories (one for each lab, one for each homework) to keep things organized.
- Type cd lab02 to go into the new directory, then type gedit correctMe.cpp & to create a new file named correctMe.cpp and open it in gedit. You'll use this file for the next section.
Debugging a Program
Copy the following code into correctMe.cpp:
#include <iostream>
using namespace std;
int main()
{
cout << "This program will calculate and compare the area of two circles." << endl;
// prompt for sizes
cout << "What is the radius of the first circle: ;
double radius_1;
cin >> radius_1
cout << "What is the radius of the second circle: ";
double radius_2;
cin > rdius2;
// calculate values
double area_1 = 3.1415 * radius_1 * radius_1;
double area_2 = 3.1415 * radius_2 * radius_2;
// print areas
cout << "The first circle's area: " << area1 << endl;
cout << "The second circle's area: " << area2 << endl;
// determine which is smaller
if (area_1 == area_2)
{
cout << "The circles are the same size." << endl;
}
else if (area_1 > area_2)
{
cout << "The first circle is smaller." << endl
}
else
{
cout << "The second circle is smaller." << endl;
}
return 0;
}
- Compile this program. The compilation process will fail; find and correct the issues reported by g++ until the program compiles successfully. These errors, where code has been typed in incorrectly, are called syntax errors, and are usually the easiest type of error to find.
- correctMe.cpp has another error in addition to the syntax errors. However, this error won't prevent it from compiling. When you run the program, it doesn't do what you think it should. This kind of error is called a logic error; it occurs when the programmer tells the computer to do the wrong thing. Look through the code, find the logic error, and correct it.
Quadratic Equation Solver
Next, you will write a program that uses the quadratic formula to find the roots of a quadratic equation. This program should:
- Read in values for the equation coefficients.
- Determine whether there are two, one, or no real roots.
- Calculate and print the root values.
Quadratic Equations
The general form of a quadratic equation is shown on the right. Given values of a, b, and c, the roots of the equation are values of x that satisfy the equation.
Use the quadratic formula to calculate the roots of an equation. Plugging in a, b, and c into the quadratic formula will result in zero, one, or two roots.
The discriminant is the part of the quadratic equation under the square root symbol. The discriminant tells you how many roots to expect:
- If the discriminant evaluates to zero, there is one real root.
- If the discriminant evaluates to a number greater than zero, there are two roots.
- If the discriminant evaluates to a number less than zero, there are no real roots.
Writing the Quadratic Roots Program
First, create a file named quadraticEq.cpp. Copy the following code into quadraticEq.cpp to get started:
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
// REPLACE THIS WITH YOUR CODE
return 0;
}
Prompt for Input
You need to get the values of a, b, and c from the user. These should be floating point values. Use cin to get values, and cout to print them.
To make sure your program is working correctly so far, you can echo these values back to the user: print out the same values you just read in. Compile your program now and make sure the echo works; this makes sure it is working like you think it should. You can delete or comment out the echo code later.
Calculate the Discriminant
Calculate the discriminant using the equation above.
Again, you should compile your code at this point to make sure it's working like you think it should. Try printing out the discriminant and checking that it is correct.
Branch based on the Discriminant
Your program will need to do different things based on the value of the discriminant. Create an if/else statement based on the value of the discriminant:
- If the discriminant was positive, you need calculate two roots (one for the "plus" root, and one for the "minus).
- If the discriminant was zero, you only need to calculate one root.
- If the discriminant was negative, there are no real roots to calculate.
Solve for the Roots
Inside of each branch of the if/else statement above, the code the calculates the roots and prints them out. If there were no roots (i.e., the discriminant was negative), print a message that says "there exists no real roots."
There is a square root function that you can use called sqrt:
int x = sqrt(9);
cout << x;
The code above would print "3".
Lab Submission
Submit the following files:
- The source code file correctMe.cpp, which you have corrected so that it compiles and runs correctly.
- The source code file quadraticEq.cpp, which should compile and run as described above.
The submission procedure is the same as was described in Lab 1, except that:
- Your tarball and the directory inside it should be named like 123456-Smith-Lab-02
- The email subject should be "[EECS 168] Lab 02". Include your full name and student number in the email.
