fork download
  1. // Gage Alvarez CS1A Chapter 6 P. 370, 4
  2.  
  3. /**************************************************************
  4.  *
  5.  * Determine Safest Area
  6.  * ____________________________________________________________
  7.  * Description
  8.  * Program determines the safest region of a major city between north, south, east, west, and central.
  9.  * ____________________________________________________________
  10.  * INPUT
  11.  * city: the city that is being investigated
  12.  * accidents: the number of accidents in the given city
  13.  *
  14.  * OUTPUT
  15.  * direction: the direction with the lowest accident total
  16.  * lowest: the lowest number of accidents out of all directions
  17.  *
  18.  **************************************************************/
  19. #include <iostream>
  20. #include <vector>
  21. using namespace std;
  22.  
  23. int getNumAccidents(string region);
  24. void findLowest(int north, int south, int west, int east, int central);
  25.  
  26. int main() {
  27. //Data Dictionary
  28. int accidentsN;
  29. int accidentsS;
  30. int accidentsW;
  31. int accidentsE;
  32. int accidentsC;
  33. //Input
  34. accidentsN = getNumAccidents("northern");
  35. accidentsS = getNumAccidents("southern");
  36. accidentsW = getNumAccidents("western");
  37. accidentsE = getNumAccidents("eastern");
  38. accidentsC = getNumAccidents("central");
  39. //Processs
  40. findLowest(accidentsN, accidentsS, accidentsW, accidentsE, accidentsC);
  41.  
  42. return 0;
  43. }
  44.  
  45. int getNumAccidents(string region) {
  46. int numAccidents;
  47. cout << "Enter the number of accidents for the " << region << " region" << endl;
  48. cin >> numAccidents;
  49. while (numAccidents < 0) {
  50. cout << "Invalid input, number of accidents cannot be less than 0";
  51. cin >> numAccidents;
  52. }
  53. return numAccidents;
  54.  
  55. }
  56.  
  57. void findLowest(int north, int south, int west, int east, int central) {
  58. vector<int> allAccidents {north, south, west, east, central};
  59. vector<string> directions {"north", "south", "west", "east", "central"};
  60. int lowest = allAccidents[0];
  61. string direction = directions[0];
  62. for (int i = 0; i < allAccidents.size(); i++) {
  63. if (allAccidents[i] < lowest) {
  64. lowest = allAccidents[i];
  65. direction = directions[i];
  66. }
  67. }
  68. cout <<"the " << direction << " had the least accidents, with only "
  69. << lowest << " accidents." << endl;
  70. }
Success #stdin #stdout 0.01s 5272KB
stdin
-15
34
64
12
91
123
stdout
Enter the number of accidents for the northern region
Invalid input, number of accidents cannot be less than 0Enter 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
the west had the least accidents, with only 12 accidents.