简体   繁体   English

在C中每行麻烦打印5个双数组元素

[英]Trouble printing 5 elements of a double array per line in C

I've been working on an assignment for school, basically we're creating 2 arrays of random doubles, then sorting them using 3 different algorithms and printing the results. 我一直在为学校分配作业,基本上是创建2个随机双精度数组,然后使用3种不同的算法对它们进行排序并打印结果。 The output needs to be printed with 5 elements per line. 输出需要每行打印5个元素。

I've got this code: 我有以下代码:

    //print sorted arrayB
for (i = 0; i < size; i++)
{
    printf ("%.1lf  ", arrayB[i]);
    if (i % 5 == 0 && i > 0)
    {
        printf ("\n");
    }
}

where all variables are defined before the sort, and my output looks like this every time: 在排序之前定义了所有变量的地方,每次我的输出都是这样的:

1.0  2.0  3.0  4.0  5.0  6.0
7.0  8.0  9.0  10.0 11.0
12.0 13.0 14.0 15.0 16.0
17.0 18.0 19.0 20.0 21.0
etc...

I don't understand why it's printing 6 elements in the top row, and 5 in all the rest. 我不明白为什么它在第一行中打印6个元素,而在其余所有元素中打印5个元素。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

You need to update the \\n condition as below - 您需要按以下方式更新\\n条件-

if ((i+1) % 5 == 0)
    {
        printf ("\n");
    }

i % 5 will cause newlines after indices 5,10,15.. . i % 5将在索引5,10,15..之后引起换行。 This is your case where you have 6 at index 5, 11 at index 10.. Rather you need to break at 4,9,14.. . 在这种情况下,索引5处有6,索引10处有4,9,14..而是需要在4,9,14..4,9,14.. which are all covered by (i+1) . 这些都被(i+1)覆盖。

Just write the evaluation by hand for your conditional: 只需为您的条件手动编写评估:

if (i % 5 == 0 && i > 0)

i  result
0  false
1  false
2  false
3  false
4  false
5  true
6  false

Now we can see that it is false 5 times, then true, which makes it print a newline, but only after you printed the number! 现在我们可以看到它是错误的5次,然后是true,这使它打印换行符,但是仅在您打印完数字之后!

So you need to rearrange your logic slightly, but the conditional is correct. 因此,您需要稍微重新排列逻辑,但条件是正确的。

The point is in the && i > 0 part of the if statement. 该点位于if语句的&& i > 0部分中。 If you'd remove it, this would be the output: 如果将其删除,则将是输出:

1.0
2.0  3.0  4.0  5.0  6.0
7.0  8.0  9.0  10.0 11.0
12.0 13.0 14.0 15.0 16.0
17.0 18.0 19.0 20.0 21.0
etc...

By excluding zero, you have prevented printing the newline after the first number, see? 通过排除零,可以防止在第一个数字之后打印换行符,请参见?

The solution is to move the i index by one, like this: 解决方案是将i索引移动一个,如下所示:

if ((i + 1) % 5 == 0)
{
    printf ("\n");
}

Now you don't even need the && i > 0 part, because i + 1 will never be zero. 现在您甚至不需要&& i > 0部分,因为i + 1永远不会为零。

Just add 1 to i(therefore the condition i > 0, becomes unnecessary), which will solve. 只需在i上加1(因此条件i> 0,就变得不必要了)即可解决。 This occurs because in the condition imposed by you, for the first line break (\\ n) to occur, it will have to go from 0 to 5, and to do so you will have to repeat the loop 6 times, so on the first line it showed 6 numbers , instead of 5 发生这种情况的原因是,在您施加的条件下,要使第一个换行符(\\ n)发生,它必须从0到5,并且这样做必须将循环重复6次,因此在第一个情况下行显示6个数字,而不是5个

Like this: 像这样:

#include <stdio.h>
#define SIZE 25

int main() {
 int i, arrayB[SIZE] = {0};
 for (i = 0; i < SIZE; i++)
 {
     printf ("%.1lf  ", arrayB[i]);
     if ((i+1) % 5 == 0)
     {
         printf ("\n");
     }
 }
return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM