i'm trying to create a matrix 20X20 double type so i could perform operations on it.
我正在尝试创建一个矩阵20X20双类型,所以我可以对它执行操作。
The problem is that when I try to print it so it could like like a matrix, there are many zeros because of the type double. And it doesn't look like a matrix.
问题是,当我尝试打印它所以它可能像矩阵一样,因为类型为double,所以有很多零。它看起来不像矩阵。
This is what I wrote:
这就是我写的:
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
mat[i][j] = (rand() % (SIZE + 1) + 0);
}
}
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
printf("%10lf", mat[i][j]);
printf("\n\n");
}
How can I make it look like 20X20 matrix and keep the double type?
如何让它看起来像20X20矩阵并保持双重类型?
1 个解决方案
#1
2
If no precision is specified for %f
argument of printf
, it is using 6 decimal digits.
如果没有为printf的%f参数指定精度,则使用6位十进制数字。
Also, for printf
there is no need to use %lf
, float
arguments are always promoted to double
when passed (Why does printf() promote a float to a double?). But usually, a printf
implementation will ignore the l
prefix.
另外,对于printf,不需要使用%lf,浮点参数在传递时总是被提升为double(为什么printf()将float提升为double?)。但通常,printf实现将忽略l前缀。
For a proper print alignment, in this case you can use:
为了正确的打印对齐,在这种情况下,您可以使用:
printf("%4.1f ", mat[i][j]);
// Here a space is added after printing the number.
// "%5.1f" could also be used for alignment (difference: the space will be prefixed),
// but then if the number will have 3 or more integer digits, it will be displayed
// "concatenated" with the previous one. Using a space prevents this
Using this, there will be 4 positions allocated to print each number, from which one position will be used by the decimal point, and another one for the decimal places (1 in this case). Two positions are remaining for the integer part (if it doesn't fit in these 2 positions, more will be used and may broke the alignment.
使用它,将分配4个位置来打印每个数字,小数点将使用一个位置,小数点使用另一个位置(在这种情况下为1)。整数部分剩余两个位置(如果它不适合这两个位置,将使用更多位置并可能打破对齐。
If running in a default Windows terminal: Even if using this alignment, there will not be enough room to print a matrix row on a single line, and 2 lines will be used. This is because by default, Windows terminal line width is 80 characters. Increasing it to 100 or more will fix the problem. This can be changed using the Properties options, or even programatically, as described here: How can I change the width of a Windows console window?
如果在默认的Windows终端中运行:即使使用此对齐,也没有足够的空间在一行上打印矩阵行,并且将使用2行。这是因为默认情况下,Windows终端线宽为80个字符。将其增加到100或更多将解决问题。这可以使用“属性”选项进行更改,甚至可以使用编程方式进行更改,如下所述:如何更改Windows控制台窗口的宽度?
#1
2
If no precision is specified for %f
argument of printf
, it is using 6 decimal digits.
如果没有为printf的%f参数指定精度,则使用6位十进制数字。
Also, for printf
there is no need to use %lf
, float
arguments are always promoted to double
when passed (Why does printf() promote a float to a double?). But usually, a printf
implementation will ignore the l
prefix.
另外,对于printf,不需要使用%lf,浮点参数在传递时总是被提升为double(为什么printf()将float提升为double?)。但通常,printf实现将忽略l前缀。
For a proper print alignment, in this case you can use:
为了正确的打印对齐,在这种情况下,您可以使用:
printf("%4.1f ", mat[i][j]);
// Here a space is added after printing the number.
// "%5.1f" could also be used for alignment (difference: the space will be prefixed),
// but then if the number will have 3 or more integer digits, it will be displayed
// "concatenated" with the previous one. Using a space prevents this
Using this, there will be 4 positions allocated to print each number, from which one position will be used by the decimal point, and another one for the decimal places (1 in this case). Two positions are remaining for the integer part (if it doesn't fit in these 2 positions, more will be used and may broke the alignment.
使用它,将分配4个位置来打印每个数字,小数点将使用一个位置,小数点使用另一个位置(在这种情况下为1)。整数部分剩余两个位置(如果它不适合这两个位置,将使用更多位置并可能打破对齐。
If running in a default Windows terminal: Even if using this alignment, there will not be enough room to print a matrix row on a single line, and 2 lines will be used. This is because by default, Windows terminal line width is 80 characters. Increasing it to 100 or more will fix the problem. This can be changed using the Properties options, or even programatically, as described here: How can I change the width of a Windows console window?
如果在默认的Windows终端中运行:即使使用此对齐,也没有足够的空间在一行上打印矩阵行,并且将使用2行。这是因为默认情况下,Windows终端线宽为80个字符。将其增加到100或更多将解决问题。这可以使用“属性”选项进行更改,甚至可以使用编程方式进行更改,如下所述:如何更改Windows控制台窗口的宽度?