Friday, July 31, 2009

Why am I getting this output?

When I compile the following C++ code using Dev-C++:





#include %26lt;stdio.h%26gt;


#include %26lt;iostream.h%26gt;


using namespace std;


int main()


{


int so[8][8], i , j;


for (i=0;i%26lt;9;i++)


{


for (j=0;j%26lt;9;j++)


{


cout %26lt;%26lt; so[i][j]%26lt;%26lt;" ";


if (j==8) cout %26lt;%26lt; endl;


}


}


return 0;


}





I get:


0 0 2237568 16 2237576 9264 2228600 2228224 72


72 2228224 37878912 2089879280 37879504 126488 2 37879288 2089879275


2089879275 37879532 2089872920 2089878896 -1 2089878893 2009252574 2228224 0


0 2009252579 0 2237497 271256 2009291924 2009145480 -1 2009252814


2009252814 37879496 8 37879776 2009291924 2009145456 -1 2009252579 2009315348


2009315348 2237560 2237488 8 2009312941 0 2147303424 2147332096 56


56 2 37879600 2009198341 37879664 4198445 4268032 4268036 37879656





and so on....


Shouldn't I get this?





0 0 0 0 0 0 0 0


0 0 0 0 0 0 0 0


0 0 0 0 0 0 0 0


0 0 0 0 0 0 0 0


0 0 0 0 0 0 0 0


0 0 0 0 0 0 0 0


0 0 0 0 0 0 0 0


0 0 0 0 0 0 0 0

Why am I getting this output?
you defined the array, you did not initialize it so the values in the cells are what was in memory at that location.
Reply:bdloving is correct. Those figures are the numbers which are in the memory assigned to your array. They are technically known as garbage and you are supposed to initialize your arrays to sweep out all the garbage.





Give bdloving best answer and be careful about sharing your garbage with us. You never know who's looking.
Reply:change { int so[8][8] } to { int so[9][9] }

daylily

No comments:

Post a Comment