fork download
  1. //Andres Guzman CSC5 Chapter 6, P. 370, #4
  2. //
  3. /*******************************************************************************
  4. *
  5. * DETERMINE SAFEST REGION
  6. * ______________________________________________________________________________
  7. * This program will determine the safest of 5 regions among car crashes
  8. *
  9. * Computation:
  10. *
  11. * region < lowest
  12. * ______________________________________________________________________________
  13. * INPUT
  14. *
  15. * north = saves north region function
  16. * south = saves south region function
  17. * east = saves east region function
  18. * west = saves west region function
  19. * central = saves central region function
  20. * OUTPUT
  21. * lowest = lowest amount of crashes
  22. * lowestregion = region with lowest crashes reported
  23. *******************************************************************************/
  24. #include <iostream>
  25. #include <string>
  26. using namespace std;
  27.  
  28. int getNumAccidents(string region); //Function Prototype
  29. void findLowest(int n, int s, int e, int w, int c); //Function Prototype
  30.  
  31. int main()
  32. {
  33. //
  34. //Displaying division and calling function for north
  35. cout << "\tNorth Region";
  36. int north = getNumAccidents("North"); //adding value of the function to
  37. //int.
  38. //
  39. //Displaying division and calling function for south
  40. cout << "\tSouth Region";
  41. int south = getNumAccidents("South"); //adding value of the function to
  42. //int.
  43. //
  44. //Displaying division and calling function for east
  45. cout << "\tEast Region";
  46. int east = getNumAccidents("East"); //adding value of the function to
  47. //int.
  48. //
  49. //Displaying division and calling function for west
  50. cout << "\tWest Region";
  51. int west = getNumAccidents("West"); //adding value of the function to
  52. //int.
  53. //
  54. //Displaying division and calling function for central
  55. cout << "\tCentral Region";
  56. int central = getNumAccidents("Central"); //adding value of the function to
  57. //int.
  58. //
  59. //Calling function to determine lowest reported value
  60. findLowest(north, south, east, west, central); //function call
  61. return 0;
  62. }
  63.  
  64. /*******************************************************************************
  65.  * Adding definition to function getNumAccidents. parameter is a string to pass
  66.  * the name of divisions. Will prompt an output to ask user for reported crashes
  67.  * and holds a while loop for invalid validation.
  68.  ******************************************************************************/
  69. int getNumAccidents(string region)
  70. {
  71. int accidents; //Input for reported accidents
  72. cout << "\nReported Accidents Within Last Year: ";
  73. cin >> accidents;
  74. cout << endl;
  75. //While loop for input validation of accidents
  76. while (accidents < 0) //accidents cannot be less than 0
  77. {
  78. cout << "\nInvalid Input, please enter a number greater than 0.";
  79. cout << "\nReported Accidents Within Last Year: ";
  80. cin >> accidents;
  81. cout << endl;
  82. }
  83. return accidents; //Returns value of accidents
  84. }
  85.  
  86. /*******************************************************************************
  87.  * Add definition to function findLowest. Will determine which region was
  88.  * reported with the lowest value and then display both the region and their
  89.  * value.
  90.  ******************************************************************************/
  91. void findLowest(int n, int s, int e, int w, int c)
  92. {
  93. int lowest = n; //Assigns lowest value to north
  94. string lowestregion = "North"; //Assigns lowestregion to North
  95. //
  96. //If statement initialized for determining lowest region
  97. if (s < lowest) //If division is lowest, continue with following statement
  98. {
  99. lowest = s; //Replaces lowest value
  100. lowestregion = "South"; //Replaces lowest region
  101. }
  102. if (e < lowest)//If division is lowest, continue with following statement
  103. {
  104. lowest = e; //Replaces lowest value
  105. lowestregion = "East"; //Replaces lowest region
  106. }
  107. if (w < lowest)//If division is lowest, continue with following statement
  108. {
  109. lowest = w; //Replaces lowest value
  110. lowestregion = "West"; //Replaces lowest region
  111. }
  112. if (c < lowest)//If division is lowest, continue with following statement
  113. {
  114. lowest = c; //Replaces lowest value
  115. lowestregion = "Central"; //Replaces lowest region
  116. }
  117. //
  118. //Output results
  119. cout << "Region with Lowest Reported Accidents: " << lowestregion;
  120. cout << "\nAccident Figure: " << lowest;
  121. }
Success #stdin #stdout 0s 5320KB
stdin
2 3 7 10 9
stdout
	North Region
Reported Accidents Within Last Year: 
	South Region
Reported Accidents Within Last Year: 
	East Region
Reported Accidents Within Last Year: 
	West Region
Reported Accidents Within Last Year: 
	Central Region
Reported Accidents Within Last Year: 
Region with Lowest Reported Accidents: North
Accident Figure: 2