1

This is the situation: I have written a program that given two arrays r and phi calculates the coordinates x,y,z and prints the on a file i this way:

 for(int i=0; i< sizeof(r)/sizeof(r[0]); i++){ for(int j=0;j< sizeof(phi)/sizeof(phi[0]);j++){ z=atan(theta)*r[i]*cos(phi[j]); y=r[i]*sin(phi[j]); x=r[i]*cos(phi[j]); fprintf(file,"%g \t %g \t %g\n", x,y,z)}} 

Now I need to consider the coordinates as arrays x[],y[],z[], but I cannot find a way to fill the arrays with my current program. The problem is that I cannot find a way to properly iterate on the indices of the coordinates: for example, if I consider the index i*j I will get 0 j-times on the first iteration of i and so on.

Can anyone help?

1 Answer 1

3

Create a separate counter for the resulting arrays and increment it on each iteration of the inner loop.

int k = 0; for(int i=0; i< sizeof(r)/sizeof(r[0]); i++){ for(int j=0;j< sizeof(phi)/sizeof(phi[0]);j++){ z[k]=atan(theta)*r[i]*cos(phi[j]); y[k]=r[i]*sin(phi[j]); x[k]=r[i]*cos(phi[j]); fprintf(file,"%g \t %g \t %g\n", x[k],y[k],z[k]) k++; } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.