Saturday, August 9, 2014

Largest and second largest of all rows and columns of a matrix.

This is a C program to find the largest and second largest element of all the rows and columns of a matrix. If you have any doubts, please feel free in asking.

#include<stdio.h>
main()
{
 int a[50][50],size,n,m,i,j,large,slarge;
 printf("enter size of matrix(row and column):");
 scanf("%d %d",&n,&m);
 printf("\n enter the elements:");
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
   scanf("%d",&a[i][j]);
 }
 printf("\n the inputed matrix is:\n");
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    printf("\t%d  ",a[i][j]);
   printf("\n");
  }
 for(i=0;i<n;i++)
  {
    slarge=a[i][0];
    large=a[i][0];
    for(j=0;j<m;j++)
     {
       if(large<a[i][j])
         large=a[i][j];
     }
    printf("\n the largest of row%d is %d",i+1,large);
    for(j=0;j<m;j++)
     {
       if(a[i][j]!=large)
       {
        if(slarge<a[i][j])
          slarge=a[i][j];
       }
     }
    printf("\nthe seceond largest of row%d is %d",i+1,slarge);
   }
 for(j=0;j<m;j++)
  {
    slarge=a[0][j];
    large=a[0][j];
   


for(i=0;i<n;i++)
    {
      if(large<a[i][j])
        large=a[i][j];
    }
    printf("\n the largest of column %dis %d",j+1,large);
    for(i=0;i<n;i++)
     {
       if(a[i][j]!=large)
        {
          if(slarge<a[i][j])
            slarge=a[i][j];
        }
     }
    printf("\nthe second largest of column %dis %d\n",j+1,slarge);
  }
 }

No comments:

Post a Comment