gpt4 book ai didi

c - 找到矩阵行和列的最大总和,这是我得到的 2 个代码,其中 1 个有效,但另一个无效,尽管数组初始化是相同的

转载 作者:行者123 更新时间:2023-11-30 15:36:08 24 4
gpt4 key购买 nike

在代码 1 中,数组运行时初始化工作正常,但在代码 2 中则不然,尽管这两个代码几乎相同

代码1:

#include<stdio.h>

int main()
{
int n;
scanf("%d",&n);
int A[n][n],i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
}
int sum=0, newsum=0;
for(i=0;i<n;i++)
{
newsum = 0;
for(j=0;j<n;j++)
{
newsum = newsum + A[i][j];
}
if(sum < newsum)
{
sum = newsum;
}
}
return 0;
}

代码2:

#include<stdio.h>

int main()
{
int dim,i,j,k,h=0;
scanf("%d",k);
int array[k][k];
int max[2k+2]={0};

for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
scanf("%d",&array[i][j]);
}
}
//rows
h=max[0];
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
max[i]+=array[i][j];

}
if(max[i]>=h)
{
h=max[i];
}
}
//columns
for(j=0;j<k;j++)
{
for(i=0;i<k;i++)
{
max[j+k]+=array[i][j];
}
if(max[j+k]>=h)
{
h=max[j+k];
}

}
//diagonal_1
for(i,j=0;j<k && i<k;j++,i++)
{
max[2k]+=array[i][j];
}
for(i=0,j=k-1;j>=0&&i<k;j--,i++)
{
max[2k+1]+=array[i][j];
}
if(max[2k]>=max[2k+1])
max[2k]=max[2k];
else
max[2k]=max[2k+1];

if(max[2k]>=h)
printf("%d",max[2k]);
else
printf("%d",h);

return 0;
}

发生的错误是:

Program: In function 'main':
Program:11:9: error: fixed-point types not supported for this target
Program:48:6: warning: left-hand operand of comma expression has no effect[-
Program:50:13: error: fixed-point types not supported for this target
Program:54:9: error: fixed-point types not supported for this target
Program:56:8: error: fixed-point types not supported for this target
Program:56:17: error: fixed-point types not supported for this target
Program:57:5: error: fixed-point types not supported for this target
Program:57:13: error: fixed-point types not supported for this target
Program:59:5: error: fixed-point types not supported for this target
Program:59:13: error: fixed-point types not supported for this target
Program:61:8: error: fixed-point types not supported for this target
Program:62:17: error: fixed-point types not supported for this target
Program:11:5: warning: unused variable 'max' [-Wunused-variable]

最佳答案

嗯,你不能使用像 int max[2k+2]={0} 这样的变量来声明数组,因为数组的大小必须在编译期间确定。

关于c - 找到矩阵行和列的最大总和,这是我得到的 2 个代码,其中 1 个有效,但另一个无效,尽管数组初始化是相同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22660809/

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