CPE 101 - Spring 2011 - Lab #2

Due:  End of lab Thursday (4/7/11)

For this lab you will be writing a program that calculates mileage reimbursement for a salesperson at a specified rate.  Your program should interact with the user in the following manner:

MILEAGE REIMBURSEMENT CALCULATOR

Enter beginning odometer reading=>  13505.2

Enter ending odometer reading=>  13810.6

Enter reimbursement rate=>  .35

You traveled 305.4 miles.  At $0.35 per mile,

your reimbursement is $106.89.

Your source code file should be called mileage.c

Using makefiles

You can compile your code at the vogon command line using the command:

        gcc -ansi -pedantic -Wall -Werror mileage.c

After you run this command, you will have a binary file named a.out which you can run.  Run the binary by typing:

        ./a.out

You can also simplify your compilation by using a makefile.  First, download the sample makefile.  Once you place this file in your lab directory, you can compile your code just by typing:

        make

NOTE:  If editing the second line in the makefile (after the line default), you must insert a Tab character before the gcc command. You can do this in vi by entering Ctrl-V, and then entering the Tab key.

Input/Output Redirection

In this lab, you should also test your program with input and output redirection.  Create an input file named sample_input.  This file should contain the following 3 lines:

        13505.2

        13810.6

        .35

Input redirection allows you to send input from a file instead of reading it from the keyboard.  You can test your program with the following command:

        ./a.out < sample_input

You will notice that when using input redirection, the input from the file will not be shown to the screen.

To save the output of your program to a file, use output redirection:

        ./a.out < sample_input > sample_output

The previous command will read input from the file sample_input and save the output to a file name sample_output.  You can look at the contents of program_output with the following command:

        more sample_output

Using diff

The diff program compares 2 files and prints out any differences between the files.  This is useful for comparing your program output with the expected output.  For example, the command:

        diff -w -B my_output ref_output

If the 2 files are exactly the same, diff will print out nothing to the screen.  If the files are different, diff will print out the differences.  For those interested, here is a how to read diff output.

Testing your lab

Here are 2 input and output files to test your lab:

        test_input1        test_output1

        test_input2        test_output2

Handin Instructions

Your submission should have the following header at the top:

        /*

         * Name:  John Doe

         * Assignment:  Lab 2

         * Instructor:  

         *

         * Short description of your program......

*/

Demo running your program with test_input2 to your instructor.

Hand in the following files:

        mileage.c

        makefile

        handin djanzen 101_lab2 mileage.c makefile


Complete this survey.