Velocity Reviews > Why are these different?

# Why are these different?

Rhybec
Guest
Posts: n/a

 10-13-2003
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[i]-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
fprintf(Rfile,"%f %f %f\n",X[i],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[i]-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[i],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

Josh Sebastian
Guest
Posts: n/a

 10-13-2003
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[i]-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
> fprintf(Rfile,"%f %f %f\n",X[i],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

Darrell Grainger
Guest
Posts: n/a

 10-13-2003
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[i]-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
> fprintf(Rfile,"%f %f %f\n",X[i],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[i][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[i]-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[i],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 http://www.velocityreviews.com/forums/(E-Mail Removed)

Martin Ambuhl
Guest
Posts: n/a

 10-13-2003
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[i]-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[i][j], then write R[i][j].

--
Martin Ambuhl

Dik T. Winter
Guest
Posts: n/a

 10-13-2003
In article <(E-Mail Removed) > (E-Mail Removed) (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[i]-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
> fprintf(Rfile,"%f %f %f\n",X[i],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
Guest
Posts: n/a

 10-13-2003
In article <(E-Mail Removed)> "Dik T. Winter" <(E-Mail Removed)> writes:
> In article <(E-Mail Removed) > (E-Mail Removed) (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[i]-Xc,2)+pow(Y[j]-Yc,2))/(2*sig));
> > fprintf(Rfile,"%f %f %f\n",X[i],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/

Peter Shaggy Haywood
Guest
Posts: n/a

 10-17-2003
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[i]-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[i],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[i]-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[i],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!

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