foolish compiler?!

hi.
Why is it that zero (0) is output after running
1
2
3
4
5
6
7
8
.........
      k=1;
      i=1;
    for(;i<201;++i)  
     for(;k<201;++k)
      A[i][k]=1000000;  
cout<<A[112][23];   
...................
Last edited on
At line 5, change for(;k <= 201 to for(k = 1;k <= 201

Now the code should work, see explanation below. Sorry for what I have said earlier.
Last edited on
inner loop will only run once, next time round k==201 already because it wasnt reset.
1
2
3
4
5
6
7
8
.........
     // k=1;
     // i=1;
    for(i=1;i<201;++i)        <<< init i
     for(k=1;k<201;++k)    <<< init k
      A[i][k]=1000000;  
cout<<A[112][23];   
...................
Why just once?
Weren't they initialized outside the loop?
Last edited on
Think about what value k will have at the end of the first time your inner loop runs (when i is 1).

Think about what value k will have at the start of the second time your inner loop runs (when i is 2).

Think about how many times your inner loop will run when i is 2.
Last edited on
Weren't they initialized outside the loop?

yes, but they should have been initialised outside "their own" loops, which would put k's initialisation INSIDE i's loop.
Topic archived. No new replies allowed.