Hi there, I am using a different compiler now, Codeblocks, because I want to try a new environment..
anyways, here's the problem.
whenever I try to run my code in cblocks,
#include <iostream>
#include <conio.h>
usingnamespace std;
int main()
{
int i, j;
int number = 0;
int table[2][2];
for (i = 0; i < 3;){
for (j = 0; j < 3; j++){
cout<<number<<" j = "<<j<<" i = "<<i<<endl;
table [i][j] =number;
number++;
}
i++;
cout<<i<<endl;
}
char y;
y = getch();
return 0;
}
the variable "i" at the end of the code is equal to 6 instead of my expected output of 2.
I don't understand the output.. lol
So I have decided to use my dev c++ compiler, and it works fine.. But I want use Cblocks because I need some functions on it in my project..
table is a 2x2 array. Array indexing starts at 0 so when you access table[2][0], table[2][1], table[2][2], table[1][2] and table[0][2] you are out of bounds. If you want 3x3 elements in table you should declare it as int table[3][3];.
If your program is incorrect you can't expect it to work correctly.
The difference is probably because the compilers put the variables at different locations in memory. When you write to the array out of bounds you overwrite memory outside the array. If a variable happens to be stored at that memory location it's value will be changed.