fork download
  1. //Matthew Santos CS1A Ch. 6, Pg. 370, #4
  2. /***********************************************
  3.  *
  4.  * FIND SAFEST DRIVING AREA
  5.  * _____________________________________________
  6.  * Determines which of the five regions is the
  7.  * safest to drive in based off the amount of
  8.  * accidents reported there in the last year.
  9.  * _____________________________________________
  10.  * INPUT
  11.  * north : reported accidents in region north
  12.  * south : reported accidents in region south
  13.  * east : reported accidents in region east
  14.  * west : reported accidents in region west
  15.  * central: reported accidents in region central
  16.  *
  17.  * OUTPUT
  18.  * lowest : region with the lowest accidents
  19.  ***********************************************/
  20. #include <iostream>
  21. #include <string>
  22. using namespace std;
  23.  
  24. int getNumAccidents(string);
  25.  
  26. void findLowest(int, int, int, int, int);
  27.  
  28. int main() {
  29. //Initialize varibles
  30. int north = getNumAccidents("North");
  31. int south = getNumAccidents("South");
  32. int east = getNumAccidents("East");
  33. int west = getNumAccidents("West");
  34. int central = getNumAccidents("Central");
  35. int lowest;
  36.  
  37. // Determine and display which region had the fewest accidents
  38. findLowest(north, south, east, west, central);
  39.  
  40. return 0;
  41. }
  42.  
  43. int getNumAccidents(string regionName) {
  44. int accidents;
  45. cout << "Enter the number of automobile accidents for the " << regionName << " region: ";
  46. cin >> accidents;
  47.  
  48. // Input validation
  49. while (accidents < 0) {
  50. cout << "Accident number cannot be negative. Enter again for " << regionName << ": ";
  51. cin >> accidents;
  52. }
  53.  
  54. return accidents;
  55. }
  56.  
  57. void findLowest(int north, int south, int east, int west, int central) {
  58. int lowest = north;
  59. string region = "North";
  60.  
  61. if (south < lowest) {
  62. lowest = south;
  63. region = "South";
  64. }
  65. if (east < lowest) {
  66. lowest = east;
  67. region = "East";
  68. }
  69. if (west < lowest) {
  70. lowest = west;
  71. region = "West";
  72. }
  73. if (central < lowest) {
  74. lowest = central;
  75. region = "Central";
  76. }
  77.  
  78. cout << "\nThe region with the fewest accidents is " << region
  79. << " with " << lowest << " accidents." << endl;
  80. }
Success #stdin #stdout 0s 5320KB
stdin
5 6 7 8 9
stdout
Enter the number of automobile accidents for the North region: Enter the number of automobile accidents for the South region: Enter the number of automobile accidents for the East region: Enter the number of automobile accidents for the West region: Enter the number of automobile accidents for the Central region: 
The region with the fewest accidents is North with 5 accidents.