delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/03/04/05:21:29

From: dontmailme AT iname DOT com (Steamer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: BUG: Code or Compiler?
Date: Sun, 04 Mar 2001 10:18:44 GMT
Organization: always disorganized
Lines: 21
Message-ID: <3aa2166d.1519015@news.freeserve.net>
References: <3AA195B1 DOT ED50BFD7 AT __mad DOT scientist DOT com-->
NNTP-Posting-Host: modem-9.minnesota.dialup.pol.co.uk
X-Trace: news5.svr.pol.co.uk 983701125 813 62.137.74.9 (4 Mar 2001 10:18:45 GMT)
NNTP-Posting-Date: 4 Mar 2001 10:18:45 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019