EECS168 09:Lab2

From ITTC
Jump to: navigation, search
Navigation
Home
Information

Syllabus
Schedule
Lecture Notes
Exam Reviews

Classwork

Labs
Homework
Submitting Work

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 fixMe.cpp & to create a new file named fixMe.cpp and open it in gedit. You'll use this file for the next section.

Debugging a Program

Copy the following code into fixMe.cpp:

/* -----------------------------------------------------------------------------
 *
 * Author: Ima Student
 * Date: January 01, 2009
 * EECS-168 Lab 2 assignment.  This program will calculate the area of two 
 * circles and compare them.
 *
 ---------------------------------------------------------------------------- */

#include <iostream>
using namespace std;

int main()
{
    cout << "This program will calculate the area of two circles and compare them." << endl;
    
    // prompt for sizes
    cout << "What is the radius of the first circle: ;
    double r_1;
    cin >> r_1
    cout << "What is the radius of the second circle: ";
    double r_2;
    cin > r2;
    
    // calculate values
    double area_1 = 3.1415 * r_1 * r_1;
    double area_2 = 3.1415 * r_2 * r_2;
    
    // print areas
    cout << "Area of the first circle: " << area1 << endl;
    cout << "Area of the second circle: " << 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;
}
  • Change the name and date at the top of the code appropriately.
  • 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.
  • fixMe.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

A quadratic equation.
The quadratic formula.
The discriminant.

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 solve_quad.cpp. Copy the following code into solve_quad.cpp to get started:

/* -----------------------------------------------------------------------------
 *
 * Author: Ima Student
 * Date: January 01, 2009
 * EECS-168 Lab 2 assignment.  This program will solve quadratic equations.
 *
 ---------------------------------------------------------------------------- */

#include <cmath>
#include <iostream>
using namespace std;

int main()
{  // Insert your code below
    
}

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."

The <cmath> package includes a square root function that you can use called sqrt:

int x = 16;
int y = sqrt(x);
cout << y;

The code above would print "4".

Lab Submission

Submit the following files:

  • The source code file fixMe.cpp, which you have corrected so that it compiles and runs correctly.
  • The source code file solve_quad.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 Smith-123456-Lab-02
  • The email subject should be "[EECS 168] Lab 02". Include your full name and student number in the email.