Largest & smallest matrix number using DMA - C

Write a 'C' Program to accept m X n matrix and find the largest and smallest number from the matrix using dynamic memory allocation.

Solution:

#include<stdio.h>
#include<stdlib.h>
int main()
{
      int **a, row,col,i,j,l,s;
      printf("Enter Limit for Rows : ");
      scanf("%d",&row);
      printf("\nEnter Limit for Columns : ");
      scanf("%d",&col);
      a=(int **)malloc(row*sizeof(int*));
      for(i=0;i<row;i++)
      {
            a[i]=(int *)malloc(col*sizeof(int));
      }
      printf("\nEnter Elements for Matrix of Size %d*%d:\n\n",row,col);
      for(i=0;i<row;i++)
      {
            for(j=0;j<col;j++)
            {
                  scanf("%d",&a[i][j]);
            }
      }
      l=a[0][0];
      s=a[0][0];
      for(i=0;i<row;i++)
      {
            for(j=0;j<col;j++)
            {
                  if(l<a[i][j])
                        l=a[i][j];
                  if(s>a[i][j])
                        s=a[i][j];
            }
      }
      printf("\n%d*%d Matrix : \n\n",row,col);
      for(i=0;i<row;i++)
      {
            for(j=0;j<col;j++)
            {
                  printf("%3d ",a[i][j]);
            }
            printf("\n");
      }
      printf("\nSmallest Element in Matrix : %d", s);
      printf("\n\nLargest Element in Matrix  : %d" , l);
      return 0;
}


Output:

large small matrix no