Home > database >  How to find difference between large and small element in a specific row of an 2D array using C
How to find difference between large and small element in a specific row of an 2D array using C

Time:12-25

Write a C program which can four cities temperature for last five days and display the difference between the highest and lowest temperature for each city

Sample Input

20 27 28 22
12 22 12 20
22 24 25 33
33 30 30 29

Sample Output

8
8
11
4

I was looking for the difference between large and small element in a particular row of an 2D array using C. I'm able to find large and small element but I'm kind of lost in between searching a particular row to find the difference between large and small element. Below I'm showing my piece of code -

#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;
}

CodePudding user response:

Set the min and max each time in the for(i) loop.

Use unsigned math to avoid int overflow.

// 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);
}

CodePudding user response:

  1. Use functions. It makes code more readable, do not program in main
  2. Use the correct type for indexes (size_t)
  3. It will be much easier for you to avoid user input wnen you test your code. It will save you a lot of time spent on entering the table values.
#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]));
    }
}
  • Related