1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <iostream>
#include <iomanip>
using namespace std;
int north, south, east, west, central;
/*
Function Prototypes
*/
void getNumAccidents ();
void sort (int array[], int size);
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
/*
Main Function*/
int main()
{
int array[ 5 ];
// call getNumAccidents
getNumAccidents();
array[ 0 ] = north;
array[ 1 ] = south;
array[ 2 ] = east;
array[ 3 ] = west;
array[ 4 ] = central;
sort ( array , 5 );
// Display the region with the least amount of accidents
if (array[0] == north)
cout << "The North region had the lowest amount of accidents. \n"
<< "Only " << north << " accidents were reported. \n";
else if (array[0] == south)
cout << "The South region had the lowest amount of accidents. \n"
<< "Only " << south << " accidents were reported. \n";
else if (array[0] == east)
cout << "The East region had the lowest amount of accidents. \n"
<< "Only " << east << " accidents were reported. \n";
else if (array[0] == west)
cout << "The West region had the lowest amount of accidents. \n"
"Only " << west << " accidents were reported. \n";
else if (array[0] == central)
cout << "The Central region had the lowest amount of accidents. \n"
<< "Only " << central << " accidents were reported. \n";
return 0;
} // end of main functin
/* getNumAccidents
*/
void getNumAccidents()
{
cout << "Enter the number of accidents in each region: \n"
<< "North : ";
cin >> north;
cout << "South : ";
cin >> south;
cout << "East : ";
cin >> east;
cout << "West : ";
cin >> west;
cout << "Central : ";
cin >> central;
while (north < 0)
{
cout << "The number of accidents canot be negative" << endl
<< "Please re-enter north: ";
cin >> north;
}
while (south < 0)
{
cout << "The number of accidents canot be negative" << endl
<< "Please re-enter north: ";
cin >> south;
}
while (west < 0)
{
cout << "The number of accidents canot be negative" << endl
<< "Please re-enter north: ";
cin >> west;
}
while (east < 0)
{
cout << "The number of accidents canot be negative" << endl
<< "Please re-enter north: ";
cin >> east;
}
while (central < 0)
{
cout << "The number of accidents canot be negative" << endl
<< "Please re-enter north: ";
cin >> central;
}
}
/* findLowest()
*/
void sort (int array[], int size)
{
for ( int i = 0; i < size; i++ )
{
int index = findSmallestRemainingElement( array, size, i );
swap( array, i, index );
}
}
int findSmallestRemainingElement (int array[], int size, int index)
{
int index_of_smallest_value = index;
for (int i = index + 1; i < size; i++)
{
if ( array[ i ] < array[ index_of_smallest_value ] )
{
index_of_smallest_value = i;
}
}
return index_of_smallest_value;
}
void swap (int array[], int first_index, int second_index)
{
int temp = array[ first_index ];
array[ first_index ] = array[ second_index ];
array[ second_index ] = temp;
}
|