如何每行打印特定数量的数组元素?

use*_*298 2 c++ arrays

我想在数组的每一行打印10个元素,因此输出看起来像这样:

 1  2  3  4  5  6  7  8  9 10
11 12 13 14 15 16 17 18 19 20
...etc
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的代码:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()
{
    srand(time(0));
    int array[1000];
    for (int i = 0; i < 1000; i++) array[i] = rand() % 1000;
    sort(array, array + 1000);
    for (int i = 0; i < 1000;){
        i += 10;
        for(int j = 0; j < i; j++){
            cout << array[j] << " ";
        }
        cout << endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但我无法让它发挥作用.出于某种原因,它一遍又一遍地重复所有数字.

Jar*_*Par 9

内部循环仅用于j参数列表.它还需要考虑,i以便它可以跳过它已经打印出来的数字

有一种更简单的方法.而不是做嵌套循环,只需每10个元素打印一个新行.

for (int i = 0; i < 1000;) {
  cout << array[i] << " ";
  if ((i + 1) % 10 == 0) {
    cout << endl;
  }
}
Run Code Online (Sandbox Code Playgroud)