Maximum Element In Each Row
Write a program to find the maximum element in each row of the matrix.
Input Format:
The input consists of (m*n+2) integers.
The first integer corresponds to m, the number of rows in the matrix and the second integer corresponds to n, the number of columns in the matrix.
The remaining integers correspond to the elements in the matrix. The elements are read in row-wise order, the first row first, then second row and so on. Assume that the maximum value of m and n is 10.

Sample Input:
Enter the number of rows in the matrix
3
Enter the number of columns in the matrix
2
Enter the elements in the matrix
4 5
6 9
0 3
Sample Output:
5
9
3

Program Code:
#include<stdio.h>
#include<malloc.h>
int** create(int m, int n)
{
    int **a, i;
    a= (int **)malloc(m*sizeof(int *));
    for(i=0; i<m; i++)
    {
        *(a+i) = (int *)malloc(n*sizeof(int ));
    }
    return a;
}
void read(int **a, int m, int n)
{
    int i, j;
    for(i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            scanf("%d", (*(a+i)+j));
        }
    }
}

void findMax(int** a,int m,int n)
{
 int i,j,max;

 for(i=0;i<m;i++)
 {
     max=-9999;
     for(j=0;j<n;j++)
     {
         if(max < (*(*(a+i)+j)))
         {
             max=*(*(a+i)+j);
         }
     }
     printf("%d\n",max);
 }
}
int main()
{
    int **a, m, n;
    printf("Enter the number of rows in the matrix\n");
    scanf("%d",&m);
    printf("Enter the number of columns in the matrix\n");
    scanf("%d",&n);
    printf("Enter the elements in the matrix\n");
    a = create(m,n);
    read(a,m,n);
    findMax(a,m,n);
    return 0;
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: