×

# newbie loop c++. please explain efficient looping

 0 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[4][5]; for(int x = 0; x < 4; x++) for(int y = 0; y < 5; y++) { cin >> a[x][y]; } largest = a[0][0]; smallest = a[0][0]; 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. asked 11 Nov '14, 18:39 135●1●6●19 accept rate: 28%

 1 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! answered 11 Nov '14, 19:42 26 accept rate: 100% 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 (11 Nov '14, 19:55) Thanku @betlista ..edited it. (11 Nov '14, 21:18) 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? (11 Nov '14, 21:50)
 2 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! answered 11 Nov '14, 21:57 46●1 accept rate: 0% From my point of view this is "advanced construct", your solution for bigger matrix will be quicker, just saying... (11 Nov '14, 23:25)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,918
×191

question asked: 11 Nov '14, 18:39

question was seen: 1,504 times

last updated: 11 Nov '14, 23:25