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. cout << "Please enter the number of accidents in " << region;
  52. cout << " region: ";
  53. cin >> accidents;
  54. cout << endl;
  55.  
  56. // Validate Input
  57. while(accidents < 0)
  58. {
  59. cout << "Enter a value of 0 or greater." << endl;
  60. cin >> accidents;
  61. }
  62.  
  63. return accidents;
  64. }
  65.  
  66. void findLowest(int north, int south, int east, int west, int central)
  67. {
  68. int lowest;
  69. string safest;
  70.  
  71. // Set North as the Base for Lowest Value
  72. lowest = north;
  73. safest = "North";
  74.  
  75. // Test Regions for Values
  76. if(south < lowest)
  77. {
  78. lowest = south;
  79. safest = "South";
  80. }
  81.  
  82. if(east < lowest)
  83. {
  84. lowest = east;
  85. safest = "East";
  86. }
  87.  
  88. if(west < lowest)
  89. {
  90. lowest = west;
  91. safest = "West";
  92. }
  93.  
  94. if(central < lowest)
  95. {
  96. lowest = central;
  97. safest = "Central";
  98. }
  99.  
  100. // Display Safest Region
  101. cout << "The safest region to drive in is the " << safest << " region.";
  102. if(lowest != 1)
  103. {
  104. cout << endl << "This region has had " << lowest << " accidents.";
  105. }
  106.  
  107. if(lowest == 1)
  108. {
  109. cout << endl << "This region has had " << lowest << " accident.";
  110. }
  111.  
  112. }
Success #stdin #stdout 0.01s 5320KB
stdin
28 14 8 40 67
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 East region.
This region has had 8 accidents.