【问题标题】:How to check if elements in a row of an array is larger than previous element of a particular row如何检查数组行中的元素是否大于特定行的前一个元素
【发布时间】: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;
}

【问题讨论】:

  • 该输入与代码所期望的不匹配。您是否遗漏了 rowcolumnnum 的值?

标签: c multidimensional-array


【解决方案1】:

基本上,您可以在每行的开头使用一个计数器变量,如果数组中的前一个元素小于当前元素,那么您可以将一个计数器变量添加到计数器变量中。在每一行的末尾,可以显示计数器变量的计数。

  1. 循环条件不准确,您从第 1 个索引而不是第 0 个索引开始。此外,它应该是i &lt;= row-1,但你写了i &lt; 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;
}

【讨论】:

  • i&lt;=row-1 是一种不太清楚的说法 i &lt; row,这是大多数人对循环条件的期望。
【解决方案2】:

由于您专门询问了比较,因此我将跳过此答案中的 输入。我假设你有一个 row x col ma​​trix,每行代表一个城市,每列代表一个日间温度测量。
所以,如果我有像你这样的矩阵:

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];

而不是立即打印我们的计数,而是将其保存在 counts 中:

printf("%d\n", count); -> counts[i] = count;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-29
    • 2020-10-13
    • 2017-03-15
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多