# CMPT 102 Assignment #2

Due February 3, 2002

## Part 1: Write a program

In part 2 of assignment 1, you described an algorithm to solve quadratic equations. Write a C program that implements the algorithm. Call the program `quad.c`.

You should ask the user to type the coefficients after the program starts to run:

``` Type the coefficients, a b c: ```
The user should then enter something like `1 3 2`.

Your program should print one of these three things (with different numbers, obviously):

``` There are no real roots. There is one real root, 2.00. There are two real roots, -1.00 and 3.00. ```

All of the values should be stored in variables of type `double`. The numbers should be printed with two decimal places.

Here are some things to keep in mind:

• You'll need to `#include <math.h>` to get the `sqrt()` function to take square roots. You also have to add `-lm` to the `gcc` command to get it working properly. Like this: `gcc -lm -o quad quad.c`
• You can't take the square root of a negative number. You must make sure the number is non-negative before you try.
• Your code should be easy to read and understand.
• You don't have to check the user's input. For the moment, you can assume it's correct.

## Part 2: Test it

You have to test your program to make sure it works. Actually, you should be testing as you write the program, but all you have to show us is the testing of the finished program.

Get your program from Part 1 working. Then,

```script quad.out
./quad  (type coefficients with no roots)
./quad  (type coefficients with one root)
./quad  (type coefficients with two roots)
exit
```

## Part 3: Another Program

This program will implement a guessing game. The user will enter a number and then another user will try to guess it. Here is a sample run of the program:

``` 1: userid_fruit% ./guess Enter a number: 102 (screen clears) Take a guess: 50 Sorry, the number was higher than that. Take a guess: 150 Sorry, the number was lower than that. Take a guess: 102 That's it! ```

Call your program `guess.c` . Some notes:

• This statement should clear the screen:
`printf("\033[2J\033[0;0f");`

If it doesn't work, don't worry about it (the game won't be as fun, but life will go on). Don't worry, you shouldn't really understand it. [I don't either -Greg]

• The game should quit when a correct guess is made.
• Use integers. Don't make them guess 5.234234.
• Again, assume they make valid input every time. We'll worry about checking it later.
• Don't forget to test your program.

## Submitting

You have to use the Submission server to submit your work. You should submit the files `quad.c`, `quad.out` and `guess.c`. You can do this by typing these commands:

``` tar cvf a2.tar quad.c quad.out guess.c gzip a2.tar ```
Then, submit the file `a2.tar.gz`.