Home > OS >  How to print a specific row from an 2D array which has the highest difference among other rows
How to print a specific row from an 2D array which has the highest difference among other rows

Time:12-26

I was trying to solve a question. And it said that I can get user input with some numbers into an array and display that pair of those number which’s difference is highest. I have solved the differences of all the rows line by line but now I'm unable to print that specific row which has the highest difference among other rows.

Here's my piece of code -

#include<stdio.h>
#include<limits>

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 numbers you want to enter: ");
    scanf("%d", &num);

    printf("\nEnter %d numbers: ", num);

    for(int i=0; i<row; i  )
    {
        for(int j=0; j<column; j  )
        {
            scanf("%d", &array1[i][j]);
        }
        printf("\n");
    }

    for(int i=0; i<row; i  )
    {
        int largeA = array1[i][0];
        int smallA = array1[i][0];
        int highEleInArray = array1[0][0];

        for(int j=0; j<column; j  )
        {
            if(array1[i][j] > largeA)
            {
                largeA = array1[i][j];
            }
            if(array1[i][j] < smallA)
            {
                smallA = array1[i][j];
            }
        }
       unsigned diff = 0u    largeA - smallA;
        printf("%d\n", diff);

        int flag = 0;

        for(int j=0; j<i; j  )
        {
            flag = 1;
            //check if there's any similar column
            for(int k=0; k<=column; k  )
            {
                if(array1[i][k] != array1[i][k])
                {
                    flag = 0;
                }
                if(flag == 1)
                {
                    break;
                }
            }

            //if none of the row is similar
            if(flag == 0)
            {
                for(int j=0; j<column; j  )
                {
                    if(array1[i][j] > highEleInArray)
                    {
                        if(highEleInArray == diff)
                        {
                               highEleInArray = array1[i][j];
                               printf("\n%d\n", highEleInArray);
                        }
                    }
                }
            }
        }
    }

    return 0;
}

I was expecting this output -

Sample Input

20 27 28 22

12 22 12 20

22 24 25 33

33 30 30 29

Sample Output

8

10

11

4

22 24 25 33 has the highest difference which is 11.

CodePudding user response:

All you need to do is remember the index of the highest row, as well as the difference, like this:

unsigned highdiff = 0;
int highrow = 0;

...

    if (diff > highdiff)) {
        highdiff = diff;
        highrow = i;
    }
    
...

for(int j=0; j<column; j  ) {
    printf("%d ", array1[highrow][j]);
}
printf("has the highest difference which is %u\n", highdiff);
  •  Tags:  
  • c
  • Related