scope of double nested for loop

Jun 28, 2019 at 1:24am
Hi, I am a little confused about the scope of double nested loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int i = 0;

while (i < tot){
    
  for (int j = 0; j < found; j++){
           
    if (i != missed[j]){
       cout << i << endl;
    }
        
  }
       
 i++;
    
}


It's repeating the printing of i the number of -found- times.
ie. int found = 4
i = 3
3
3
3
3

Is there a way to only cout it once?

Thank you.
Jun 28, 2019 at 1:43am
you need to plan before coding
start with some pseudocode
1
2
3
for i = 0 to n:
   if not is_member(i, missed)
      print(i)
now explode the functions
1
2
3
4
5
is_member(x, array)
   for elem in array
      if x == elem
         return true
   return false
now traduce to c++
1
2
3
4
5
6
bool is_member(int x, int array[], int size){
   for(int K=0; K<size; ++K)
      if(x==array[K])
         return true;
   return false;
}


> ie
e.g.
Last edited on Jun 28, 2019 at 1:45am
Jun 28, 2019 at 1:53am
ne555

Hey!! Thanks for the input! That's definitely very sound and useful advice that I will most certainly use, but, and I am trying my best not to sound like a prick here, but....

I know that using a double nested loop isn't the best way to do what the code needs doing, and I actually already finished the project another way. I simply like to take note of areas I struggled in during my projects, and try to strengthen those areas in my coding.

I am specifically needing to understand double nested for loops better.

I see how I wasn't very clear on that though.
Jun 28, 2019 at 5:34am
I am specifically needing to understand double nested for loops better.


They're hard to understand, hence why @ne555 has steered you away from them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int i = 0; i < total; ++i)
{
  bool found = false; 
  for (int j = 0; j < found; ++j)
  {
    if (i == member[j]) 
    {
      found = true;
      break;
    }
  }

  if (! found)
    std::cout << i << '\n';
}
Last edited on Jun 28, 2019 at 5:34am
Jun 28, 2019 at 6:17am
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.
Jun 28, 2019 at 2:37pm
Consider me well schooled! =)

Turns out, my intended question was being addressed and I didn't even know it.

Thanks ya'll.
Topic archived. No new replies allowed.