You must Sign In to post a response.
  • Category: Software

    C program to find largest element in the row and column of matrix

    Please tell me the code for the above program
  • #137468
    This question can be understood in two ways. So, I am mentioning the C programs for both the cases with their sample outputs and explanations.

    To find the largest number of the matrix
    Program
    #include<stdio.h>
    #include<conio.h>
    int max(int a[][],int m,int n)
    {
    int i,j,max;
    max=a[0][0];
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    {
    if(a[i][j]>max)
    max=a[i][j];
    }
    }
    return max;
    }
    void main(void)
    {
    int a[25][25],m,n,i,j;
    clrscr();
    printf("\n Enter the order of matrix : ");
    scanf("%dx%d",m,n);
    printf("\n Enter the matrix row wise : ");
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    scanf("%d",&a[i][j]);
    }
    printf("\n Largest number of matrix is : %d",max(a,m,n));
    getch();
    }

    Output
    Enter the order of matrix : 3x3
    Enter the matrix row wise : 1 2 3 4 5 6 7 8 9
    Largest number of matrix is : 9

    Here, a function called 'max' is used to find the largest number in the matrix. A variable 'max' is considered and the value at position (0,0) is put in it. Then, it is compared with every number in the matrix and replaced if a larger number is found. Eventually, the largest number of the matrix gets into it.

    To find the largest number in every row and every column of the matrix
    Program
    #include<stdio.h>
    #include<conio.h>
    int max(int a[],int n)
    {
    int i,max;
    max=a[0];
    for(i=0;i<n;i++)
    {
    if(a[i]>max)
    max=a[i];
    }
    return max;
    }
    void main(void)
    {
    int a[25][25],b[25],m,n,i,j;
    clrscr();
    printf("\n Enter the order of matrix : ");
    scanf("%dx%d",m,n);
    printf("\n Enter the matrix row wise : ");
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    scanf("%d",&a[i][j]);
    }
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    b[j]=a[i][j];
    printf("\n Largest number of row %d : %d",i+1,max(b,n));
    }
    for(i=0;i<n;i++)
    {
    for(j=0;j<m;j++)
    b[j]=a[i][j];
    printf("\n Largest number of column %d : %d",i+1,max(b,m));
    }
    getch();
    }

    Output
    Enter the order of matrix : 3x3
    Enter the matrix row wise : 1 2 3 4 5 6 7 8 9
    Largest number of row 1 : 3
    Largest number of row 2 : 6
    Largest number of row 3 : 9
    Largest number of column 1 : 7
    Largest number of column 2 : 8
    Largest number of column 3 : 9

    Here, each row and each column of the matrix are copied to a single dimensional array and the largest number in that single dimensional array is found using the 'max' function. A variable 'max' is considered and the value at position 0 is put in it. Then, it is compared with every number in the single dimensional array and replaced if a larger number is found.

    Hope this answer helps.

  • #139497
    This C program is awesome!Helped me a lot. Except one small thing:

    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    for(i=0;i{
    for(j=0;jb[j]=a[i][j];
    printf("\n Largest number of row %d : %d",i+1,max(b,n));
    }
    for(i=0;i{
    for(j=0;j-------------
    b[j]=a[i][j];
    ^^^^^^^^^^
    /*This part the same as rows.You need to change it for coloumn "b[j] = a[j][i]" so instead of 'i' 'j' use 'j' 'i' */

    """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

  • #140289
    Below is the program to find out largest number from give matrix(i.e. from given rows and columns). In C or C++ there is a provision to represent the data in row and column format known as multidimensional array representation.
    Program to find out greatest number from row and column of matrix.

    please include stdio.h and conio.h header files respectively.

    #include
    #include
    void main(void)
    {
    int arr[2][3],i,j,max;
    clrscr();
    printf("Enter any six elements one by one\n ");
    for(i=0;i<2;i++)
    {
    for(j=0;j<3;j++)
    {
    scanf("%d",&arr[i][j]);
    }
    }
    printf("Elements of the array are as follows\n");
    for(i=0;i<2;i++)
    {
    for(j=0;j<3;j++)
    {
    printf("%d ",arr[i][j]);
    }
    printf("\n\n");
    }
    max=arr[0][0];
    for(i=0;i<2;i++)
    {
    for(j=0;j<3;j++)
    {
    if(arr[i][j]>max)
    {
    max=arr[i][j];
    }
    }
    }
    printf("Maximum element of array is %d",max);
    getch();
    }

    In the above program a matrix of 2 x 3 elements is represented by using double dimensional array. In the first part of program variable declaration is done. in next session by using two nested for loops, elements of array get inputted by user. Then after array elements will be displayed in matrix form by using two nested for loops. (Actually in memory array elements were stored in contiguous memory locations).
    After that zeroth element of the array will be declared as maximum number and then comparing this maximum number with rest of the array elements gives actual maximum number from the matrix.


Sign In to post your comments