[英]% symbols keep printing when trying to print out a char 2D array in C
I'm new to C and I'm just trying to print out a two 2 array. 我是C语言的新手,我只是想打印出两个2的数组。 This bug has been annoying me all day and I'm not really sure whats going on.
这个错误整天困扰着我,我不太确定发生了什么。
#include<stdio.h>
void run(int);
main()
{
run(5);
return 0;
}
//Have to make it a character array as it needs to
//store numbers AND commas.
run(int x)
{
int size = 2*x -1;
char array[size][size];
int i = 0;
int j = 0;
for( i; i < size; i++){
for(j; j< size; j++){
array[i][j] = '1';
}
}
int k = 0;
int l = 0;
for( k; k < size; k++){
for(l; l< size; l++){
printf( "%c" , array[l][k]);
}
printf("%\n", "");
}
}
This is the output I get: 这是我得到的输出:
1%
%
%
%
%
%
%
%
%
You code has several mistakes: 您的代码有几个错误:
The biggest problem is that your not initializing your loop counters where you should: 最大的问题是您没有在以下位置初始化循环计数器:
for(i; i < size; i++){
for(j; j < size; j++){
With that, i
& j
are left as they were prior to the for
statement. 这样,
i
& j
保留为for
语句之前的状态。 The first section of these statements does nothing at all. 这些语句的第一部分什么也不做。 While that's harmless for
i
(since it's initialized to 0 before the for
), that's devastating for j
, which never goes back to 0. Your code should be: 尽管这对于
i
是无害的(因为它在for
之前被初始化为0),但是对于j
是毁灭性的,它永远不会回到0。您的代码应该是:
for(i = 0; i < size; i++){
for(j = 0; j < size; j++){
The same issue exists with k
& l
, and the same fix should be applied: k
& l
存在相同的问题,应采用相同的解决方法:
for(k = 0; k < size; k++){
for(l = 0; l < size; l++){
Next, you're "rotating" access in your array. 接下来,您将“旋转”阵列中的访问权限。 When you fill the array with values, you have
i
in your outer loop and j
in the inner loop, and you use them as [i][j]
: 当用值填充数组时,外循环中有
i
,内循环中有j
,并将它们用作[i][j]
:
array[i][j] = '1';
Think of that as Out
& In
--> [Out][In]
. 认为是
Out
& In
> [Out][In]
。
When you print the array, you "rotate" that, k
is outer & l
is inner, and you use them as [l][k]
: 当您打印数组时,您将“旋转”,
k
为外部, l
为内部,并将它们用作[l][k]
:
printf("%c", array[l][k]);
That's like doing [In][Out]
. 就像做
[In][Out]
。
While that's not a problem with all values being identical ( '1'
), and the matrix being square (width == height), it won't work with other values or dimensions, and is confusing. 尽管所有值都相同(
'1'
)并且矩阵为正方形(宽度==高度)不是问题,但它不能与其他值或尺寸一起使用,并且令人困惑。
Last, you're attempt to print a new line is wrong. 最后,您尝试打印新行是错误的。 You have a
%
specifier, but your not really using any valid character after that, and you don't need that anyway, just print: 您有一个
%
指示符,但此后您并未真正使用任何有效字符,而且您也不需要它,只需打印:
printf("\n");
So, all together, here's what the code should be: 因此,总的来说,这就是代码的含义:
run(int x)
{
int size = 2*x -1;
char array[size][size];
int i,j;
for(i = 0; i < size; i++){
for(j = 0; j < size; j++){
array[i][j] = '1';
}
}
int k, l;
for(k = 0; k < size; k++){
for(l = 0; l < size; l++){
printf("%c", array[k][l]);
}
printf("\n");
}
}
(And as a side note, k
& l
are not really required, you can simply reuse i
& j
) (此外,并不需要
k
& l
,您可以简单地重用i
& j
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.