Just started C after first year of java and am already confused. I try to print an array into a multiplication table. It works otherwise but I get zeroes after the first round of printing the array. I have allocated data in the whole array and it loops the right amount after checking. Still after the first print line I get 0 0 0 0 0 0 0 after the correct output.
在java的第一年刚刚开始使用C,我已经很困惑了。我尝试将数组打印到乘法表中。它起作用,但在第一轮打印阵列后我得到了零。我已在整个数组中分配数据,并在检查后循环正确的数量。在第一个打印行之后,在正确输出后,我得到0 0 0 0 0 0 0。
#include<stdio.h>
int main(void)
{
int kertotaulu[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int kertoja;
int indeksi;
for(kertoja = 0; kertoja <=15; kertoja++) {
if(kertoja == 0) {
printf("x");
printf(" ");
for(indeksi = 0; indeksi< (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi]);
printf(" ");
}
}
for(indeksi = 0; indeksi< (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi]*kertoja);
printf(" ");
}
printf("\n");
}
return 0;
}
and the output is:
输出是:
x 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45
4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90
7 14 21 28 35 42 49 56 63 70 77 84 91 98 105
8 16 24 32 40 48 56 64 72 80 88 96 104 112 120
9 18 27 36 45 54 63 72 81 90 99 108 117 126 135
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
11 22 33 44 55 66 77 88 99 110 121 132 143 154 165
12 24 36 48 60 72 84 96 108 120 132 144 156 168 180
13 26 39 52 65 78 91 104 117 130 143 156 169 182 195
14 28 42 56 70 84 98 112 126 140 154 168 182 196 210
15 30 45 60 75 90 105 120 135 150 165 180 195 210 225
3 个解决方案
#1
3
kertoja
is zero the first time through the loop so your second indeksi
loop multiplies each element by zero giving you all of the extra 0
's. You could add the second indeksi
loop into an else case to avoid this, I'm not sure exactly what output you're looking for however so...
第一次通过循环时kertoja为零,所以你的第二个indeksi循环将每个元素乘以零,给你所有额外的0。您可以将第二个indeksi循环添加到else情况中以避免这种情况,我不确定您正在寻找什么输出但是如此...
#2
0
The issue could be fixed by ommitting the second loop for the index 0
as follows.
可以通过省略索引0的第二个循环来解决该问题,如下所示。
int main(void)
{
int kertotaulu[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int kertoja;
int indeksi;
for (kertoja = 0; kertoja <= 15; kertoja++)
{
if (kertoja == 0)
{
printf("x");
printf(" ");
for (indeksi = 0; indeksi < (sizeof(kertotaulu)
/ sizeof(kertotaulu[0])); indeksi++)
{
printf("%d", kertotaulu[indeksi]);
printf(" ");
}
}
else // <-- change
{
for (indeksi = 0; indeksi < (sizeof(kertotaulu)
/ sizeof(kertotaulu[0])); indeksi++)
{
printf("%d", kertotaulu[indeksi] * kertoja);
printf(" ");
}
}
printf("\n");
}
return 0;
}
#3
0
For the case when kertoja is zero it does it's own loop, but it also continues on to the second loop. A simple new line and continue should fix it.
对于kertoja为零的情况,它会自己循环,但它也会继续到第二个循环。一个简单的新行和继续应该修复它。
#include<stdio.h>
int main(void)
{
int kertotaulu[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
int kertoja;
int indeksi;
for (kertoja = 0; kertoja <= 15; kertoja++) {
if (kertoja == 0) {
printf("x");
printf(" ");
for (indeksi = 0; indeksi < (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi]);
printf(" ");
}
printf("\n"); // New Line
continue; // Continue, goes to the top of loop again
}
for (indeksi = 0; indeksi < (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi] * kertoja);
printf(" ");
}
printf("\n");
}
return 0;
}
#1
3
kertoja
is zero the first time through the loop so your second indeksi
loop multiplies each element by zero giving you all of the extra 0
's. You could add the second indeksi
loop into an else case to avoid this, I'm not sure exactly what output you're looking for however so...
第一次通过循环时kertoja为零,所以你的第二个indeksi循环将每个元素乘以零,给你所有额外的0。您可以将第二个indeksi循环添加到else情况中以避免这种情况,我不确定您正在寻找什么输出但是如此...
#2
0
The issue could be fixed by ommitting the second loop for the index 0
as follows.
可以通过省略索引0的第二个循环来解决该问题,如下所示。
int main(void)
{
int kertotaulu[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int kertoja;
int indeksi;
for (kertoja = 0; kertoja <= 15; kertoja++)
{
if (kertoja == 0)
{
printf("x");
printf(" ");
for (indeksi = 0; indeksi < (sizeof(kertotaulu)
/ sizeof(kertotaulu[0])); indeksi++)
{
printf("%d", kertotaulu[indeksi]);
printf(" ");
}
}
else // <-- change
{
for (indeksi = 0; indeksi < (sizeof(kertotaulu)
/ sizeof(kertotaulu[0])); indeksi++)
{
printf("%d", kertotaulu[indeksi] * kertoja);
printf(" ");
}
}
printf("\n");
}
return 0;
}
#3
0
For the case when kertoja is zero it does it's own loop, but it also continues on to the second loop. A simple new line and continue should fix it.
对于kertoja为零的情况,它会自己循环,但它也会继续到第二个循环。一个简单的新行和继续应该修复它。
#include<stdio.h>
int main(void)
{
int kertotaulu[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
int kertoja;
int indeksi;
for (kertoja = 0; kertoja <= 15; kertoja++) {
if (kertoja == 0) {
printf("x");
printf(" ");
for (indeksi = 0; indeksi < (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi]);
printf(" ");
}
printf("\n"); // New Line
continue; // Continue, goes to the top of loop again
}
for (indeksi = 0; indeksi < (sizeof(kertotaulu) / sizeof(kertotaulu[0])); indeksi++) {
printf("%d", kertotaulu[indeksi] * kertoja);
printf(" ");
}
printf("\n");
}
return 0;
}