gpt4 book ai didi

c - 声明二维数组的程序

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:46 25 4
gpt4 key购买 nike

我正在编写一个程序,声明一个名为 mynumbers 的二维数组。该程序应静态初始化数组 mynumbers 的 8 个元素并显示1. 最少的元素2.最大元素

这是我尝试过的。

#include <stdio.h>
int main(){
int mynumbers[2][4]={{17,27,13,32},{18,52,19,62}};
int largenum = mynumbers[1][2];
int leastnum = mynumbers[0][1];
if(mynumbers[0][0]>largenum)
{
largenum = mynumbers[0][0];
}
else largenum =largenum;
if(mynumbers[0][1]>largenum)
{
largenum = mynumbers[0][1];
}
else largenum =largenum;
if(mynumbers[0][2]>largenum)
{
largenum = mynumbers[0][2];
}
else largenum =largenum;
if(mynumbers[0][3]>largenum)
{
largenum = mynumbers[0][3];
}
else largenum =largenum;
if(mynumbers[1][0]>largenum)
{
largenum = mynumbers[1][0];
}
else largenum =largenum;
if(mynumbers[1][1]>largenum)
{
largenum = mynumbers[1][1];
}
else largenum =largenum;
if(mynumbers[1][2]>largenum)
{
largenum = mynumbers[1][2];
}
else largenum =largenum;
if(mynumbers[1][3]>largenum)
{
largenum = mynumbers[1][3];
}
else largenum =largenum;
{
printf("the largest number is :%d/n",largenum);
}
//else leastnum =leastnum;
if(mynumbers[0][0]<leastnum)
{
leastnum = mynumbers[0][0];
}
else leastnum =leastnum;
if(mynumbers[0][1]<leastnum)
{
leastnum = mynumbers[0][1];
}
else leastnum =leastnum;
if(mynumbers[0][2]<leastnum)
{
leastnum = mynumbers[0][2];
}
else leastnum =leastnum;
if(mynumbers[0][3]<leastnum)
{
leastnum = mynumbers[0][3];
}
else leastnum =leastnum;
if(mynumbers[1][0]<leastnum)
{
leastnum = mynumbers[1][0];
}
else leastnum =leastnum;
if(mynumbers[1][1]<leastnum)
{
leastnum = mynumbers[1][1];
}
else leastnum =leastnum;
if(mynumbers[1][2]<leastnum)
{
leastnum = mynumbers[1][2];
}
else leastnum =leastnum;
if(mynumbers[1][3]<leastnum)
{
leastnum = leastnum[1][3];
}
else leastnum =leastnum;
{
printf("the smallest number is :%d/n",leastnum);
}
}

我得到以下错误

 ||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\Admin\Documents\Untitled1.c||In function 'main':|
C:\Users\Admin\Documents\Untitled1.c|87|error: subscripted value is neither array nor pointer nor vector|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

最佳答案

编译时出现以下错误:error: subscripted value is neither array nor pointer nor vector leastnum = leastnum[1][3];,这很清楚,因为你声明并初始化了leastnum 作为 int 而不是 int[][]。然后您可能想要编写 leastnum = mynumbers[1][3]

此外,您还错过了使用循环的好机会:您正在手动比较二维数组的每个值。以下是您可以如何压缩所有的 if else 条件:

#include <stdio.h>

int main(void){
int mynumbers[2][4]={
{17,27,13,32},
{18,52,19,62}
};
int min = **mynumbers; // <=> int min = mynumbers[0][0]
int max = **mynumbers;

int row = sizeof(mynumbers) / sizeof(mynumbers[0]); // = (8*4) / (4*4) = 2
int column = sizeof(mynumbers[0]) / sizeof(int); // = (4*4) / 4 = 4

for(int i=0; i<row; i++){
for(int j=0; j<column; j++){
if(mynumbers[i][j] < min) min = mynumbers[i][j];
if(mynumbers[i][j] > max) max = mynumbers[i][j];
}
}
printf("Min: %d\nMax: %d\n", min, max);
return 0;
}

关于c - 声明二维数组的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56984136/

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