【问题标题】:How to find difference between large and small element in a specific row of an 2D array using C如何使用 C 在二维数组的特定行中查找大元素和小元素之间的差异
【发布时间】: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 变量

标签: c multidimensional-array


【解决方案1】:

在for(i) 循环中每次设置最小值和最大值。

使用unsigned 数学来避免int 溢出。

// Not needed
// int largeA = array1[0][0];
// int smallA = array1[0][0];
// int diff = 0;

for(int i=0; i<row; i++)
{
    //int diff = 0;
    int largeA = array1[i][0]; // add
    int smallA = array1[i][0]; // add
    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;
    unsigned diff = 0u + largeA - smallA;
    // printf("%d\n", diff);
    printf("%u\n", diff);
}

【讨论】:

  • 它帮助了我。感谢您的解决方案。
  • 很确定你永远不会遇到温度整数溢出
  • @AlanBirtles 小任务的良好编码实践避免big bad ones - 你的电话。
【解决方案2】:
  1. 使用函数。它使代码更具可读性,不要在main 中编程
  2. 为索引使用正确的类型 (size_t)
  3. 在您测试您的代码时,您可以更轻松地避免用户输入。它将为您节省大量输入表格值的时间。
#include <stdio.h>

int getDiff(const size_t cols, const int *arr)
{
    int max = 0, min = 0;
    if(arr)
    {
        max = arr[0]; min = arr[0];
        for(size_t col = 1; col < cols; col++)
        {
            if(min > arr[col]) min = arr[col];
            else if(max < arr[col]) max = arr[col];
        }
    }
    return max - min;
}

int main(void)
{
    int temps[][4] =  {
        {20, 27, 28, 22,},
        {12, 22, 12, 20,},
        {22, 24, 25, 33,},
        {33, 30, 30, 29,},
    };

    for(size_t row = 0; row < 4; row++)
    {
        printf("Diff row %zu = %d\n", row, getDiff(4, temps[row]));
    }
}

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 2022-01-25
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    相关资源
    最近更新 更多