7

Spring 2008

Introduction to C - Programming Assignment #6

Due date: Please consult WebCT for your section

Objectives

1. Learn how to write functions that take in files and arrays as parameters.

2. Learn how to use a struct.

Note: In this assignment, you are required to solve two separate problems. The first problem will simply be to write a function. The latter will be to write a small program.

Problem A: Functions for Graphical Fuel Efficiency Display

You must encapsulate the first part of your solution to program #5 in a function for this portion of the assignment. In particular, you must write a function with the following header:

// Pre-condition: fin is a file pointer pointing to the

// beginning of a file with fuel efficiency

// data in the format of the file from

// program #5. mpg is an uninitialized

// array of size 6.

// Post-condition: mpg stores the fuel efficiency for the

// car described in the file pointed to by

// fin for each five minute interval. If

// there is no data for an interval, 0

// should be stored in the appropriate

// array location.

void fillMPG(FILE *fin, double mpg[]);

For this part of the assignment, in order to properly test that your function works, rewrite your program #5 to utilize this function.

Problem A Optional Work

One of the COP 3223 TAs, Danzhou Liu, has written some code in Python that will allow you to use the function above, as well as a function with the following prototype (similar to program #4):

// Pre-condition: timetowork represents the number of

// minutes it takes to get to work,

// startdrive represents the number of

// minutes before work you start to drive,

// rememberitem represents the number of

// minutes INTO the drive you remember that

// you left an item at home.

// 0<rememberitem<timetowork<=startdrive

// Post-condition: Returns 1 iff you can go back home to

// pick up the item and arrive on time to

// work. 0 is returned otherwise.

int getitem(int timetowork, int startdrive, int rememberitem);

to complete a graphical application similar to one that you might see in a car. Directions for how to integrate your code into his code to run this graphical application will be posted on-line.

You won't need to turn this in at all, it's for your own curiosity. I hope that some of you do it, because Danzhou spent a good deal of time learning Python and putting this together for you.


Problem B: Car Struct

In this program, you will keep track of some standard information about a car "object" using a struct. The struct you will use is the following:

struct car {

char name[20];

int mileage;

int milesOil;

double gasLeft;

};

name stores the name of the car, mileage stores the total mileage of the car, milesOil stores the number of miles driven since the last oil change, and gasLeft indiciates the amount of gas left in the gas tank, in gallons.

You will also use the following constants:

#define GASTANK_CAPACITY 11

#define MILES_PER_GALLON 47

#define NEED_OIL_CHANGE 5000

Your program will ask the user to give a name to their new car. At this point, the car will be brand new, with no miles on it and a full tank of gas. From there, you will present the user with the following menu:

1) Drive

2) Get Gas

3) Change Oil

4) Print Car Status

5) Quit

For option #1, you will ask the user how far they wish to drive. If they have enough gas to drive that distance, adjust their gas, mileage and the number of miles they've driven without an oil change. If they run out of gas, or go over 5,000 miles without an oil change with the drive, then print out the following message:

You have NOT properly maintained your car.

You lose your driving privileges.

and immediately end the program.

For option two, ask the user how many gallons of gas they would like to put in their car. If the answer is beyond the capacity of the tank, simply fill up the tank and print out a message to this effect.

For option three, simply reset the number of miles since an oil change to 0.

For option four, print out the name of the car, its mileage, the amount of gas in gallons left in the tank, and the number of miles since the last oil change.

Implementation Restrictions

1)You must write a function to execute part of options 1, 2 and 3.

2) You must use the struct and constants described above.

3) You must have a function that initializes a struct car variable.

4) You must create exactly one struct car variable in main.

5) Your functions take in a struct car as a reference parameter.

Sample Run

Welcome to the car simulator!

What would you like to name your new car?

OldFaithful

Great, here is what you can do now:

1) Drive

2) Get Gas

3) Change Oil

4) Print Car Status

5) Quit

What would you like?

1

How far would you like to drive, in miles?

500

1) Drive

2) Get Gas

3) Change Oil

4) Print Car Status

5) Quit

What would you like?

2

How many gallons of gas would you like?

15

You don't have that much space in your gas tank, but it's been filled up.

1) Drive

2) Get Gas

3) Change Oil

4) Print Car Status

5) Quit

What would you like?

1

How far would you like to drive, in miles?

470

1) Drive

2) Get Gas

3) Change Oil

4) Print Car Status

5) Quit

What would you like?

4

Name: OldFaithful

Mileage: 970

Miles since Last Oil Change: 970

Gas Left: 1.0 gallon(s)

1) Drive

2) Get Gas

3) Change Oil

4) Print Car Status

5) Quit

What would you like?

3

Great, your oil has been changed.

1) Drive

2) Get Gas

3) Change Oil

4) Print Car Status

5) Quit

What would you like?

4

Name: OldFaithful

Mileage: 970

Miles since Last Oil Change: 0

Gas Left: 1.0 gallon(s)

1) Drive

2) Get Gas

3) Change Oil

4) Print Car Status

5) Quit

What would you like?

1

How far would you like to drive, in miles?

235

You have NOT properly maintained your car.

You lose your driving privileges.


References

Textbook: Chapter 9, 12 Notes: Lectures 17, 18, 19, 23, 24

Deliverables

Two source files:

1) fuelgraph2.c, for your solution to program A

2) drive.c, for your solution to program B.

These files are to be submitted over WebCT.

Restrictions

Although you may use other compilers, your program must compile and run using gcc or Dev C++. Please use either your Olympus account or Dev C++ to develop your programs. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure you include comments throughout your code describing the major steps in solving the problem.

Grading Details

Your program will be graded upon the following criteria:

1) Your correctness.

2) Using a reasonable program design with logical functions.

3) Your programming style and use of white space. Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor, you could get 10% or 15% deducted from your grade.

4) Compatibility to either gcc (in Olympus) or Dev C++ (in Windows). If your program does not compile in either of these environments, you will get a sizable deduction from your grade.