# Homework 3: Expressions

## Objectives

More practice using arithmetic expressions with printf().

## Assignment

We continue to write programs to help middle schoolers do their math homework. This time we'll help them with the quadratic formula.

In algebra, a quadratic equation is usually written in the form:

y = ax2 + bx + c

Students are often asked to find the roots of the quadratic equation. That is, find the values of x that make y zero. These roots (if they exist) are easily found using the quadratic formula, which you will use in your program.

Your program should query the user for the constants a, b and c then output the values of the two roots. As in Classwork 4, your program should print out the "work" so a middle schooler can just copy the output of your program for his/her homework.

A sample run of your program should look like the following. (The user's responses are in orange.)

```PT[103]% gcc -Wall quad.c

PT[104]% ./a.out
Specify your quadratic equation in the form: y = a * x^2 + b * x + c
Enter a: 1
Enter b: -5
Enter c: 6

The discriminant is:
b^2 - 4 * a * c
=  -5.000000^2 - 4 * 1.000000 * 6.000000
=  25.000000 - 24.000000
=  1.000000

root1  = (-b + sqrt( discriminant ) ) / (2 * a)
= (- -5.000000 + sqrt( 1.000000 ) ) / (2 * 1.000000)
= (- -5.000000 + 1.000000) / 2.000000
= 6.000000 / 2.000000
= 3.000000

root2  = (-b - sqrt( discriminant ) ) / (2 * a)
= (- -5.000000 - sqrt( 1.000000 ) ) / (2 * 1.000000)
= (- -5.000000 - 1.000000) / 2.000000
= 4.000000 / 2.000000
= 2.000000

Let's check our answer with  x = root1 = 3.000000:
y = a * x^2  + b * x + c
= 1.000000 * 3.000000^2  + -5.000000 * 3.000000 + 6.000000
= 1.000000 * 9.000000  + -5.000000 * 3.000000 + 6.000000
= 9.000000  + -15.000000 + 6.000000
= 0.000000

Let's check our answer with  x = root2 = 2.000000:
y = a * x^2  + b * x + c
= 1.000000 * 2.000000^2  + -5.000000 * 2.000000 + 6.000000
= 1.000000 * 4.000000  + -5.000000 * 2.000000 + 6.000000
= 4.000000  + -10.000000 + 6.000000
= 0.000000
PT[105]%

```

## Notes

2. A good starting point for your program is your program from Classwork 4.

3. Remember that you must declare a variable before you use it. For this program, all of your variables should have type double.

4. To square the variable b, just use b * b.

5. You will need to use the sqrt() function from the math library. Here's an example shown in class: math.c. (Basically, just use sqrt( disc ) when you want to calculate the discriminant, assuming you have the discriminant stored in a double variable called disc).

6. Remember that when you use functions from the math library, you need to have:

```   #include <math.h>
```

at the top of your program. You will also need to use the -lm option when you compile (that's the letter l and not the digit 1).

```   gcc -Wall quad.c -lm
```

7. If the quadratic equation specified by the user does not have any roots, your program will print out nan when you try to take the square root of a negative discriminant. Here nan means "not a number". Ideally, your program should print out something more helpful like

```   The discriminant is negative, your quadratic equation has no roots.
```

You will see how to do that when we cover if statements. For now, printing out nan is fine.

8. Implement your program in steps. So, you always have a working version that does something. For example, you can compile, run and debug your program after each of the following steps:

• First make sure that your program can correctly get input from the user. Have your program print out the variables for a, b and c even though the assignment doesn't ask for this.
• Calculate the discriminant. Store it in a variable. Print out the value of the discriminant. Check that it is correct.
• Print out the work for the discriminant.
• Calculate root1. Check that you have the right value.
• Print out the work for root1.
• Calculate and print out root2.

This is a sizable program, but none of the steps are very complicated. If you type in the entire program in one sitting and try to compile it, you might get dozens of warnings and/or error messages and have a tough time finding your bugs. If you compile and debug after implementing a small part of the program, you know that the bug is in the few lines that you just typed in.

9. There is a lot of repetition in the program. Learn how to use cut and paste in nano. Here's a Beginner's Guide to Nano from How-To Geek. Basically:
• use ctrl-shift-^ to "mark" a place in your program
• use arrow keys to select the lines to copy
• use ctrl-K to cut the selected text
• immediately type ctrl-U to put the text back
• use arrow keys to move the insertion point where you want
• use ctrl-U to paste in a copy of the previously selected text

## What to submit

Use the script command to record yourself compiling your program and running your program 3 times using different data. Use exit to terminate the recording. Only record yourself compiling and running your program. DO NOT record yourself editing your program. If you mistakenly start up nano while running script, just exit from script and start over. (The new typescript file will overwrite the old one.)

When you are done. Use:

```
cat typescript

```

to check the contents of your typescript file. Make sure it does not include you editing your program. Then submit your program and typescript file:

```