如何检查数组一行中的元素是否大于特定行的前一个元素
How to check if elements in a row of an array is larger than previous element of a particular row
我被要求用 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;
}
基本上你可以在每一行的开头取一个计数器变量,如果数组中的前一个元素小于当前元素,那么你可以将一个计数器变量加一。在每一行的末尾,可以显示计数器变量的计数。
- 循环条件不准确,您是从第 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;
}
由于您专门询问了比较,我将跳过此答案中的 input。我在这里假设你有一个 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);
}
}
此外,如果您需要(我不知道您为什么要)保存这个 counts 供以后使用,只需创建一个数组,例如:
int counts[MAX_ARRAY_SIZE];
而不是立即打印我们的计数,将其保存在 counts:
printf("%d\n", count); -> counts[i] = count;
我被要求用 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;
}
基本上你可以在每一行的开头取一个计数器变量,如果数组中的前一个元素小于当前元素,那么你可以将一个计数器变量加一。在每一行的末尾,可以显示计数器变量的计数。
- 循环条件不准确,您是从第 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;
}
由于您专门询问了比较,我将跳过此答案中的 input。我在这里假设你有一个 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);
}
}
此外,如果您需要(我不知道您为什么要)保存这个 counts 供以后使用,只需创建一个数组,例如:
int counts[MAX_ARRAY_SIZE];
而不是立即打印我们的计数,将其保存在 counts:
printf("%d\n", count); -> counts[i] = count;