gpt4 book ai didi

c - 没有给出正确的输出

转载 作者:行者123 更新时间:2023-11-30 18:06:14 26 4
gpt4 key购买 nike

程序应创建一个 8*8 的 2D 表,其中包含随机 number<3
它应该打印该表。
另一个任务是将这个表翻译成另一个表
例如
120
210
111

中间的数字应该改为它周围所有数字的和1+2+0+2+0+1+1+1=8
一切事情都应该这样做; 那么程序应该被打印
如果有任何大于 9 的数字,则应将其转换为十六进制......我还没有做十六进制。但它仍然不起作用......

#include <stdio.h>  
#include <stdlib.h>
#define cols 8
#define rows 8
void printA(int A[][cols]);
void printC(char C[][cols]);
void SumThemUp(int A[][cols], char C[][cols]);
int main()
{
srand(time(NULL));
int A[rows][cols];
char C[rows][cols];
int i, j;
for(i=0; i<rows; i++)
for(j=0; j<cols; j++)
A[i][j]=rand()%3;
printA(A);
SumThemUp(A,C);
printC(C);
return 0;
}

void printA(int A[][cols])
{ int i, j;
for(i=0;i<rows;i++)
{for(j=0;j<cols; j++)
{printf("%d ", A[i][j]);}
printf("\n");}
return ;
}
void printC(char C[][cols])
{
int i, j;
for(i=0;i<rows;i++)
{for(j=0;j<cols; j++)
{printf("%ch ", C[i][j]);}
printf("\n");}
return ;
}
void SumThemUp(int A[][cols], char C[][cols])
{
int i,j;
for(i=0;i<rows;i++)
{for(j=0;j<cols; j++)
C[i][j]=0;}
for(i=0;i<rows;i++)
{for(j=0;j<cols; j++)
A[i][j]=C[i++][j];
}
for(j=0;j<cols; j++)
{for(i=0;i<rows;i++)
C[i][j]+=A[i][j++];
}return;
}

最佳答案

所以 - 我不完全确定我知道你想要的输出是什么 - 但你所拥有的有几个问题:

0:对于您的数组,名称应该描述数组实际保存的内容,A 和 C 非常不明确。

1:使用 { } 确定作用域,并将 { } 放在自己的行上。 (也许它只是在 Stack Overflow 中粘贴得不好)

2:你有一组循环,基本上将 C 中的所有内容设置为 0:

for(i=0;i<rows;i++)  
{
for(j=0;j<cols; j++)
{
C[i][j]=0;
}
}

紧接着你就有了:

for(i=0;i<rows;i++)
{
for(j=0;j<cols; j++)
{
A[i][j]=C[i++][j]; // <--- problem here
}
}

所以之后A和C都全是0了。最重要的是,当访问 C 中的列时,您有 i++ 内联。这实际上改变了 for 循环正在使用的值,因此每行和每列的 i 都会递增。想必您想要:

A[i][j]=C[i+1][j];

3:您这里也有类似的问题:

for(j=0;j<cols; j++)
{
for(i=0;i<rows;i++)
{
C[i][j]+=A[i][j++]; // Presumably you want j+1
}
}

4:为什么在 C 语言中使用字符数组?如果它保存的是整数之和,那么它可能应该声明为 int。如果这是您将整数打印为十六进制(或只是普通整数)的想法,那么简单地使用 printf 将整数输出为十六进制会更容易:

// use %d to print the integer "normally" (base 10)
// use %x if you want a hex value with lowercase letters
// use %X if you want a hex value with capital letters
printf("125 as hex is: 0x%x", 125); // 0x7d

我希望这能为您指明正确的方向。

--丹

关于c - 没有给出正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5738520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com