fork download
  1. //Cameron Pham CS1A Chapter 6, p. 370, #4
  2. //
  3. /******************************************************************************
  4. * DETERMINE SAFEST DRIVING AREA
  5. * ______________________________________________________________________________
  6. * This program accepts an integer value for a guess of a random number. It
  7. * Generates a random number.
  8. * ______________________________________________________________________________
  9. * INPUT
  10. * Number of accidents // Number of automobile accidents reported in given region
  11. *
  12. * OUTPUT
  13. * Safest driving area // Region with the smallest number of automobile accidents
  14. *
  15. *******************************************************************************/
  16. #include <iostream>
  17. #include <cmath>
  18. using namespace std;
  19.  
  20. // Set Function Prototypes
  21. int getNumAccidents(string region);
  22. void findLowest(int north, int south, int east, int west, int central);
  23.  
  24. int main()
  25. {
  26. // Declare Variables
  27. int north; // Number of accidents in north region
  28. int south; // Number of accidents in south region
  29. int east; // Number of accidents in east region
  30. int west; // Number of accidents in west region
  31. int central; // Number of accidents in central region
  32. int lowest; // Lowest number of accidents
  33.  
  34. // Get Accident Totals
  35. north = getNumAccidents("North");
  36. south = getNumAccidents("South");
  37. east = getNumAccidents("East");
  38. west = getNumAccidents("West");
  39. central = getNumAccidents("Central");
  40.  
  41. // Display the Safest Driving Area
  42. findLowest(north, south, east, west, central);
  43.  
  44. return 0;
  45. }
  46.  
  47. // Define the Prototypes
  48. int getNumAccidents(string region)
  49. {
  50. int accidents;
  51.  
  52. cout << "Please enter the number of accidents in " << region;
  53. cout << " region: ";
  54. cin >> accidents;
  55. cout << endl;
  56.  
  57. // Validate Input
  58. while(accidents < 0)
  59. {
  60. cout << "Enter a value of 0 or greater." << endl;
  61. cin >> accidents;
  62. }
  63.  
  64. return accidents;
  65. }
  66.  
  67. void findLowest(int north, int south, int east, int west, int central)
  68. {
  69. int lowest;
  70. string safest;
  71.  
  72. // Set North as the Base for Lowest Value
  73. lowest = north;
  74. safest = "North";
  75.  
  76. // Test Regions for Values
  77. if(south < lowest)
  78. {
  79. lowest = south;
  80. safest = "South";
  81. }
  82.  
  83. if(east < lowest)
  84. {
  85. lowest = east;
  86. safest = "East";
  87. }
  88.  
  89. if(west < lowest)
  90. {
  91. lowest = west;
  92. safest = "West";
  93. }
  94.  
  95. if(central < lowest)
  96. {
  97. lowest = central;
  98. safest = "Central";
  99. }
  100.  
  101. // Display Safest Region
  102. cout << "The safest region to drive in is the " << safest << " region.";
  103. if(lowest != 1)
  104. {
  105. cout << endl << "This region has had " << lowest << " accidents.";
  106. }
  107.  
  108. if(lowest == 1)
  109. {
  110. cout << endl << "This region has had " << lowest << " accident.";
  111. }
  112.  
  113. }
Success #stdin #stdout 0s 5320KB
stdin
1 7 21 46 9
stdout
Please enter the number of accidents in North region: 
Please enter the number of accidents in South region: 
Please enter the number of accidents in East region: 
Please enter the number of accidents in West region: 
Please enter the number of accidents in Central region: 
The safest region to drive in is the North region.
This region has had 1 accident.