Determine Lowest Number

I am working on an assignment that is asking me to determine the smallest number. Since we have not learned about arrays, this will have to be resolved with if statements and/or loops. For the portion that determines the lowest number entered, can the code be structured to be more efficient using loops or other if statement combinations?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void findLowest(int nTotals, int sTotals, int eTotals, int wTotals, int cTotals)
{
	int lowestNum;

	if (nTotals < sTotals && nTotals < eTotals && nTotals < wTotals && nTotals < cTotals)
	{
		lowestNum = nTotals;
		cout << "With " << lowestNum << ", " << REGION_NORTH << " had the lowest number of automobile accidents." << endl;
	}
		else if (sTotals < eTotals && sTotals < wTotals && sTotals < cTotals)
		{
			lowestNum = sTotals;
			cout << "With " << lowestNum << ", " << REGION_SOUTH << " had the lowest number of automobile accidents." << endl;
		}
			else if (eTotals < wTotals && eTotals < cTotals)
			{
				lowestNum = eTotals;
				cout << "With " << lowestNum << ", " << REGION_EAST << " had the lowest number of automobile accidents." << endl;
			}
				else if (wTotals < cTotals)
				{
					lowestNum = cTotals;
					cout << "With " << lowestNum << ", " << REGION_WEST << " had the lowest number of automobile accidents." << endl;
				}
	else
	{
		lowestNum = cTotals;
		cout << "With " << lowestNum << ", " << REGION_CENTRAL << " had the lowest number of automobile accidents." << endl;
	}
	return;
}


Any help is appreciated.
Last edited on
Well, how about creating a function that determines what is the lowest of two integers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int findLowest(int x1, int x2, int x3, int x4, int x5) // I would use ... here if you are allowed, infinite variables
{
   return GetLow(x5, GetLow(x4, GetLow(x3, GetLow(x1, x2)))); // Seems a little condensed but works!
}

int GetLow(int a1, int a2)
{
   if(a1 < a2)
      return a1;
   return a2 // If they are the same, it won't matter then I assume
}

void main()
{
   int x = findLowest(10, 15, 100, 7, 5);
   cout << x;
   getch();
}


Add the headers and declare your functions, that should give you a basis on what to do. There is an infinite variable when you use ... in a function which is how scanf and printf works, but I am not sure you are allowed to use it or not for this but it would make it better so you could add as many or as little variables as you like to make it really interesting, but then you would need a loop since you don't know how many times you need to test for lowest number.
do like this

int min=1000000;

for (int i=0; i<5; i++)
{
int z;
cin >>z;
if (z<min) min = z;
}

cout << min;

So you use loop (for) and if.
Topic archived. No new replies allowed.