fork download
  1. //Castulo Jason Quintero CSC5 Chapter 6, Pg. 370, #4
  2. //
  3. /**************************************************************************
  4.  *
  5.  * Determine Safest Driving Area
  6.  * ________________________________________________________________________
  7.  * This program takes as user input the accidents that have
  8.  * occured in each region and display the region with the
  9.  * least amount of accidents.
  10.  *
  11.  * Computation is based on formula:
  12.  * The findLowest function will compare the accidents of each
  13.  * region and display the region with the least amount of
  14.  * accidents.
  15.  * ________________________________________________________________________
  16.  * INPUT
  17.  * crash : The amount of accidents in the region
  18.  *
  19.  * OUTPUT
  20.  * accident1 : Accidents in the north region
  21.  * accident2 : Accidents in the east region
  22.  * accident3 : Accidents in the south region
  23.  * accident4 : Accidents in the west region
  24.  * accident5 : Accidents in the central region
  25.  * The output will be the region with the least amount of
  26.  * accidents.
  27.  *
  28.  *************************************************************************/
  29.  
  30. #include <iostream>
  31. #include <iomanip>
  32. using namespace std;
  33.  
  34. // Prototypes
  35. int getNumAccidents(string);
  36. void findLowest(int, int, int, int, int);
  37.  
  38. int main()
  39. {
  40. // String variables to hold the regions names.
  41. string name1 = "North";
  42. string name2 = "East";
  43. string name3 = "South";
  44. string name4 = "West";
  45. string name5 = "Central";
  46.  
  47. // Variables to hold the accidents of each region.
  48. int accident1;
  49. int accident2;
  50. int accident3;
  51. int accident4;
  52. int accident5;
  53.  
  54. // Tells the user what will happen
  55. cout << "Enter the number of accidents in each region and the "
  56. << "region with the least amount of accidents will be presented.\n";
  57.  
  58. // Function that is passed region name, validates, and returns the
  59. // value to the variable associated with the region.
  60. accident1 = getNumAccidents(name1);
  61. accident2 = getNumAccidents(name2);
  62. accident3 = getNumAccidents(name3);
  63. accident4 = getNumAccidents(name4);
  64. accident5 = getNumAccidents(name5);
  65.  
  66. // Function that is passed the values and checks to see which
  67. // region had the least amount of accidents.
  68. findLowest(accident1, accident2, accident3, accident4, accident5);
  69. return 0;
  70. }
  71.  
  72. // User enters the accidents of each region and validates it.
  73. int getNumAccidents(string regionname)
  74. {
  75. int crash;
  76.  
  77. cout << "Please enter the number of accidents in the " << regionname
  78. << " region: " << endl;
  79. cin >> crash;
  80. while (crash < 0)
  81. {
  82. cout << "Please enter a value greater than zero: ";
  83. cin >> crash;
  84. }
  85. return crash;
  86. }
  87.  
  88. // Determines which region has the least amount of accidents.
  89. void findLowest (int accident1, int accident2, int accident3,
  90. int accident4, int accident5)
  91. {
  92. if (accident1 < accident2 && accident1 < accident3 &&
  93. accident1 < accident4 && accident1 < accident5)
  94. {
  95. cout << "The North region has the least amount of accidents!"
  96. << " With only " << accident1 << " accident(s).";
  97. }
  98. else if (accident2 < accident1 && accident2 < accident3
  99. && accident2 < accident4 && accident2 < accident5)
  100. {
  101. cout << "The East region has the least amount of accidents!"
  102. << " With only " << accident2 << " accident(s).";
  103. }
  104. else if (accident3 < accident1 && accident3 < accident2
  105. && accident3 < accident4 && accident3 < accident5)
  106. {
  107. cout << "The South region has the least amount of accidents!"
  108. << " With only " << accident3 << " accident(s).";
  109. }
  110. else if (accident4 < accident1 && accident4 < accident2
  111. && accident4 < accident3 && accident4 < accident5)
  112. {
  113. cout << "The West region has the least amount of accidents!"
  114. << " With only " << accident4 << " accident(s).";
  115. }
  116. else
  117. {
  118. cout << "The Central region has the least amount of accidents!"
  119. << " With only " << accident5 << " accident(s).";
  120. }
  121. }
Success #stdin #stdout 0.01s 5300KB
stdin
1 2 3 4 5
stdout
Enter the number of accidents in each region and the region with the least amount of accidents will be presented.
Please enter the number of accidents in the North region: 
Please enter the number of accidents in the East region: 
Please enter the number of accidents in the South region: 
Please enter the number of accidents in the West region: 
Please enter the number of accidents in the Central region: 
The North region has the least amount of accidents! With only 1 accident(s).