/* Safest Driving Area - This program determines which of five geographic
regions within a major city (North, South, East, West, and Central) had
the fewest reported automobile accidents last year. It has the following
two functions, called by main:
* int getNumAccidents()
* void findLowest()
Input Validation: No accident number that is less than 0 is accepted. */
#include "Utility.h"
/* Prototypes: Get number of Accidents, Find region with lowest
accidents */
int getNumAccidents(int, string);
void findLowest(int, int, int, int, int);
int main()
{
/* Variables: Regions (North, South, East, West, Central) */
int regionNorth = 0,
regionSouth = 0,
regionEast = 0,
regionWest = 0,
regionCentral = 0,
numAccidents = 0;
/* Display: Introduction */
cout << "\t\tTraffic Control Center - Accident Reports\n\n"
<< "---------------------------------------------------\n"
<< "Enter the number of accidents that occured in each\n"
<< "of the following regions: North, South, East, West\n"
<< "and Central to determine which region has had the\n"
<< "smallest number of automobile accidents last year.\n"
<< "---------------------------------------------------\n\n";
/* Get: Accident figures
Call: getNumAccidents */
regionNorth = getNumAccidents(numAccidents, "Silver Creek North: ");
regionSouth = getNumAccidents(numAccidents, "Silver Creek South: ");
regionEast = getNumAccidents(numAccidents, "Silver Creek East: ");
regionWest = getNumAccidents(numAccidents, "Silver Creek West: ");
regionCentral = getNumAccidents(numAccidents, "Silver Creek Central: ");
/* Call: findLowest */
findLowest(regionNorth, regionSouth, regionEast,
regionWest, regionCentral);
pauseSystem();
return 0;
}
/* **********************************************************
Definition: getNumAccidents
The name of the region is passed to this function. It asks
the user for the number of automobile accidents reported
in that region during the last year, validates the input,
then returns it.
********************************************************** */
int getNumAccidents(int accidents, string regionName)
{
cout << "Accident Reports - " << regionName;
cin >> accidents;
/* Valdiation: While number of accidents is negative, a
message is displayed, asking the user to repeat his or
her input */
while (accidents < 0)
{
cout << "\nError! The number of traffic accidents entered\n"
<< "is invalid. Please enter 0 if applicable, or any\n"
<< "other number above 0.\n"
<< "Number of accidents: " << regionName;
cin >> accidents;
}
return accidents;
}
/* **********************************************************
Definition: findLowest
The five accident totals are passed to this function. It
determines which is the smallest and prints the name of
the region, along with its accident figure.
********************************************************** */
void findLowest(int lowestNorth, int lowestSouth, int lowestEast,
int lowestWest, int lowestCentral)
{
/* Variable: Region name */
string regionName = " ";
/* Variable: Lowest is initialized to lowestNorth as starting
value for the comparisons */
int lowest = lowestNorth;
/* Conditional Statements Tier 1: Determine which region has
had the lowest traffic accidents */
lowest = lowestSouth < lowest ? lowestSouth : lowest;
lowest = lowestEast < lowest ? lowestEast : lowest;
lowest = lowestWest < lowest ? lowestWest : lowest;
lowest = lowestCentral < lowest ? lowestCentral : lowest;
/* Conditional Statements Tier 2: The regions are assigned region
names for display in the report that follows */
regionName = lowestNorth == lowest ? "Silver Creek North" : regionName;
regionName = lowestSouth == lowest ? "Silver Creek South" : regionName;
regionName = lowestEast == lowest ? "Silver Creek East" : regionName;
regionName = lowestWest == lowest ? "Silver Creek West" : regionName;
regionName = lowestCentral == lowest ? "Silver Creek Central" : regionName;
/* Display: The region with the lowest accident number, along
with the region name */
cout << "\nAccident Report Summary: " << regionName << "\n"
<< "---------------------------------------------------\n"
<< "With " << lowest << " automobile accidents,\n"
<< regionName << " is the safest driving area.\n";
}
Monday, January 2, 2017
Programming Challenge 6.4 - Safest Driving Area
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment