C++中数组与CUT交互的解决方案

C++中数组与CUT交互的解决方案,c++,arrays,for-loop,cout,C++,Arrays,For Loop,Cout,我想要一个函数或循环在数组中运行,并打印出每个元素,直到打印出10个元素。在这种情况下,将启动新行并继续打印。例1 2 3 4 5 678910 这是为一个像50年代的家庭主妇一样工作的数组而设计的程序,该程序对所述数组执行许多计算和修改 这是我目前对我问题背后逻辑的攻击 int main() { test = new int[100]; for (int i = 0; i < 100; i++){ //Set array

我想要一个函数或循环在数组中运行,并打印出每个元素,直到打印出10个元素。在这种情况下,将启动新行并继续打印。例1 2 3 4 5 678910

这是为一个像50年代的家庭主妇一样工作的数组而设计的程序,该程序对所述数组执行许多计算和修改

这是我目前对我问题背后逻辑的攻击

int main()
    {
test = new int[100];
        for (int i = 0; i < 100; i++){                      
//Set array to rand
            test[i] = rand() % 44 + 55;
        }

        printf("original List\n");
        for (int i = 0; i < 100; i++){          
// print original order
            printf("%d\n", test[i]);
        }


        sortArr;                
// function call sort array ascend

        printf("\Sorted List\n");
            for (int i = 0;i < 100;i++) {           
// print sorted order

                printf("%d , ", test[i]);
                int temp;
//temp counter for width of printout

for (temp = 0;temp < 10;temp++) cout<< "\n" << endl;


sum += test[i];

            }
预期是一个输出块,由网格中的100个数组元素组成,每行宽度为10个元素


实际结果是一堆新线循环,让我更加头疼。

非常常见的问题就是使用基于索引i的模数:

for (int i = 0;i < 100;i++) {           
    printf("%d , ", test[i]);
    if ((i + 1) % 10 == 0) {
        printf("\n");
    }
}
但是,如果您想要格式良好的输出,则需要:

#include <iomanip>


最简单的解决方案是打印分隔符i%10==0\n:。您正确地认识到,在循环的每次迭代中使用剩余部分是低效的,并且希望编写一个将打印十个元素,然后换行的循环

这里的技巧是编写一个内部循环,增加第二个计数器j,在内部循环中执行所有输出,然后在外部循环的底部更新i。一个简化的例子:

#include <array>
#include <iostream>
#include <stdlib.h>
#include <time.h>

using std::cout;

int main()
{
  constexpr size_t ARRAY_LEN = 100;
  std::array<int, ARRAY_LEN> test;

  {
    // Quick and dirty initialization of the random seed to the lowest 30
    // or so bits of the system clock, which probably does not really have
    // nanosecond precision.  It’ll do for this purpose.

    timespec current_time;
    timespec_get( &current_time, TIME_UTC );
    srand(current_time.tv_nsec);
  }

  for (int i = 0; i < test.size(); i++){
    //Set array to rand
    test[i] = rand() % 44 + 55;
  }

  for ( int i = 0, j = 0;
        i < test.size();
        i += j ) {
    for ( j = 0; j < 10 && i + j < test.size(); ++j ) {
      cout << test[i + j] << ' ';
    }

    cout << '\n';
  }

  return EXIT_SUCCESS;
}

请注意,您编写的版本不会初始化标准库的随机种子,因此您会得到相同的分布不均匀的随机数。您可以编写一个版本,使用更高级的STL随机数生成器,或许可以替代C版本,但这有点超出了您的问题范围。

好吧,对于一个运行十次的for循环,您到底希望它运行十次,每次在循环中打印一个换行,然后再从std::endl中打印另一个换行,您对它有什么期望?如果您期望的不是一行20条换行符,那么您能解释一下为什么期望的不是这个非常简单的for循环的简单结果吗?你想要的只是每10个元素后面的一个换行符。您碰巧已经在使用i来计算每个元素的数量,因为它被打印出来了,所以只需每隔十个is添加一个换行即可。@SamVarshavchik请友好一点?我希望在对for循环进行10次计数之后会有一个换行,但我似乎理解了你们所说的关于换行的问题,因为它是指数型的。你们在一个循环中打印一个换行,后面跟着std::endl,而这个循环不做任何其他事情。当然,这样做的最终结果将是连续20行换行。非常感谢。我必须记住带模索引的if语句。非常有帮助!
#include <array>
#include <iostream>
#include <stdlib.h>
#include <time.h>

using std::cout;

int main()
{
  constexpr size_t ARRAY_LEN = 100;
  std::array<int, ARRAY_LEN> test;

  {
    // Quick and dirty initialization of the random seed to the lowest 30
    // or so bits of the system clock, which probably does not really have
    // nanosecond precision.  It’ll do for this purpose.

    timespec current_time;
    timespec_get( &current_time, TIME_UTC );
    srand(current_time.tv_nsec);
  }

  for (int i = 0; i < test.size(); i++){
    //Set array to rand
    test[i] = rand() % 44 + 55;
  }

  for ( int i = 0, j = 0;
        i < test.size();
        i += j ) {
    for ( j = 0; j < 10 && i + j < test.size(); ++j ) {
      cout << test[i + j] << ' ';
    }

    cout << '\n';
  }

  return EXIT_SUCCESS;
}