我被要求用 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;
}
uj5u.com熱心網友回復:
基本上你可以在每一行的開頭取一個計數器變數,如果陣列中的前一個元素小于當前元素,那么你可以向計數器變數添加一個。在每一行的末尾,您可以顯示計數器變數的計數。
- 回圈條件不準確,您從第 1 個索引而不是第 0 個索引開始。此外,它應該是
i <= row-1但你寫的i < row-1,這樣最后一行沒有被處理。
檢查下面的代碼
#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-1; i )
{
for(int j=0; j <= column-1; j )
{
scanf("%d", &array1[i][j]);
}
printf("\n");
}
for(int i=0; i<=row-1; i )
{
int counter = 0;
for(int j=1; j <= column-1; j )
{
maxTemp = array1[i][j-1];
if ( maxTemp <= array1[i][j] )
counter ;
}
printf("%d\n", counter);
}
return 0;
}
uj5u.com熱心網友回復:
由于您專門詢問了比較,因此我將跳過此答案中的輸入。我在這里假設你有一個row x col matrix,每一行代表一個城市,每一列代表一個每天的溫度測量。
所以,如果我有像你這樣的矩陣:
20 27 28 22
12 22 12 20
22 24 25 33
33 30 30 22
為了得到這樣的輸出:
2
2
3
0
我會寫這樣的東西:
for (int i = 0; i < row; i ) { // for each city
int count = 0;
for (int j = 1; j < col; j ) { // I'm starting here with i=1, because I can't compare day 0 with day -1 (day -1 doesn't exist)
if (matrix[i][j] > matrix[i][j - 1]) // If the temperature was higher than previous day
count ; // then add one to our counter
}
printf("%d\n", count); // print our count
}
這是我檢查此解決方案的完整代碼,以防您需要它:
#include <stdio.h>
int main() {
int row=4, col=4;
int matrix[4][4] = {
{20, 27, 28, 22},
{12, 22, 12, 20},
{22, 24, 25, 33},
{33, 30, 30, 22},
};
for (int i = 0; i < row; i ) {
int count = 0;
for (int j = 1; j < col; j ) {
if (matrix[i][j] > matrix[i][j - 1])
count ;
}
printf("%d\n", count);
}
}
另外,如果您需要(我不知道您為什么要)保存此count以供以后使用,只需創建一個陣列,例如:
int counts[MAX_ARRAY_SIZE];
而不是列印我們的計數立即保存在計數中:
printf("%d\n", count); -> counts[i] = count;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393263.html
上一篇:如何使用C在二維陣列的特定行中查找大小元素之間的差異
下一篇:OpenBSD套接字編程
