fork download
  1. //Devin Scheu CS1A Chapter 6, P. 370, #4
  2. //
  3. /**************************************************************
  4. *
  5. * DETERMINE SAFEST DRIVING AREA BY ACCIDENTS
  6. * ____________________________________________________________
  7. * This program identifies the geographic region with the
  8. * fewest reported automobile accidents last year.
  9. * ____________________________________________________________
  10. * INPUT
  11. * regionAccidents : The number of accidents for each region
  12. *
  13. * OUTPUT
  14. * safestRegion : The name and accident figure of the region with the fewest accidents
  15. *
  16. **************************************************************/
  17.  
  18. #include <iostream>
  19. #include <iomanip>
  20. #include <string>
  21.  
  22. using namespace std;
  23.  
  24. // Function to get number of accidents for a region
  25. int getNumAccidents(string regionName) {
  26. int accidents;
  27. cout << "Enter the number of accidents for " << regionName << ": ";
  28. cin >> accidents;
  29. while (accidents < 0) {
  30. cout << "\nError: Please enter a non-negative number: ";
  31. cin >> accidents;
  32. }
  33. cout << accidents << endl;
  34. return accidents;
  35. }
  36.  
  37. // Function to find the region with the lowest accidents
  38. void findLowest(int north, int south, int east, int west, int central) {
  39. int lowestAccidents = north;
  40. string safestRegion = "North";
  41.  
  42. if (south < lowestAccidents) {
  43. lowestAccidents = south;
  44. safestRegion = "South";
  45. }
  46. if (east < lowestAccidents) {
  47. lowestAccidents = east;
  48. safestRegion = "East";
  49. }
  50. if (west < lowestAccidents) {
  51. lowestAccidents = west;
  52. safestRegion = "West";
  53. }
  54. if (central < lowestAccidents) {
  55. lowestAccidents = central;
  56. safestRegion = "Central";
  57. }
  58.  
  59. cout << left << setw(25) << "Safest Region:" << right << setw(15) << safestRegion << endl;
  60. cout << left << setw(25) << "Number of Accidents:" << right << setw(15) << lowestAccidents << endl;
  61. }
  62.  
  63. int main () {
  64.  
  65. //Variable Declarations
  66. int northAccidents; //INPUT - The number of accidents for North region
  67. int southAccidents; //INPUT - The number of accidents for South region
  68. int eastAccidents; //INPUT - The number of accidents for East region
  69. int westAccidents; //INPUT - The number of accidents for West region
  70. int centralAccidents; //INPUT - The number of accidents for Central region
  71. string safestRegion; //OUTPUT - The name and accident figure of the region with the fewest accidents
  72.  
  73. //Get Accidents for Each Region
  74. northAccidents = getNumAccidents("North");
  75. southAccidents = getNumAccidents("South");
  76. eastAccidents = getNumAccidents("East");
  77. westAccidents = getNumAccidents("West");
  78. centralAccidents = getNumAccidents("Central");
  79.  
  80. //Separator and Output Section
  81. cout << "-------------------------------------------------------" << endl;
  82. cout << "OUTPUT:" << endl;
  83.  
  84. //Find and Display Safest Region
  85. findLowest(northAccidents, southAccidents, eastAccidents, westAccidents, centralAccidents);
  86.  
  87. } //end of main()
Success #stdin #stdout 0.01s 5312KB
stdin
30
7
14
32
18
stdout
Enter the number of accidents for North: 30
Enter the number of accidents for South: 7
Enter the number of accidents for East: 14
Enter the number of accidents for West: 32
Enter the number of accidents for Central: 18
-------------------------------------------------------
OUTPUT:
Safest Region:                     South
Number of Accidents:                   7