c ++中的多维数组 [英] Multidimensional array in c++
本文介绍了c ++中的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的目标是在c ++中创建一个多维数组。问题是,当我运行我的代码时,它将整个数组显示为一列,但我希望它将其显示为一个表。请任何人都可以帮我解决这些问题。谢谢。
My goal is to make a create a multidimensional array in c++. The problem is when i run my code it displays the whole array as a column but i want it to display it as a table. Please can anyone help me with the code. Thanks.
int array [3] [3];
for (int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
cout<<"Enter your array values:"<<endl;
cin>> array[i] [j];
}
}
for (int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
cout<<array[i] [j]<<endl;
}
}
推荐答案
要将其显示为表格,您可以像这样修改您的cout
To Display it as a table you can modify your cout like this
for (int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
cout<<array[i][j]<<" ";
}
cout<<endl;
}
我不确定你的意思是bij显示为一列。
但如果你的意思是一切显示在彼此之下,然后你就会这样做:
for(int i = 0; i< 3; i ++)
{
for(int j = 0; j <3; j ++)
{
cout<<阵列[i] [j]; //不要把你的行尾放在这里
cout<< (J = 2!) :? //为您的值插入分隔符
}
cout<< ENDL; //而是把它放在这里,所以每个行都以新行开头
}
您可能还需要交换i和j,具体取决于您认为您的行是什么:第一个或第二个索引。
I'm not sure what you mean bij displays as a column.
But if you mean everything is shown underneath each other then you shoule do:
for (int i = 0; i<3; i++)
{
for(int j = 0; j<3; j++)
{
cout << array[i][j]; // don't put you end-of-line here
cout << (j!=2)?",":""; // insert a delimiter for your values
}
cout << endl; // but instead put it here, so each "row" starts as a new line
}
you may also need to exchange the i and j, depending on what you think your row is: the first or second index.
提供值之间的间距
Provide the spacing between values
for(int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout<<array[i][j]<<"\t";
}
cout<<endl;
}
这篇关于c ++中的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文