c++ question Write a program that determines which of 5 countrys (China, England
ID: 3878924 • Letter: C
Question
c++ question
Write a program that determines which of 5 countrys (China, England, Iceland, India, and USA) had the fewest reported automobile accidents last year.
It should have the following two functions, which are called by main. int getNumAccidents( ) is passed the name of a country.
It asks the user for the number of automobile accidents reported in that country during the last year, validates the input, then returns it. It should be called once for each country.
void findLowest( ) is passed the five accident totals. It determines which is the smallest and prints the name of the country, along with its accident figure
Explanation / Answer
#include <iostream>
using namespace std;
string countries[5] = {"China", "England", "Iceland", "India", "USA"};
// TAKING a country and validating input
int getNumAccidents(string country)
{
int acc;
cout << "Enter numbers of accidents for " << country << ": ";
cin >> acc;
// checking if number is negative, then asking for input one more time
while(acc < 0)
{
cout << "Note: Invalid number. Please enter positive numbers. ";
cout << "Enter numbers of accidents for " << country << ": ";
cin >> acc;
}
return acc;
}
// finding lowest and printing the country name
void findLowest(int a[] )
{
int i;
int min = a[0], minIndex = 0;
// finding if any is lesser
for(i=1; i<5; i++)
{
if(a[i] < min)
{
min = a[i];
minIndex = i;
}
}
cout << countries[minIndex] << " has lowest accident rate.";
}
main() {
int i;
int accidents[5];
// asking for user input through a method
for(i=0; i<5; i++)
{
accidents[i] = getNumAccidents(countries[i]);
}
// calling findLowest function
findLowest(accidents);
}
/*SAMPLE OUTPUT
Enter numbers of accidents for China: 5
Enter numbers of accidents for England: 7
Enter numbers of accidents for Iceland: 6
Enter numbers of accidents for India: 3
Enter numbers of accidents for USA: 4
India has lowest accident rate.
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.