Why are these different?

Discussion in 'C Programming' started by Rhybec, Oct 13, 2003.

  1. Rhybec

    Rhybec Guest

    Can anyone tell me why this works:


    for (i=0;i<nx;i++) {
    for (j=0;j<ny;j++) {
    R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
    fprintf(Rfile,"%f %f %f\n",X,Y[j],R[i,j]);
    }
    }

    And this does not?

    for (i=0;i<nx;i++) {
    for (j=0;j<ny;j++) {
    R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
    }
    }
    for (i=0;i<nx;i++) {
    for (j=0;j<ny;j++) {
    fprintf(Rfile,"%f %f %f\n",X,Y[j],R[i,j]);
    }
    }

    I may be missing something, but I have been perplexed for hours. The
    second example outputs a completly wrong value for R.

    Thanks,
    Rob
     
    Rhybec, Oct 13, 2003
    #1
    1. Advertising

  2. On Sun, 12 Oct 2003 19:27:01 -0700, Rhybec wrote:

    > Can anyone tell me why this works:
    >
    >
    > for (i=0;i<nx;i++) {
    > for (j=0;j<ny;j++) {
    > R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
    > fprintf(Rfile,"%f %f %f\n",X,Y[j],R[i,j]);
    > }
    > }


    Maybe if you broke that down into simpler statements, you'd see the
    problem. My guess, though, is that it has something to do with R[i, j]
    being a mistake. This isn't Pascal.

    Josh
     
    Josh Sebastian, Oct 13, 2003
    #2
    1. Advertising

  3. On Sun, 12 Oct 2003, Rhybec wrote:

    > Can anyone tell me why this works:
    >
    >
    > for (i=0;i<nx;i++) {
    > for (j=0;j<ny;j++) {
    > R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
    > fprintf(Rfile,"%f %f %f\n",X,Y[j],R[i,j]);
    > }
    > }


    I think you miss understand the syntax of R[i,j]. This does not mean a two
    dimensional array. For a two dimensional array it would be R[j]. The
    syntax you have here has the same results as using R[j]. So you are
    assigning values to R[j] nx times. In the above loop you are assigning it
    to R[j], printing it. So the information gets saved to Rfile before you
    destroy it.

    In the loop below the last iteration of the outer loop destroys the
    previous iteration. So only when i == nx-1 does the information remain.

    > And this does not?
    >
    > for (i=0;i<nx;i++) {
    > for (j=0;j<ny;j++) {
    > R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
    > }
    > }
    > for (i=0;i<nx;i++) {
    > for (j=0;j<ny;j++) {
    > fprintf(Rfile,"%f %f %f\n",X,Y[j],R[i,j]);
    > }
    > }
    >
    > I may be missing something, but I have been perplexed for hours. The
    > second example outputs a completly wrong value for R.
    >
    > Thanks,
    > Rob
    >


    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
     
    Darrell Grainger, Oct 13, 2003
    #3
  4. Rhybec wrote:
    > Can anyone tell me why this works:


    I don't know what "works" means in this context, since I doubt that

    > for (i=0;i<nx;i++) {
    > for (j=0;j<ny;j++) {
    > R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));

    ^^^^^^
    (etc) is what you really mean. R[i,j] is the same as R[j] (apart from side
    effects of evaluating i). If you mean R[j], then write R[j].


    --
    Martin Ambuhl
     
    Martin Ambuhl, Oct 13, 2003
    #4
  5. In article <> (Rhybec) writes:
    > Can anyone tell me why this works:
    > for (i=0;i<nx;i++) {
    > for (j=0;j<ny;j++) {
    > R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
    > fprintf(Rfile,"%f %f %f\n",X,Y[j],R[i,j]);
    > }
    > }

    ....

    How is R declared? Did you allocate enough space for it? I think
    that R[i,j] at some stage points outside allocated memory.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Oct 13, 2003
    #5
  6. In article <> "Dik T. Winter" <> writes:
    > In article <> (Rhybec) writes:
    > > Can anyone tell me why this works:
    > > for (i=0;i<nx;i++) {
    > > for (j=0;j<ny;j++) {
    > > R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
    > > fprintf(Rfile,"%f %f %f\n",X,Y[j],R[i,j]);
    > > }
    > > }

    > ...
    >
    > How is R declared? Did you allocate enough space for it? I think
    > that R[i,j] at some stage points outside allocated memory.


    Forgetting of course that R[i,j] is not what is wanted.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Oct 13, 2003
    #6
  7. Groovy hepcat Rhybec was jivin' on 12 Oct 2003 19:27:01 -0700 in
    comp.lang.c.
    Why are these different?'s a cool scene! Dig it!

    >Can anyone tell me why this works:


    Not until you tell us A) what i is, B) what j is, C) what nx is, D)
    what ny is, E) what R is, F) what A is, G) what X is, H) what Y is, I)
    what Xc is, J) what Yc is, K) what sig is, L) what Rfile is and M)
    what "works" means.

    >for (i=0;i<nx;i++) {
    > for (j=0;j<ny;j++) {
    > R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));


    Others have pointed out that the expression R[i,j] contains a
    meaningless evaluation of i, and is equivalent to R[j], so I won't
    mention it. :)

    > fprintf(Rfile,"%f %f %f\n",X,Y[j],R[i,j]);
    > }
    >}
    >
    >And this does not?


    In what way does this not work? You must define "works" and "does
    not work" in detail.

    >for (i=0;i<nx;i++) {
    > for (j=0;j<ny;j++) {
    > R[i,j]=-1*A*exp(-1*(pow(X-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
    > }
    >}
    >for (i=0;i<nx;i++) {
    > for (j=0;j<ny;j++) {
    > fprintf(Rfile,"%f %f %f\n",X,Y[j],R[i,j]);
    > }
    >}
    >
    >I may be missing something, but I have been perplexed for hours. The
    >second example outputs a completly wrong value for R.


    What is the right answer, then? You must define the problem
    properly, otherwise noone can help you. Post the smallest *complete*
    program that illustrates the problem. Tell us exactly what it is
    supposed to do (and how, if that is not absolutely obvious) and
    exactly what it actually does.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Oct 17, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Lilja
    Replies:
    9
    Views:
    379
    Old Wolf
    May 26, 2005
  2. r.e.s.
    Replies:
    6
    Views:
    269
    r.e.s.
    Dec 17, 2003
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    994
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,264
    Smokey Grindel
    Dec 2, 2006
  5. yuricake
    Replies:
    3
    Views:
    121
    Sylvain Joyeux
    May 5, 2007
Loading...

Share This Page