【发布时间】:2022-01-25 09:48:35
【问题描述】:
编写一个C程序,可以显示四个城市最近五天的温度,并显示每个城市的最高和最低温度之间的差异
示例输入
20 27 28 22
12 22 12 20
22 24 25 33
33 30 30 29
样本输出
8
8
11
4
我正在使用 C 来寻找二维数组的特定行中的大元素和小元素之间的区别。我能够找到大元素和小元素,但是在搜索特定行以查找时我有点迷失了大元素和小元素的区别。下面我展示我的一段代码-
#include<stdio.h>
int main()
{
int array1[10][10];
int num, row, column;
printf("How many rows and columns are needed: ");
scanf("%d %d", &row, &column);
printf("\nHow many cities temperature you want to enter: ");
scanf("%d", &num);
printf("\nEnter %d cities temperature: ", num);
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
scanf("%d", &array1[i][j]);
}
printf("\n");
}
int largeA = array1[0][0];
int smallA = array1[0][0];
int diff = 0;
for(int i=0; i<row; i++)
{
int diff = 0;
for(int j=1; j<column; j++)
{
if(array1[i][j] > largeA)
{
largeA = array1[i][j];
}
if(array1[i][j] < smallA)
{
smallA = array1[i][j];
}
}
diff = largeA - smallA;
printf("%d\n", diff);
}
return 0;
}
【问题讨论】:
-
我有点迷失在每一行中找出大元素和小元素之间的区别。
-
第二个输出不应该是10而不是8吗?您发布的代码不起作用吗?它到底有什么问题?
-
“那种迷失”是如何体现的?
-
“有点迷路”的意思是我试图找出每一行中大小元素之间的区别,但在第一行我得到了正确的答案,但其余的行得到了错误的答案。
-
您忽略了
num变量