Why are these different?

R

Rhybec

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
 
J

Josh Sebastian

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
 
D

Darrell Grainger

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
 
M

Martin Ambuhl

Rhybec said:
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].
 
D

Dik T. Winter

> 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.
 
D

Dik T. Winter

> In article said:
> > 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.
 
P

Peter Shaggy Haywood

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"?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top