Lab 3: Arithmetic Expressions & Program I/O

Objectives

The objectives of this lab are to give you practice at using arithmetic expressions to do computations in C. You will also use printf() and scanf() to do program output and input. These programs will give you more practice at finding and correcting errors and begin to write programs yourself from scratch.

Programs

You should begin by creating a new directory called Lab3 in your EE160/Labs directory (which you have created in previous labs). All of the program files you write for this lab should be in this Lab3 directory.

    • (2 Points). Copy your repaired version of the gravity.c file from your Lab2 directory. Modify the program to read the time from the user as a float value and compute the distance and speed of the falling object. (Don't forget to prompt the user for input).
    • (2 Points). Given an integer number of seconds, compute how many hours, minutes (within an hour) and seconds (within a minute) this corresponds to. Again, your program should prompt the user for one number of seconds each time it is run. My code for this program (with comments showing the algorithm) can be found in the file seconds.c. You should fix any bugs so that it compiles and runs. Try some hand examples for this task and make sure your program always produces the correct answer (for example, what should the output be for 3775 seconds?).
    • (2 Points). Compile and link the program cars.c (If the link doesn't work, you can check the~ee160/Labs directory and make a copy) which calculates the total cost of a new Porsche after discount and all taxes. Your job is to modify the program to remove unnecessary parentheses in the arithmetic expressions while maintaining the meaning of the program. One approach is to remove one set of parentheses and see if the output is still the same as the original program. Then remove another set, and so on, until you have just those that are necessary to get the correct answer.
    • (2 Points). Write a program to compute some statistics on the cost of operating the above car each time you fill up with gas. Your program should prompt the user for the number of gallons used and the number of miles since the last fill up. It should print the number of gallons, the cost per gallon ($3.12 - only the best :-)), the cost of the fill-up, cost per mile, and miles per gallon for the fill-up. Use the five step process to develop this program including a hand example to get some sample data, write the algorithm (and use the steps as comments in your code), and then code and test your program. Call the file gas.c
    • (2 Points). Write a program to convert temperatures in degrees Fahrenheit to degrees Celsius. The formula for the conversion is:
        • Subtract 32 from the Fahrenheit temperature
        • Multiply the result by 5/9.


The program should read and process one temperature value when it is run. Call the file temperature.c


What You Turn In

Use the "grade" command to turn in these five programs (gravity.c, seconds.c, cars.c, gas.c and temperature.c). Your command will look like the following.

The format is 'grade -lab#s@,ee160 file1.c file2.c' -

(# = lab number, @ = section number) Make sure to replace # and @ accordingly.

The above command submits file1.c and file2.c


If you are in Section 001 use:

          grade -lab3s1,ee160 gravity.c seconds.c cars.c gas.c temperature.c


If you are in Section 002 use:

          grade -lab3s2,ee160 gravity.c seconds.c cars.c gas.c temperature.c


If you are in Section 003 use:

          grade -lab3s3,ee160 gravity.c seconds.c cars.c gas.c temperature.c


If you are in Section 004 use:

          grade -lab3s4,ee160 gravity.c seconds.c cars.c gas.c temperature.c


You should verify that you turned in things successfully, which you can do with the command (which simply leaves the file names off from the previous command).

          grade -lab3s1,ee160
          
          OR
          
          grade -lab3s2,ee160
          
          OR
          
          grade -lab3s3,ee160