Home > Software engineering >  Write a program in C to create a matrix A of order m×n based on run-time integer inputs and find all
Write a program in C to create a matrix A of order m×n based on run-time integer inputs and find all

Time:05-03

Write a program in C to create a matrix A of order m×n based on run-time integer inputs and find all elements A[i][j] such that it is smallest in the ith row and largest in the jth column. I am new to C and I have written a code for it but it's not working.

#include <stdio.h>

int main()
{
    int m,n,min,max;
    printf("Let the matrix be m x n\n Enter the value of m:");
    scanf("%d",&m);
    printf("Enter the value of n: ");
    scanf("%d",&n);
    int array1 [m][n];
    int min_i [m] ;
    int min_i_value [m] ;
    int max_j [n] ;
    int max_j_value [n] ;
    printf("Enter elements in the matrix");

    for (int i=0; i<m;   i){//taking input for matrix
        for(int j=0;j<n;  j){
            scanf("%d", &array1[i][j]);
        }
    }
    for(int i=0;i<m;  i){
        array1[i][0]=min;//calculating min value in a row
        for(int j=0;j<n;  j){
            if (array1[i][j]<min){
                array1[i][j]=min;
            }
        }
        min_i_value[i]=min;//putting the min value in array
        for (int j=0;j<n;  j){
            if (array1[i][j]==min){
                min_i[i]=j;//putting the index of min value in array
            }
        }
    }
    for(int j=0;j<n;  j){
        array1[0][j] = max;
        for(int i=0;i<m;i  ){
            if (array1[i][j]>max){
                array1[i][j]=max;
                
            }
            
        }
        max_j_value [j] = max;
        for(int i=0;i<m;i  ){
            if (array1[i][j]==max){
                max_j [j] = i;
            }
        }
    }
    for(int i=0;i<m;i  ){
        for(int j=0;j<n;j  ){
            if(min_i_value[i]==max_j_value[j]){
                printf("%d,(%d,%d)\n",max_j_value[j],min_i[i],max_j[j]);
                
            }
        }
    }
    
    
    return 0;
}

It is returning 0(0,2) and I can't figure out why.

CodePudding user response:

You aren't correctly calculating the min and max values for rows and columns. Your assignment should be reversed.

        array1[i][0]=min;//calculating min value in a row
        for(int j=0;j<n;  j){
            if (array1[i][j]<min){
                array1[i][j]=min;
            }
        }

should be

        min = array1[i][0];
        for(int j=0;j<n;  j){
            if (array1[i][j]<min){
                min = array1[i][j];
            }
        }

and likewise for the loop that calculates the max.

  • Related