Click here to Skip to main content
15,889,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I am a beginner in C++. I have question about nested loop. I want to generate a list of coordinates (x,y,z) from 1 to N.

This is the code that I have tried, but it doesnt give the correct coordinates like (0,0,0) (1,0,0) and so on. I am struggling to put the coordinates into array list of X[] Y[] and Z[]. Can someone please give me hint on what is going wrong?

What I have tried:

int count=1;


for ( int i = 0; i < size ; i++)
{ for ( int j = 0; j < size ; j++)
{ for ( int k = 0; k < size; k++) // I want to put the value of i,j,k into arraylist from 1 to N for X[], Y[] and Z[]
{ X[count] = k;
Z[count] = i;
Y[count] = j;



count++;
}

}

}




for ( int count = 1; count<=N; count++)
{ cout << count << setw(6)<< X[count] <<Y[count]<<Z[count]<<endl;
}
Posted
Updated 22-Mar-17 4:15am
Comments
Sunasara Imdadhusen 22-Mar-17 9:44am    
Not clear at all!
Patrice T 22-Mar-17 14:45pm    
Post code that can be compiled.

1 solution

Its not clear what your sequence is .. but if you look at this

C++
{ for ( int k = 0; k < size; k++) // I want to put the value of i,j,k into arraylist from 1 to N for X[], Y[] and Z[]
{ X[count] = k;
Z[count] = i;
Y[count] = j;}


vs what you have written, should it not be

C++
...
{ for ( int k = 0; k < size; k++) // I want to put the value of i,j,k into arraylist from 1 to N for X[], Y[] and Z[]
{ X[count] = i;
Y[count] = j;
Z[count] = k;}


you also need to learn how to use debugger and to step through your program, which by then observing the values of the variables etc would show you what is/isnt happening
 
Share this answer
 
v2
Comments
CPallini 22-Mar-17 11:10am    
5.
Member 12985257 22-Mar-17 12:33pm    
Sorry for not explaining clearly on the problem. When we created a nested loop, if let say the size = 2; with this code:
for ( int i = 0; i < size ; i++)
{ for ( int j = 0; j < size ; j++)
{ for ( int k = 0; k < size; k++)
{ cout << i << j << k << endl;



count++;
}

}

}

Output will be:
000
100
010
110
001
101
011
111

But, what I want is something like this, where the index value is stored into an array.
Element X[] Y[] Z[]
1 0 0 0
2 1 0 0
3 0 1 0
4 1 1 0
5 0 0 1
6 1 0 1
7 0 1 1
8 1 1 1
Member 12985257 22-Mar-17 17:27pm    
Thanks everyone, I now know where the mistake come from.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900