int findLowest(int arr[]) Is passed the five accident totals as an array of ints. It determines which is the smallest and returns the subscript for that array element.
The main function declares two arrays, a string array containing the names of the boroughs and an int array which will hold the accident numbers for each borough. The main() function calls the getNumAccidents() function to fill the int array. It then calls findLowest() to learn which borough had the fewest accidents. It should print out the accident numbers for each borough and then print out the borough and the accident number with the fewest accidents.
What is meant by "validates the input"? The number of accidents in a year cannot be negative, so only a value of 0 or greater is an acceptable input.
So far I have everything except for the code to validate the input, that's the only part still missing, and what I do have is all in working order... I've tried playing around with if/else, do/while, etc., can't figure it out. Code I have is as follows:
#include <iostream>
using namespace std;
int getNumAccidents (string);
int findLowest (int arr[]);
int main()
{
string Boroughs[5] = {"Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island"};
int numAccidents[5];
int x;
cout << "For each borough, input the number of automobile accidents for that borough for the year.\n" << endl;
for (int i=0; i<5; i++)
{
numAccidents[i] = 0;
cout << Boroughs[i] << "\t";
numAccidents[i] = getNumAccidents(Boroughs[i]);
cout << endl;
}
x = findLowest(numAccidents);
cout << "Borough with the fewest accidents this year: " << Boroughs[x] << " (" << numAccidents[x] << ")";
return 0;
}
int getNumAccidents (string instance)
{
int numAccidents;
cin >> numAccidents;
return numAccidents;
}
int findLowest (int arr[])
{
int Lowest = arr[0], hold;
for (int i=0; i<4; i++)
{
if (arr[i+1] < Lowest)
{
Lowest = arr[i+1];
hold = i+1;
}
}
return hold;
}