# Why are these different?

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

1. ### RhybecGuest

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

2. ### Josh SebastianGuest

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

3. ### Darrell GraingerGuest

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
4. ### Martin AmbuhlGuest

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
5. ### Dik T. WinterGuest

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
6. ### Dik T. WinterGuest

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
7. ### Peter Shaggy HaywoodGuest

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
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!