# Basic Function Problem

• 08-16-2009
AJOHNZ
Basic Function Problem
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;         } }```
• 08-16-2009
robwhit
think arrays.

have n, e, s, w, and c elements of an array, and have constants (or enums) of the same name to index it. then you can just loop through the array in findLowest to find the lowest element.
• 08-16-2009
scwizzo
Yea those if statements are kind of a space hog, even though it does the trick.

consider something like this...

Code:

```// return value is the index of the array that has the lowest accidents, or number int findLowest(int* accidentsPerArea, int sizeOfParamOne) {     int lowestIndex = 0xFFFFFFFF; // One big arse number     for(int i = 0; i < sizeOfParamOne; i++){         //if the number in the next index point is lower than lowestIndex then assign it     }     //return the index pertaining to the lowest accident count; }```
edit: robwhit beat me.
• 08-16-2009
Sebastiani
Quote:

Originally Posted by scwizzo
Yea those if statements are kind of a space hog, even though it does the trick.

consider something like this...

Code:

```// return value is the index of the array that has the lowest accidents, or number int findLowest(int* accidentsPerArea, int sizeOfParamOne) {     int lowestIndex = 0xFFFFFFFF; // One big arse number     for(int i = 0; i < sizeOfParamOne; i++){         //if the number in the next index point is lower than lowestIndex then assign it     }     //return the index pertaining to the lowest accident count; }```
edit: robwhit beat me.

There's no need to set lowestIndex to a high value - just plain 0 works fine.
• 08-16-2009
AJOHNZ
if it's set to 0 then the number it's compared to won't fit the condition of being less than it so it will never be assigned to the lowest value. or will it? do you get what i'm saying though?
• 08-16-2009
Sebastiani
Quote:

Originally Posted by AJOHNZ
if it's set to 0 then the number it's compared to won't fit the condition of being less than it so it will never be assigned to the lowest value. or will it? do you get what i'm saying though?

No, in the example he posted the return value is an index into the array. If it's the value itself you want to return, simply initialize the return value to the first element in the array (perhaps checking that the array size is not zero, beforehand).