 # newbie loop c++. please explain efficient looping

I have this code where a 4x5 array receives 20 different numbers and as the program runs it will find the largest and smallest number in the series.

``````int largest = 0;
int smallest = 0;
int a;

for(int x = 0; x < 4; x++)
for(int y = 0; y < 5; y++)
{
cin >> a[x][y];
}

largest = a;
smallest = a;
for(int x = 0; x < 4; x++)
for(int y = 0; y < 5; y++)
{
if(largest < a[x][y])
largest = a[x][y];
if(smallest > a[x][y])
smallest = a[x][y];
}

cout << "largest: " << largest << endl;
cout << "smallest: " << smallest << endl;

return 0;
``````

In this particular code:

for(int x = 0; x < 4; x++)

for(int y = 0; y < 5; y++)

{

if(largest < a[x][y])

largest = a[x][y];

if(smallest > a[x][y])

smallest = a[x][y];

}

in our class, our prof said that this program will loop for like 20 times(4x5), then how can we make this loop efficient that it will run lesser than 20 times but still get the same result. He was saying something about efficient looping. Tips and suggestions pls? and another thing, he said that we cannot use advance constructs so im thinking this is something about manipulating the loop.

Well…you might just find the largest as well the smallest while taking input only.
As you input an integer…check it against the largest and smallest found yet.
like…

``````largest = -100000;   //large negative values considered to be minimum found till yet
smallest = 100000;   //large positive value considered to be maximum found till yet
for(int x = 0; x < 4; x++)
for(int y = 0; y < 5; y++)
{
cin >> a[x][y];
if(largest < a[x][y])
largest = a[x][y];
if(smallest > a[x][y])
smallest = a[x][y];
}
``````

Instead of running the nested loops twice…you can achieve the result in running the nested loops once.
It will get the no of iterations from 40 to 20 in the complete program.(almost)
Hope it helps!

1 Like

Your program must do at least 20 operations. The thing is that you can get maximum efficiency if you use 1 loop instead of 2

``````for (int i=0; i<20; i++)
``````

and not even store the numbers on the array:

``````int min=2000000000, max = -2000000000, num;
for (int i=0; i<20; i++)
{
cin >> num;
if (num < min) num = min;
if (num > max) max = num;
}
``````

hope it helps!

2 Likes

But before you had 20 iterations with one operations + 20 iterations with 2 operations (`if`), in your approach there are 20 iterations with 3 operations = not really improved…

You can also notice, that you are not updating `smallest` and `largest` in one iteration (except first one), but again, from my point of view adding `else` is not helping…

btw: your smallest initialization (`smallest = -100000`) is wrong

Thanku @betlista …edited it.

I might have not implied it correctly but our prof was just pointing at the code

`````` for(int x = 0; x < 4; x++)
for(int y = 0; y < 5; y++)
{
if(largest < a[x][y])
largest = a[x][y];
if(smallest > a[x][y])
smallest = a[x][y];
}
``````

its true that from 40 iterations it became 20 iterations, but I need a suggestion regarding only for the code above. Wherein i have to get the same result, but the iterations will be less than 20, is it possible?

From my point of view this is “advanced construct”, your solution for bigger matrix will be quicker, just saying…