fork download
  1. // Gage Alvarez CS1A Chapter 6 P. 370, 4
  2.  
  3. /**************************************************************
  4.  *
  5.  * DETERMINE MINIMAL ACCIDENTS AREA
  6.  * ____________________________________________________________
  7.  * Description
  8.  * Program determines the safest region of a major city between north, south, east, west, and central.
  9.  *
  10.  * This program accepts the number of reported accidents for several regions of
  11.  * a city. The program reports the region with the smallest number of reported
  12.  * accidents and the number of accidents in that region.
  13.  * ____________________________________________________________
  14.  * INPUT
  15.  * regions: number of regions in the given city
  16.  * accidents: the number of accidents in the given city
  17.  *
  18.  * OUTPUT
  19.  * direction: the region with the lowest number of accidents
  20.  * lowest: the number of accidents in the above region
  21.  *
  22.  **************************************************************/
  23. #include <iostream>
  24. #include <vector>
  25. using namespace std;
  26.  
  27. int getNumAccidents(string region);
  28. void findLowest(int north, int south, int west, int east, int central);
  29.  
  30. int main() {
  31. //Data Dictionary
  32. int accidentsN;
  33. int accidentsS;
  34. int accidentsW;
  35. int accidentsE;
  36. int accidentsC;
  37. //Input
  38. accidentsN = getNumAccidents("northern");
  39. accidentsS = getNumAccidents("southern");
  40. accidentsW = getNumAccidents("western");
  41. accidentsE = getNumAccidents("eastern");
  42. accidentsC = getNumAccidents("central");
  43. //Processs
  44. findLowest(accidentsN, accidentsS, accidentsW, accidentsE, accidentsC);
  45.  
  46. return 0;
  47. }
  48.  
  49. int getNumAccidents(string region)
  50. {
  51. //This function accepts a number of accidents(validated to be greater than 0)
  52. // for a specified region, and returns that number to the calling program.
  53. int numAccidents;
  54. cout << "Enter the number of accidents for the " << region << " region" << endl;
  55. cin >> numAccidents;
  56. while (numAccidents < 0) {
  57. cout << "Invalid input, number of accidents cannot be less than 0" << endl;
  58. cin >> numAccidents;
  59. }
  60. return numAccidents;
  61.  
  62. }
  63.  
  64. void findLowest(int north, int south, int west, int east, int central) {
  65. int lowest;
  66. lowest = north;
  67. if (lowest <= north)
  68. if (lowest <= south)
  69. if (lowest <= west)
  70. if (lowest <= east)
  71. if (lowest <= central)
  72. lowest = lowest;
  73. else
  74. lowest = central;
  75. else
  76. lowest = east;
  77. else
  78. lowest = west;
  79. else
  80. lowest = south;
  81. else
  82. lowest = north;
  83.  
  84. cout << "Line #83: " << lowest;
  85. // vector<int> allAccidents {north, south, west, east, central};
  86. // vector<string> directions {"north", "south", "west", "east", "central"};
  87. // int lowest = allAccidents[0];
  88. // string direction = directions[0];
  89. // for (int i = 0; i < allAccidents.size(); i++) {
  90. // if (allAccidents[i] < lowest) {
  91. // lowest = allAccidents[i];
  92. // direction = directions[i];
  93. // }
  94. // }
  95. // cout <<"the " << direction << " had the least accidents, with only "
  96. // << lowest << " accidents." << endl;
  97. }
Success #stdin #stdout 0.01s 5280KB
stdin
-15
34
64
12
11
123
stdout
Enter the number of accidents for the northern region
Invalid input, number of accidents cannot be less than 0
Enter the number of accidents for the southern region
Enter the number of accidents for the western region
Enter the number of accidents for the eastern region
Enter the number of accidents for the central region
Line #83: 12