QuestionQuestion

Transcribed TextTranscribed Text

In this assignment, you will gain experience in using structs, pointers, malloc() and free(). Make sure to put comments in your code … especially ones to describe your functions, definitions and important parts of your code. You must also write your code as cleanly & efficiently as possible. (1) The Driver, Vehicle & Infraction We would like to simulate a police database that keeps track of a driver’s history with respect to the number of times they were pulled over and ticketed for an infraction (i.e., a driving violation). The database will make use of both static and dynamically-allocated arrays so as to allow many drivers, vehicles and infractions to be stored together in lists. We will design a very small system and test it with only a few vehicles and owners. Download the template code called PoliceDatabase.c. It contains a main() function which will serve as your test case for the assignment. Read it over carefully to make sure that you understand what it is doing. You will need to write the functions and procedures that are called from this main() function. You ARE NOT ALLOWED to modify nor change the main() function. To begin, you will need to define 4 structs that are being used. They are described below. All strings should be char * … DO NOT allocate char [] arrays with fixed limits. Define a typedef struct called Driver that defines the following attributes: license - a string representing a driver's license ID (e.g., "L0678-67825-83940"); name - a string representing the name of the driver street - a string representing the street name and number for this driver (e.g., "12 Elm St."); city - a string representing the city in which this driver lives province - a string representing the province in which this driver lives Define a typedef struct called Vehicle that defines the following attributes: make - a string representing the company that made the vehicle (e.g., "Honda") model - a string representing the model of the vehicle (e.g., "Civic", etc..) color - a string representing the color of the vehicle (e.g., "red") plate - a string representing the license plate of the vehicle (e.g., "X5T6Y8") year – an unsigned short int representing the year of the vehicle (e.g., 2004) reportedStolen - an unsigned char indicating if vehicle has been reported as stolen owner - a Driver * representing the owner of the vehicle Define a typedef struct called Infraction (i.e., violation) that defines the following attributes: amount - a float indicating how much the fine was for this infraction description - a String describing the infraction (e.g., "Not stopping for red light") year – an unsigned short int representing year of infraction (e.g., 2020) month – an unsigned char representing month of infraction (e.g., 1 = Jan) day – an unsigned char representing day of infraction (e.g., 23) outstanding – an unsigned char indicating if infraction was paid yet driver - a Driver * representing the driver that received the infraction Define a typedef struct called PoliceDatabase that defines the following attributes: drivers – an array storing all drivers (i.e., people who drive vehicles) in the database. You should set this to be a maximum of 2000 … create a #define for this number. numDrivers - an unsigned short int that keeps track of how many drivers there are in the database vehicles – an array storing all vehicles in the database. You should set this to be a maximum of 1000 … create a #define for this number. numVehicles - an unsigned short int that keeps track of how many vehicles there are in the database infractions – an array storing all infractions that have ever been given to drivers. You should set this to be a maximum of 800 … create a #define for this number. numInfractions - an unsigned short int that keeps track of how many infractions there are in the database You must write the following functions and procedures in order for the program to work: • a registerDriver() function which takes a PoliceDatabase * and driver information (i.e., license, name, address, city, province) as parameters. It should then add the driver to the given database ... assuming that the database is not full, otherwise the driver is not to be registered. It should return 1 if the registration was ok, otherwise 0. • a registerVehicle() function which takes a PoliceDatabase * and vehicle information (i.e., make, model, year, color, plate and driver’s license) as parameters. It should then add the vehicle to the given database ... assuming that the database is not full, otherwise the vehicle is not to be registered. Make sure to store the vehicle’s owner properly … you’ll have to find the one with the given license. It should return 1 if the registration was ok, otherwise 0. • a unregisterVehicle() function which takes a PoliceDatabase * and a vehicle plate as parameters. It should then remove the vehicle with that plate from the database without altering the order of the vehicles currently in the database and without leaving any gaps in the array. You must copy all other vehicles back one position in the array to fill in the gap. If the plate is not in the database, then nothing is to be done. It should return 1 if the unregistration was ok, otherwise 0. • a procedure called pay() which takes a single Infraction * as parameter and then pays the infraction (hint…use the outstanding attribute). • a changeOwner() function which takes a PoliceDatabase *, a vehicle plate and a driver’s license as its parameters. It should then update the database by changing the owner information for the vehicle with the given plate to the driver with the given license (i.e., this second parameter is not the license plate of the vehicle … it is the driver’s license). If either the plate or the driver’s license is not in the database, then nothing is to be done. It should return 1 if the change was made, otherwise 0. • a reportStolen() function which takes a PoliceDatabase * and a vehicle plate as its parameters. It should then properly record that the vehicle with the given plate has been stolen. If the plate is not in the database, then nothing is to be done. It should return 1 if the car was found, otherwise 0. • issueInfraction() function which takes a PoliceDatabase *, a driver’s license and the infraction information (i.e., amount, description, year, month, day) as its parameters. It should then cause the driver with the given driver’s license to receive an infraction for the given amount and description (i.e., reason) on the given date ... updating the database as necessary. This function MUST return the Infraction created. If the number of infractions has reached its limit in the database, then nothing is to be done. • hasOutstandingInfractions() function which takes a PoliceDatabase * and a Driver * as its parameters. It should then return 1 if the driver has any infractions that have not been paid yet, otherwise it should return 0. • shouldStopVehicle() function which takes a PoliceDatabase * and a vehicle plate as its parameters. It should then decide whether or not the police should pull this vehicle over. It should return 1 if and only if the vehicle has been reported stolen, or if the vehicle's owner has at least one outstanding infraction … otherwise, the function should return 0. You MUST make use of the function hasOutstandingInfractions(). • showInfractionsFor() procedure which takes a PoliceDatabase * and a driver’s license as its parameters. It should then display a list of all infractions that the driver with this license has incurred showing (on successive lines) the driver's infractions … and then finish with the total number of outstanding infractions. Here is the format you should use … the date showing year, month and then day … and the status of each infraction as being either PAID IN FULL or OUTSTANDING: $100.00 infraction on 2010/02/03 [PAID IN FULL] $250.00 infraction on 2012/09/06 [PAID IN FULL] $250.00 infraction on 2017/07/07 [PAID IN FULL] $350.00 infraction on 2019/11/03 [OUTSTANDING] $350.00 infraction on 2020/01/01 [OUTSTANDING] Total outstanding infractions = 2 • cleanDrivers() procedure which takes a PoliceDatabase * and a double star pointer (i.e., Driver **) to an initially-null array of drivers as its parameters. It should determine the number of clean drivers (i.e., all drivers who have never had any infractions) and then dynamicallyallocate a Driver * array with that exact size and fill it in with drivers that are found to be clean. It should then set the incoming Driver ** parameter to the newly-allocated array. • showInfractionReport() procedure which takes a PoliceDatabase * as its parameter. It should then list information about all drivers who have had at least one infraction. Each line in the list should show a driver’s name (formatted to take exactly 20 spaces) followed by the number of infractions that they received and the total amount for all infractions that they have already paid (formatted as shown) as follows: Bob B. Pins: 1 infractions, total paid = $ 75.00 Ilene Dover: 4 infractions, total paid = $400.00 Jim Class: 5 infractions, total paid = $600.00 Bob Upandown: 1 infractions, total paid = $ 0.00 Sam Pull: 2 infractions, total paid = $250.00 Neil Down: 1 infractions, total paid = $ 0.00 _________________________________________________________________________________________ Here is the proper output for the main() function … make sure that it works: Here are the drivers: L1567-34323-84980 Matt Adore living at 1323 Kenaston St., Gloucester, ON L0453-65433-87655 Bob B. Pins living at 32 Rideau Rd., Greely, ON L2333-45645-54354 Stan Dupp living at 1355 Louis Lane, Gloucester, ON L1234-35489-99837 Ben Dover living at 2348 Walkley Rd., Ottawa, ON L8192-87498-27387 Patty O'Lantern living at 2338 Carling Ave., Nepean, ON L2325-45789-35647 Ilene Dover living at 287 Bank St., Ottawa, ON L1213-92475-03984 Patty O'Furniture living at 200 St. Laurant Blvd., Ottawa, ON L1948-87265-34782 Jen Tull living at 1654 Stonehenge Cres., Ottawa, ON L0678-67825-83940 Jim Class living at 98 Oak Blvd., Ottawa, ON L0122-43643-73286 Mark Mywords living at 3 Third St., Ottawa, ON L6987-34532-43334 Bob Upandown living at 434 Gatineau Way, Hull, QC L3345-32390-23789 Carrie Meehome living at 123 Thurston Drive, Kanata, ON L3545-45396-88983 Sam Pull living at 22 Colonel By Drive, Ottawa, ON L1144-26783-58390 Neil Down living at 17 Murray St., Nepean, ON L5487-16576-38426 Pete Reedish living at 3445 Bronson Ave., Ottawa, ON Here are the vehicles: A yellow 2015 Honda Civic with plate W3EW4T A dark green 2007 Pontiac Grand Prix with plate GO SENS A white 2004 Mazda RX-8 with plate OH YEAH A bergundy 2017 Nissan Altima with plate Y6P9O7 A white 2002 Saturn Vue with plate 9R6P2P A gray 2018 Honda Accord with plate 7U3H5E A gold 2006 Chrysler PT-Cruiser with plate OLDIE A white 2010 Nissan Cube with plate 5Y6K8V A silver 1989 Porsche 959 with plate CATCHME A red 2018 Kia Soul with plate J8JG2Z A black 2014 Porsche Cayenne with plate EXPNSV A silver 2010 Nissan Murano with plate Q2WF6H A black 2008 Honda Element with plate N7MB5C A green 2010 Toyota RAV-4 with plate R3W5Y7 A red 2006 Toyota Celica with plate FUNFUN Removing 3 vehicles (i.e., W3EW4T, FUNFUN and CATCHME) ... Here are the remaining vehicles: A dark green 2007 Pontiac Grand Prix with plate GO SENS A white 2004 Mazda RX-8 with plate OH YEAH A bergundy 2017 Nissan Altima with plate Y6P9O7 A white 2002 Saturn Vue with plate 9R6P2P A gray 2018 Honda Accord with plate 7U3H5E A gold 2006 Chrysler PT-Cruiser with plate OLDIE A white 2010 Nissan Cube with plate 5Y6K8V A red 2018 Kia Soul with plate J8JG2Z A black 2014 Porsche Cayenne with plate EXPNSV A silver 2010 Nissan Murano with plate Q2WF6H A black 2008 Honda Element with plate N7MB5C A green 2010 Toyota RAV-4 with plate R3W5Y7 Here is the owner of the Honda Accord: Ilene Dover Ilene Dover sells the car to Sam Pull, here is the owner now: Sam Pull Here are the stolen cars at this time: Report some cars as stolen... Here are the stolen cars now: A white 2004 Mazda RX-8 with plate OH YEAH A gold 2006 Chrysler PT-Cruiser with plate OLDIE Vehicles that should be stopped: A white 2004 Mazda RX-8 with plate OH YEAH A gold 2006 Chrysler PT-Cruiser with plate OLDIE A white 2010 Nissan Cube with plate 5Y6K8V A black 2014 Porsche Cayenne with plate EXPNSV A black 2008 Honda Element with plate N7MB5C Here are all the clean drivers: L1567-34323-84980 Matt Adore living at 1323 Kenaston St., Gloucester, ON L2333-45645-54354 Stan Dupp living at 1355 Louis Lane, Gloucester, ON L1234-35489-99837 Ben Dover living at 2348 Walkley Rd., Ottawa, ON L8192-87498-27387 Patty O'Lantern living at 2338 Carling Ave., Nepean, ON L1213-92475-03984 Patty O'Furniture living at 200 St. Laurant Blvd., Ottawa, ON L1948-87265-34782 Jen Tull living at 1654 Stonehenge Cres., Ottawa, ON L0122-43643-73286 Mark Mywords living at 3 Third St., Ottawa, ON L3345-32390-23789 Carrie Meehome living at 123 Thurston Drive, Kanata, ON L5487-16576-38426 Pete Reedish living at 3445 Bronson Ave., Ottawa, ON Here is the infraction report: Bob B. Pins: 1 infractions, total paid = $ 75.00 Ilene Dover: 4 infractions, total paid = $400.00 Jim Class: 5 infractions, total paid = $600.00 Bob Upandown: 1 infractions, total paid = $ 0.00 Sam Pull: 2 infractions, total paid = $250.00 Neil Down: 1 infractions, total paid = $ 0.00 Here is the status of Jim Class: $100.00 infraction on 2010/02/03 [PAID IN FULL] $250.00 infraction on 2012/09/06 [PAID IN FULL] $250.00 infraction on 2017/07/07 [PAID IN FULL] $350.00 infraction on 2019/11/03 [OUTSTANDING] $350.00 infraction on 2020/01/01 [OUTSTANDING] Total outstanding infractions = 2

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

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

#define TRUE 1
#define FALSE 0

// This struct represents a driver in the database

typedef struct {
    // *** complete this definition ***
    // license - a string representing a driver's license ID (e.g., "L0678-67825-83940");
    char *license;
    // name - a string representing the name of the driver
    char *name;
    // street - a string representing the street name and number for this driver (e.g., "12
    // Elm St.");
    char *street;
    // city - a string representing the city in which this driver lives
    char *city;
    // province - a string representing the province in which this driver lives
    char *province;
} Driver;

// This struct represents a vehicle in the database

typedef struct {
    // *** complete this definition ***
    // make - a string representing the company that made the vehicle (e.g., "Honda")
    char *make;
    // model - a string representing the model of the vehicle (e.g., "Civic", etc..)
    char *model;
    // color - a string representing the color of the vehicle (e.g., "red")
    char *color;
    // plate - a string representing the license plate of the vehicle (e.g., "X5T6Y8")
    char *plate;
    // year – an unsigned short int representing the year of the vehicle (e.g., 2004)
    unsigned short int year;
    // reportedStolen - an unsigned char indicating if vehicle has been reported as stolen
    unsigned char reportedStolen;
    // owner - a Driver * representing the owner of the vehicle
    Driver *owner;
} Vehicle;

// This struct represents an infraction in the database

typedef struct {
    // *** complete this definition ***
    // amount - a float indicating how much the fine was for this infraction
    float amount;
    // description - a String describing the infraction (e.g., "Not stopping for red light")
    char *description;
    // year – an unsigned short int representing year of infraction (e.g., 2020)
    unsigned short int year;
    // month – an unsigned char representing month of infraction (e.g., 1 = Jan)
    unsigned char month;
    // day – an unsigned char representing day of infraction (e.g., 23)
    unsigned char day;
    // outstanding – an unsigned char indicating if infraction was paid yet
    unsigned char outstanding;
    // driver - a Driver * representing the driver that received the infraction
    Driver *driver;
} Infraction;

// This struct represents the database
#define MAX_DRIVERS 2000
#define MAX_INFRACTIONS 800
#define MAX_VEHICLES 1000

typedef struct {
    // *** complete this definition ***
    /*
    * drivers – an array storing all drivers (i.e., people who drive
    vehicles) in the database. You should set this to be a
    maximum of 2000 … create a #define for this number.
    */
    Driver drivers[MAX_DRIVERS];

    /*
    * numDrivers - an unsigned short int that keeps track of how
    * many drivers there are in the database
    */
    unsigned short int numDrivers;

    /*
    * vehicles – an array storing all vehicles in the database. You
    * should set this to be a maximum of 1000 … create a #define
    * for this number.
    */
    Vehicle vehicles[MAX_VEHICLES];

    /**
    * numVehicles - an unsigned short int that keeps track of how many
    * vehicles there are in the database
    */
    unsigned short int numVehicles;
    /**
    * infractions – an array storing all infractions that have ever been
    * given to drivers. You should
    * set this to be a maximum of 800 … create a#define for this number.
    */
    Infraction infractions[MAX_INFRACTIONS];
    /**
    * numInfractions - an unsigned short int that keeps track of how many
    * infractions there are in the database
    */
    unsigned short int numInfractions;

} PoliceDatabase;

// ********************************************
// WRITE ALL YOUR FUNCTIONS AND PROCEDURES HERE
// ********************************************

/**
* a registerDriver() function which takes a PoliceDatabase * and driver information
* (i.e., license, name, address, city, province) as parameters. It should then
* add the driver to the
* given database ... assuming that the database is not full, otherwise
* the driver is not to be
* registered. It should return 1 if the registration was ok, otherwise 0.
* @param pdb
* @param license
* @param name
* @param address
* @param city
* @param province
* @return
*/
int registerDriver(PoliceDatabase *pdb, char *license, char *name,
       char *address, char *city, char *province) {
   
    // be sure that the database is not full
    if (pdb->numDrivers >= MAX_DRIVERS) {
       return FALSE;
    }
   
    // get a empty slot
    Driver *driver = pdb->drivers + pdb->numDrivers;
   
    // update it
    driver->city = strdup(city);
    driver->license = strdup(license);
    driver->name = strdup(name);
    driver->province = strdup(province);
    driver->street = strdup(address);

    // update counter
    pdb->numDrivers = pdb->numDrivers + 1;
      
    return TRUE;
}

/**
* a registerVehicle() function which takes a PoliceDatabase * and vehicle
* information (i.e., make, model, year, color, plate and driver’s license) as
* parameters. It should then add the vehicle to the given database ... assuming
* that the database is not full, otherwise the vehicle is
* not to be registered. Make sure to store the vehicle’s owner properly …
* you’ll have to find the one with the given license. It should return 1
* if the registration was ok, otherwise 0.
* @param pdb
* @param make
* @param model
* @param year
* @param color
* @param plate
* @param license
* @return
*/
int registerVehicle(PoliceDatabase *pdb, char *make, char *model,
       unsigned short int year, char *color,
       char *plate, char *license) {
   
    // make sure the database is not full
    if (pdb->numVehicles >= MAX_VEHICLES) {
       return FALSE;
    }
   
    // get an empty vehicle slot
    Vehicle *vehicle = pdb->vehicles + pdb->numVehicles;
   
    // update its data
    vehicle->color = strdup(color);
    vehicle->make...

By purchasing this solution you'll be able to access the following files:
policeDatabase.c.

$60.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats