Both
logic and
data do affect the result.
We do not know what you have in missed.
We do not know how the found relates to the missed.
I can guess data that would produce four 3's with your logic, but that data would print other values too.
A loop is a statement that executes some statements multiple times (could be 0 times too).
1 2 3 4 5
|
int i = 0;
while ( i < tot ) {
// statementX;
i++;
}
|
The statementX is repeated tot times, (unless it modifies the i).
Think of a fire fighter. She has to to check every room in a building. She will:
Visit every floor of the building
Visit every apartment of current floor
Visit every room of current apartment
Do the check in current room |
That was not a double nested loop. It is triple.
ne555 wrote:
for i = 0 to n:
if not is_member(i, missed)
print(i) |
That is a double nested loop, because the is_member has a loop.
That has also different logic than your OP code.
This has same logic as you had:
1 2 3 4 5 6 7 8 9
|
int i = 0;
while ( i < tot ) {
int count = std::count( missed, missed+found, i );
int other = found - count;
for ( int j = 0; j < other; j++) {
cout << i << endl;
}
++i;
}
|
The std::count is a loop too.
You could write the loop yourself, but when you have the inner loop within a function (like std::count or is_missed) the logic of your outer loop is easier to see and within a function it is easier to see what it does (preferably one thing only).
PS. Term "scope" has very specific meaning in C++ parlance.