【发布时间】:2022-01-25 09:18:20
【问题描述】:
有人给了我一个用 C 语言解决的问题。
编写一个C程序,可以显示四个城市最近五天的温度,并显示每个城市的温度比前一天高多少天
示例输入
20 27 28 22
12 22 12 20
22 24 25 33
33 30 30 22
样本输出
2
2
3
0
我一直在尝试比较二维数组的特定行中的元素。但是我迷失在如何比较元素和计数之间,尽管我能够连续定位大小元素。我正在展示我的一段代码:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int array1[100][100];
int num, row, column, maxTemp = 0, minTemp = 0, counter = 0;
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");
}
maxTemp = array1[0][0];
minTemp = array1[0][0];
int maxTempRowL, maxTempColumnL, minTempRowL, minTempColumnL;
for(int i=1; i<row-1; i++)
{
for(int j=1; j<column-1; j++)
{
if(maxTemp < array1[i+1][j])
{
maxTemp = array1[i][j];
maxTempRowL = i; //row location
maxTempColumnL = j; //column location
}
if(minTemp > array1[i-1][j])
{
minTemp = array1[i][j];
minTempRowL = i; //row location
minTempColumnL = j; //column location
}
}
if(maxTemp > minTemp)
{
counter++;
break;
}
/*if(maxTemp <= minTemp)
{
return NULL;
}*/
printf("%d\n", counter);
counter = 0;
}
return 0;
}
【问题讨论】:
-
该输入与代码所期望的不匹配。您是否遗漏了
row、column和num的值?