my code works fine but it seems like it took an awfully large amount of code to complete this task. is there any way i could shorten this code?
the code is supposed to have 2 functions. one is suposed to ask the user to enter the data for their zone. it is supposed to be called 5 times, once for each zone. the other function is supposed to figure out which zone has least amount and display that. i especially think there is a shorter way to code around all my if statements that i can't think of. please let me know.
here is code:
Code:#include <iostream> #include <string> using namespace std; int getNumAccidents(string); void findLowest(int, int, int, int, int); int main() { string north = "north"; string east = "east"; string south = "south"; string west = "west"; string central = "central"; int n, e, s, w ,c; n = getNumAccidents(north); e = getNumAccidents(east); s = getNumAccidents(south); w = getNumAccidents(west); c = getNumAccidents(central); findLowest(n, e, s, w, c); return 0; } int getNumAccidents(string area) { int accidents; cout << "Enter the number of automobile accidents for " << area << ": "; cin >> accidents; return accidents; } void findLowest(int north, int east, int south, int west, int central) { if (north < east && north < south && north < west && north < central) { cout << "North has least amount of accidents with " << north << endl; } if (east < north && east < south && east < west && east < central) { cout << "East has least amount of accidents with " << east << endl; } if (south < east && south < north && south < west && south < central) { cout << "South has least amount of accidents with " << south << endl; } if (west < east && west < south && west < north && west < central) { cout << "West has least amount of accidents with " << west << endl; } if (central < east && central < south && central < west && central < north) { cout << "North has least amount of accidents with " << north << endl; } }



LinkBack URL
About LinkBacks



