Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//program 4 #include <stdio.h> #include <string.h> #define true 1 typedef struct

ID: 3571230 • Letter: #

Question

//program 4

#include <stdio.h>
#include <string.h>

#define true 1

typedef struct {
   int carId; // a unique ID of a car
   char make[20]; // make of the car
   char model[20]; // model of the car
   int numDoors; // 2 or 4 door cars
   double rate; // the rental rate of the car
} CarT;

typedef struct {
char renterName[20]; // name of the renter
   int daysRenting; // days renting the car
   int carId; // ID of the car rented
} RentalT;

// Prints a menu to the screen.
void printMenu();

// Handles finding a reservation. It prompts the user to enter a renter name.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
void handleFindReservation ( CarT *allCars, int totalCars, RentalT *allRentals, int totalRentals );

// Handles printing information about a selected car. It prompts the user for a car Id.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
void handlePrintCarInfo ( CarT *allCars, int totalCars );

// Create a new inventory.
// allCars - a list of cars
// Returns the number of cars in the inventory
int createInventory ( CarT *allCars );

// Adds new car to the inventory.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// size - the size of the list
// Returns the new number of cars in the inventory or -1 if the new
// total number of cars exceeds the size of the array.
int addNewCar ( CarT *allCars, int totalCars, int size );

// Creates a new record in the list of rentals.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
// size - the size of the list
// Returns the new number of rentals or -1 if the new total number
// of rentals exceeds the size of the array.
int addNewRental ( CarT *allCars,
int totalCars,
RentalT *allRentals,
           int totalRentals,
int size );

// Finds a car id by the car name (make).
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// carMake - the make of the car
// Returns the ID of the car in the array that matches the given make
// of the car or -1 if no car matches the given make.
int findCarIDByName ( CarT *allCars,
int totalCars,
char *carMake );

// Find a car reservation among all reservations.
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
// renterName - the name of the renter
// Returns the index of the rental reservation given a renter name.
int findReservation ( RentalT *allRentals,
int totalRentals,
char *renterName );

// Finds a car by a given car id.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// carId - the Id of a car  
// Returns the index of the car in the list matching the given car ID
// or -1 if no car matches the given car ID.
int findCarById ( CarT *allCars,
int totalCars,
int carId );

// Prints all car rental records to the screen.
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
// allCars - a list of cars
// totalCars - the number of cars in the inventory
void printAllRentals (RentalT *allRentals,
int totalRentals,
                   CarT *allCars,
                   int totalCars);

// Prints car informations to the screen.
// allCars - a list of cars
// totalCars - the number of cars in the inventory                 
void printCarInfo ( CarT *allCars,
int totalCars,
int carId );

// Computes average days of car rentals.
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
// Returns the average rental days or -1 if no car has been rented.                  
double getAverageRentalDays (RentalT *allRentals,
int totalRentals );


int main ( void )
{
   CarT allCars[20]; // a list of cars
   RentalT allRentals[60]; // a list of rentals
   int totalCars; // number of all cars owned
   int totalRentals; // number of rented cars
   int choice;

   // add pre-defined list of cars to the list
totalCars = createInventory ( allCars );
   totalRentals = 0;
  
   while ( true ) {
       printMenu();
       printf ("Choose option: ");
       scanf ("%d", &choice);
       printf (" ");
      
       if (choice == 7)
           break;
          
       handleSelection (choice, allCars, &totalCars, allRentals, &totalRentals );
   };

return 0;
}

void printMenu()
{
   printf ("1   Add new car to the inventory. ");
   printf ("2   Make a reservation. ");
   printf ("3   Find a reservation using a renter name and print it to the screen. ");
   printf ("4   Print all rental information to the screen. ");
   printf ("5   Print car information to the screen of selected car. ");
   printf ("6   Calculate and print average number of days rented. ");
   printf ("7   Exit program. ");
}

void handleSelection ( int choice, CarT *allCars, int *totalCars, RentalT *allRentals, int *totalRentals)
{
   double value;
  
   switch ( choice ) {
       case 1 : *totalCars = addNewCar ( allCars, *totalCars, 20 ); break;
       case 2 : *totalRentals = addNewRental ( allCars, *totalCars, allRentals, *totalRentals, 60 ); break;
       case 3 : handleFindReservation ( allCars, *totalCars, allRentals, *totalRentals ); break;
       case 4 : printAllRentals ( allRentals, *totalRentals, allCars, *totalCars ); break;
       case 5 : handlePrintCarInfo ( allCars, *totalCars ); break;
       case 6 : value = getAverageRentalDays ( allRentals, *totalRentals );
       printf ("Average number of days cars are rented is %lf. ", value ); break;
       default : printf ("Invalid entry.");
      
   }
   printf (" ");
}

void handleFindReservation ( CarT *allCars, int totalCars, RentalT *allRentals, int totalRentals )
{
   int index;
   char renterName[20];
  
   do {
       printf ("Enter renter name: ");
       scanf ("%s", renterName);
      
       index = findReservation ( allRentals, totalRentals, renterName );
   } while (index == -1);
  
   printf ("Renter: %s ", allRentals[index].renterName);
   printf ("Days renting: %d ", allRentals[index].daysRenting);
   printCarInfo ( allCars, totalCars, allRentals[index].carId );
}

void handlePrintCarInfo ( CarT *allCars, int totalCars )
{
   int carId;
  
   printf ("Enter the car ID: ");
   scanf ("%d", &carId);
  
   printCarInfo ( allCars, totalCars, carId );
}

int createInventory ( CarT *allCars )
{
   allCars[0].carId = 1234;
   allCars[0].make[0] = 'V';
   allCars[0].make[1] = 'W';
   allCars[0].make[2] = '';
   allCars[0].model[0] = 'G';
   allCars[0].model[1] = 'o';
   allCars[0].model[2] = 'l';
   allCars[0].model[3] = 'f';
   allCars[0].model[4] = '';
   allCars[0].numDoors = 2;
   allCars[0].rate = 66.0f;
  
   allCars[1].carId = 2241;
   allCars[1].make[0] = 'F';
   allCars[1].make[1] = 'o';
   allCars[1].make[2] = 'r';
   allCars[1].make[3] = 'd';
   allCars[1].make[4] = '';
   allCars[1].model[0] = 'F';
   allCars[1].model[1] = 'o';
   allCars[1].model[2] = 'c';
   allCars[1].model[3] = 'u';
   allCars[1].model[4] = 's';
   allCars[1].model[5] = '';
   allCars[1].numDoors = 4;
   allCars[1].rate = 45.0f;
  
   allCars[2].carId = 3445;
   allCars[2].make[0] = 'B';
   allCars[2].make[1] = 'M';
   allCars[2].make[2] = 'W';
   allCars[2].make[3] = '';
   allCars[2].model[0] = 'X';
   allCars[2].model[1] = '3';
   allCars[2].model[2] = '';
   allCars[2].numDoors = 4;
   allCars[2].rate = 128.0f;
  
   return 3;
}

int addNewCar ( CarT *allCars, int totalCars, int size )
{
   if (totalCars == 20)
       return 20;
      
   printf ("Enter car ID: ");
   scanf ("%d", &allCars[totalCars].carId);
   printf ("Enter make: ");
   scanf ("%s", allCars[totalCars].make);
   printf ("Enter model: ");
   scanf ("%s", allCars[totalCars].model);
   printf ("Enter number of doors (2 or 4): ");
   scanf ("%d", &allCars[totalCars].numDoors);
   printf ("Enter car rate: ");
   scanf ("%lf", &allCars[totalCars].rate);
  
   return totalCars + 1;
}

int addNewRental ( CarT *allCars,
int totalCars,
RentalT *allRentals,
           int totalRentals,
int size )
{
char name[20];
   int carId;
  
   printf ("Enter renter name: ");
   scanf ("%s", allRentals[totalRentals].renterName);
   printf ("Enter number of days of rental: ");
   scanf ("%d", &allRentals[totalRentals].daysRenting);
  
   do {
       printf ("Enter car name (model): ");
       scanf ("%s", name);
       carId = findCarIDByName ( allCars, totalCars, name );
   } while (carId == -1);
  
   allRentals[totalRentals].carId = carId;
  
   return totalRentals + 1;
}

int findReservation ( RentalT *allRentals,
int totalRentals,
char *renterName )
{
   int i;
  
   for (i=0; i < totalRentals; i++ )
       if ( strcmp ( allRentals[i].renterName, renterName ) == 0 )
           return i;
  
   return -1;
}

int findCarIDByName ( CarT *allCars,
int totalCars,
char *carName )
{
   int i;
  
   for (i=0; i < totalCars; i++ )
       if ( strcmp ( allCars[i].make, carName ) == 0 )
           return allCars[i].carId;
  
   return -1;
}

int findCarById ( CarT *allCars,
int totalCars,
int carId )
{
   int i;
  
   for (i=0; i < totalCars; i++ )
       if ( allCars[i].carId == carId )
           return i;
  
   return -1;
}

void printAllRentals (RentalT *allRentals,
int totalRentals,
                   CarT *allCars,
                   int totalCars)
{
   int i;
  
   for (i=0; i < totalRentals; i++ ) {
       printf ("------------------------------------------------------------------------- ");
       printf ("Renter: %s ", allRentals[i].renterName);
       printf ("Days renting: %d ", allRentals[i].daysRenting);
       printCarInfo ( allCars, totalCars, allRentals[i].carId );
       printf ("------------------------------------------------------------------------- ");
       printf (" ");
   }
}
             
void printCarInfo ( CarT *allCars,
int totalCars,
int carId )
{
   int i;
  
   for (i=0; i < totalCars; i++ ) {
       if ( allCars[i].carId == carId ) {
           printf ("Make: %s ", allCars[i].make);
           printf ("Model: %s ", allCars[i].model);
           printf ("Number of doors: %d ", allCars[i].numDoors);
           printf ("Rental rate: %lf ", allCars[i].rate);
           return;
       }
   }
   printf ("Car info for car %d not found. ", carId);
}
                  
double getAverageRentalDays (RentalT *allRentals,
int totalRentals )
{
   int i;
   double sum = 0.0;
  
   for (i=0; i < totalRentals; i++ ) {
       sum += allRentals[i].daysRenting;
   }
  
   return sum / totalRentals;
}

Extend the program from project 4 to make the data in your program persistent by storing all car and rental records into separate files and using the infomation when the program starts up. The program must implement four new functions: void Wr Rentals char filename RentalT *allRentals, rite int totalRentals int readRentals (char *filename, RenatalT *allRentals); void writ eCars (Char filename, CarT *allCars, int total Cars int readCars char filename CarT all Cars Use the command line arguments to read two text strings from the command line when the program is started. The first string must specify the file name of the rental records file, the second string specifies the file name of the car records file. The program must call the two read functions. Note, the previous call of createInventory0 in main is no longer needed as the car inventory is read from a file. Before the program terminates, it must store the rental and car records using the sponding functions. For implementing the functions, you must use text-based file IO. When you create the file to write rental or car records to the file, begin by writing the entire record on a single line with each field separated by one space using fprintf then continue with the next record the same way until all records have been written to the file. You should have as many text lines as records. For example, the output format for the 3 cars specified in the project description of project 4 should look as follows: 1234 VW Golf 00 2241 Ford Focus 4 45 00 3445 BMW X3 4 128 00 You may assume that none of the text strings for make or model contains spaces in their names. For reading the records, do the same but use fscan f0 to read numbers or strings for the individual fields of records. Assume that each record is completely stored in the file. If you reach the end of a file, then you should have a complete m of records that have umber been successfully read. Retum that number as the number of rentals or cars. Your program must be robust to handle empty text files with no records. (0 points0

Explanation / Answer

Here are the 4 functions extended for you. I only declared and defined the functions, but didn't called from main(). Please check with that.

//program 4
#include <stdio.h>
#include <string.h>
#define true 1
typedef struct {
int carId; // a unique ID of a car
char make[20]; // make of the car
char model[20]; // model of the car
int numDoors; // 2 or 4 door cars
double rate; // the rental rate of the car
} CarT;
typedef struct {
char renterName[20]; // name of the renter
int daysRenting; // days renting the car
int carId; // ID of the car rented
} RentalT;
// Prints a menu to the screen.
void printMenu();
void writeRentals(char *filename, RentalT *allRentals, int totalRentals);
int readRentals(char *filename, RentalT *allRentals);
void writeCars(char *filename, CarT *allCars, int totalCars);
int readCars(char *filename, CarT *allCars);
void handleSelection ( int choice, CarT *allCars, int *totalCars, RentalT *allRentals, int *totalRentals);
// Handles finding a reservation. It prompts the user to enter a renter name.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
void handleFindReservation ( CarT *allCars, int totalCars, RentalT *allRentals, int totalRentals );
// Handles printing information about a selected car. It prompts the user for a car Id.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
void handlePrintCarInfo ( CarT *allCars, int totalCars );
// Create a new inventory.
// allCars - a list of cars
// Returns the number of cars in the inventory
int createInventory ( CarT *allCars );
// Adds new car to the inventory.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// size - the size of the list
// Returns the new number of cars in the inventory or -1 if the new
// total number of cars exceeds the size of the array.
int addNewCar ( CarT *allCars, int totalCars, int size );
// Creates a new record in the list of rentals.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
// size - the size of the list
// Returns the new number of rentals or -1 if the new total number
// of rentals exceeds the size of the array.
int addNewRental ( CarT *allCars,
int totalCars,
RentalT *allRentals,
int totalRentals,
int size );
// Finds a car id by the car name (make).
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// carMake - the make of the car
// Returns the ID of the car in the array that matches the given make
// of the car or -1 if no car matches the given make.
int findCarIDByName ( CarT *allCars,
int totalCars,
char *carMake );
// Find a car reservation among all reservations.
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
// renterName - the name of the renter
// Returns the index of the rental reservation given a renter name.
int findReservation ( RentalT *allRentals,
int totalRentals,
char *renterName );
// Finds a car by a given car id.
// allCars - a list of cars
// totalCars - the number of cars in the inventory
// carId - the Id of a car
// Returns the index of the car in the list matching the given car ID
// or -1 if no car matches the given car ID.
int findCarById ( CarT *allCars,
int totalCars,
int carId );
// Prints all car rental records to the screen.
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
// allCars - a list of cars
// totalCars - the number of cars in the inventory
void printAllRentals (RentalT *allRentals,
int totalRentals,
CarT *allCars,
int totalCars);
// Prints car informations to the screen.
// allCars - a list of cars
// totalCars - the number of cars in the inventory   
void printCarInfo ( CarT *allCars,
int totalCars,
int carId );
// Computes average days of car rentals.
// allRentals - a list of car rentals
// totalRentals - the number of car rentals
// Returns the average rental days or -1 if no car has been rented.
double getAverageRentalDays (RentalT *allRentals,
int totalRentals );

int main ( void )
{
CarT allCars[20]; // a list of cars
RentalT allRentals[60]; // a list of rentals
int totalCars; // number of all cars owned
int totalRentals; // number of rented cars
int choice;

// add pre-defined list of cars to the list
totalCars = createInventory ( allCars );
totalRentals = 0;
  
while ( true ) {
printMenu();
printf ("Choose option: ");
scanf ("%d", &choice);
printf (" ");
  
if (choice == 7)
break;
  
handleSelection (choice, allCars, &totalCars, allRentals, &totalRentals );
};

return 0;
}

void printMenu()
{
printf ("1 Add new car to the inventory. ");
printf ("2 Make a reservation. ");
printf ("3 Find a reservation using a renter name and print it to the screen. ");
printf ("4 Print all rental information to the screen. ");
printf ("5 Print car information to the screen of selected car. ");
printf ("6 Calculate and print average number of days rented. ");
printf ("7 Exit program. ");
}
void writeRentals(char *filename, RentalT *allRentals, int totalRentals)
{
FILE *fp = fopen(filename, "w");
for(int i = 0; i < totalRentals; i++)
fprintf(fp, "%s %i %i ", allRentals[i].renterName, allRentals[i].daysRenting, allRentals[i].carId);
}
int readRentals(char *filename, RentalT *allRentals)
{
int i = 0;
FILE *fp = fopen(filename, "r");
while(!feof(fp))
{
fscanf(fp, "%s%i%i", allRentals[i].renterName, &allRentals[i].daysRenting, &allRentals[i].carId);
i++;
}
return i;
}
void writeCars(char *filename, CarT *allCars, int totalCars)
{
FILE *fp = fopen(filename, "w");
for(int i = 0; i < totalCars; i++)
fprintf(fp, "%i %s %s %i %.2lf ", allCars[i].carId, allCars[i].make, allCars[i].model, allCars[i].numDoors, allCars[i].rate);
}
int readCars(char *filename, CarT *allCars)
{
int i = 0;
FILE *fp = fopen(filename, "r");
while(!feof(fp))
{
fscanf(fp, "%i%s%s%i%lf", &allCars[i].carId, allCars[i].make, allCars[i].model, &allCars[i].numDoors, &allCars[i].rate);
i++;
}
return i;
}
void handleSelection ( int choice, CarT *allCars, int *totalCars, RentalT *allRentals, int *totalRentals)
{
double value;
  
switch ( choice ) {
case 1 : *totalCars = addNewCar ( allCars, *totalCars, 20 ); break;
case 2 : *totalRentals = addNewRental ( allCars, *totalCars, allRentals, *totalRentals, 60 ); break;
case 3 : handleFindReservation ( allCars, *totalCars, allRentals, *totalRentals ); break;
case 4 : printAllRentals ( allRentals, *totalRentals, allCars, *totalCars ); break;
case 5 : handlePrintCarInfo ( allCars, *totalCars ); break;
case 6 : value = getAverageRentalDays ( allRentals, *totalRentals );
printf ("Average number of days cars are rented is %lf. ", value ); break;
default : printf ("Invalid entry.");
  
}
printf (" ");
}
void handleFindReservation ( CarT *allCars, int totalCars, RentalT *allRentals, int totalRentals )
{
int index;
char renterName[20];
  
do {
printf ("Enter renter name: ");
scanf ("%s", renterName);
  
index = findReservation ( allRentals, totalRentals, renterName );
} while (index == -1);
  
printf ("Renter: %s ", allRentals[index].renterName);
printf ("Days renting: %d ", allRentals[index].daysRenting);
printCarInfo ( allCars, totalCars, allRentals[index].carId );
}
void handlePrintCarInfo ( CarT *allCars, int totalCars )
{
int carId;
  
printf ("Enter the car ID: ");
scanf ("%d", &carId);
  
printCarInfo ( allCars, totalCars, carId );
}
int createInventory ( CarT *allCars )
{
allCars[0].carId = 1234;
allCars[0].make[0] = 'V';
allCars[0].make[1] = 'W';
allCars[0].make[2] = '';
allCars[0].model[0] = 'G';
allCars[0].model[1] = 'o';
allCars[0].model[2] = 'l';
allCars[0].model[3] = 'f';
allCars[0].model[4] = '';
allCars[0].numDoors = 2;
allCars[0].rate = 66.0f;
  
allCars[1].carId = 2241;
allCars[1].make[0] = 'F';
allCars[1].make[1] = 'o';
allCars[1].make[2] = 'r';
allCars[1].make[3] = 'd';
allCars[1].make[4] = '';
allCars[1].model[0] = 'F';
allCars[1].model[1] = 'o';
allCars[1].model[2] = 'c';
allCars[1].model[3] = 'u';
allCars[1].model[4] = 's';
allCars[1].model[5] = '';
allCars[1].numDoors = 4;
allCars[1].rate = 45.0f;
  
allCars[2].carId = 3445;
allCars[2].make[0] = 'B';
allCars[2].make[1] = 'M';
allCars[2].make[2] = 'W';
allCars[2].make[3] = '';
allCars[2].model[0] = 'X';
allCars[2].model[1] = '3';
allCars[2].model[2] = '';
allCars[2].numDoors = 4;
allCars[2].rate = 128.0f;
  
return 3;
}
int addNewCar ( CarT *allCars, int totalCars, int size )
{
if (totalCars == 20)
return 20;
  
printf ("Enter car ID: ");
scanf ("%d", &allCars[totalCars].carId);
printf ("Enter make: ");
scanf ("%s", allCars[totalCars].make);
printf ("Enter model: ");
scanf ("%s", allCars[totalCars].model);
printf ("Enter number of doors (2 or 4): ");
scanf ("%d", &allCars[totalCars].numDoors);
printf ("Enter car rate: ");
scanf ("%lf", &allCars[totalCars].rate);
  
return totalCars + 1;
}
int addNewRental ( CarT *allCars,
int totalCars,
RentalT *allRentals,
int totalRentals,
int size )
{
char name[20];
int carId;
  
printf ("Enter renter name: ");
scanf ("%s", allRentals[totalRentals].renterName);
printf ("Enter number of days of rental: ");
scanf ("%d", &allRentals[totalRentals].daysRenting);
  
do {
printf ("Enter car name (model): ");
scanf ("%s", name);
carId = findCarIDByName ( allCars, totalCars, name );
} while (carId == -1);
  
allRentals[totalRentals].carId = carId;
  
return totalRentals + 1;
}
int findReservation ( RentalT *allRentals,
int totalRentals,
char *renterName )
{
int i;
  
for (i=0; i < totalRentals; i++ )
if ( strcmp ( allRentals[i].renterName, renterName ) == 0 )
return i;
  
return -1;
}
int findCarIDByName ( CarT *allCars,
int totalCars,
char *carName )
{
int i;
  
for (i=0; i < totalCars; i++ )
if ( strcmp ( allCars[i].make, carName ) == 0 )
return allCars[i].carId;
  
return -1;
}
int findCarById ( CarT *allCars,
int totalCars,
int carId )
{
int i;
  
for (i=0; i < totalCars; i++ )
if ( allCars[i].carId == carId )
return i;
  
return -1;
}
void printAllRentals (RentalT *allRentals,
int totalRentals,
CarT *allCars,
int totalCars)
{
int i;
  
for (i=0; i < totalRentals; i++ ) {
printf ("------------------------------------------------------------------------- ");
printf ("Renter: %s ", allRentals[i].renterName);
printf ("Days renting: %d ", allRentals[i].daysRenting);
printCarInfo ( allCars, totalCars, allRentals[i].carId );
printf ("------------------------------------------------------------------------- ");
printf (" ");
}
}

void printCarInfo ( CarT *allCars,
int totalCars,
int carId )
{
int i;
  
for (i=0; i < totalCars; i++ ) {
if ( allCars[i].carId == carId ) {
printf ("Make: %s ", allCars[i].make);
printf ("Model: %s ", allCars[i].model);
printf ("Number of doors: %d ", allCars[i].numDoors);
printf ("Rental rate: %lf ", allCars[i].rate);
return;
}
}
printf ("Car info for car %d not found. ", carId);
}
  
double getAverageRentalDays (RentalT *allRentals,
int totalRentals )
{
int i;
double sum = 0.0;
  
for (i=0; i < totalRentals; i++ ) {
sum += allRentals[i].daysRenting;
}
  
return sum / totalRentals;
}