Mail Archives: djgpp/2001/03/04/05:21:29
Anderson wrote:
> I was playing with arrays (for matrix calculation) when I've got this
> strange error. When I set the value it seems ok, but when I check it
> out... it's not there anymore!
> Here's the bugged code:
> ----------------------------------------
> int X=2;
> int Y=2;
> for (int i=0; i<X ; i++){
> for (int j=0; j<Y ; j++){
> if (i==j) data[i,j]=1; // defines a 2x2 identity matrix
> else data[i,j]=0;
> cout << data [i,j] <<", "; // SEEMS OK...
As Alexei Frounze has already noted, you should be using data[i][j],
not data[i,j]. data[i,j] means the same as data[j]. If you can't see
why, try looking up "comma operator" in your favourite C or C++ book.
Always compile with warnings enabled (at least -Wall and -W).
Had you done so, the compiler would have pointed out this mistake.
- Raw text -