c++ - 如何将5个值的数组1D打印为列?

标签 c++ arrays

我有一个一维整数值数组,我需要将其打印为列。
虽然我可以将值打印为列,但可以打印5个以上的值。理想的输出如下所示:

1 1 1
1 1

我该怎么办?
例如,我的尝试是这样的:
int main(){

int array[5] = {1,1,1,1,1};

  cout<<"array printed in the form of columns: "<<endl;

   for ( int i = 0; i < 5; i++ ){

         cout<<setw(3)<< array[i]<<endl<<setw(3)<<array[i];
      }  
    return 0;
}

最佳答案

您可以编写2个循环:

for ( int i = 0; i < 3; ++i ) {
         cout << setw(3) << array[i];   // first row
}

cout << endl;       // next row

for ( int i = 3; i < 5; ++i ) {
         cout << setw(3) << array[i];   // second row 
}

关于c++ - 如何将5个值的数组1D打印为列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61703129/

相关文章:

c++ - 快速路径数据包处理的配置更新

c++ - 如何在 QML 中访问 C++ 对象的列表属性

c++ - C++ 数组运算符和 *(array + index)(如果有)有什么区别?

python - 除以包含零的数组,python

python - 将 numpy 数组追加到元素中

C++ 文件处理 - 输出与输入不匹配

c++ - Ubuntu 20.04 中未找到 io.h CLion

c++ - gcc 优化导致应用程序失败

java - 为什么我收到此错误 : data structures (arrays)?

python - 将 csv 中的字符串条目写入 python 中的数组?