如何将嵌套循环的索引存储到坐标xyz的arraylist中 [英] How to store index of nested loop into arraylist of coordinates xyz

查看:153
本文介绍了如何将嵌套循环的索引存储到坐标xyz的arraylist中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







我是C ++初学者。我对嵌套循环有疑问。我想生成一个从1到N的坐标(x,y,z)列表。



这是我试过的代码,但它没有给出正确的坐标如(0,0,0)(1,0,0)等。我正在努力将坐标放入X [] Y []和Z []的数组列表中。有人可以给我一些关于出了什么问题的提示吗?



我的尝试:



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,j,k的值放入arraylist中,从1到N为X [],Y []和Z []

{X [count] = k;

Z [count] = i;

Y [count] = j;







count ++;

}



}



}









for(int count = 1; count< = N;计数++)

{cout<<计数<<运输及工务局局长(6)<< X [count]<< Y [count]<< Z [count]<< endl;

}

解决方案

< blockquote>不清楚你的序列是什么..但如果你看这个



 { for  int  k =  0 ; k< size; k ++) //  我想将i,j,k的值放入arraylist中,从1到N为X [],Y []和Z []  
{X [count] = k;
Z [count] = i;
Y [count] = j;}





vs你所写的,如果不是



 ... 
{ for int k = 0 ; k< size; k ++) // < span class =code-comment>我想将i,j,k的值放入arraylist中,从1到N为X [],Y []和Z []
{ X [count] = i;
Y [count] = j;
Z [count] = k;}





你还需要学习如何使用调试器和逐步完成你的程序,然后通过观察变量的值等来显示你正在发生的事情


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;
}

解决方案

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

{ 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

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


这篇关于如何将嵌套循环的索引存储到坐标xyz的arraylist中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆