Functions - Matrix Maximum

QUESTION:

Write a program to find the maximum element in a matrix using functions.

Function specification:

int findMax(int **a, int m, int n)
The first argument corresponds to the pointer to the matrix.
The second argument corresponds to the number of rows in the matrix.
The third argument corresponds to the number of columns in the matrix.


Input and Output Format:
Assume that the maximum number of rows and columns in the matrix is 10.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output :
Enter the number of rows in the matrix
3
Enter the number of columns in the matrix
2
Enter the elements of the matrix
2
4
1
3
5
9
The matrix is
2 4
1 3
5 9
The maximum element in the matrix is 9

Function Definitions: 
int findMax (int **a, int m, int n) 
SOLUTION:

#include<stdio.h>
#include<stdlib.h>
int findMax(int**,int,int);
int main()
{
    int m,n,i,j,larg;
    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");
    int **a=(int**)malloc(m*sizeof(int *));
    for(i=0;i<m;i++)
        a[i]=(int *)malloc(n*sizeof(int));
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            scanf("%d\n",&a[i][j]);
    larg=findMax(a,m,n);
    printf("The matrix is\n");
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    printf("The maximum element in the matrix is %d\n",larg);
    return 0;
}
int findMax(int **a,int m,int n)
{
    int i,j,larg;
    larg=a[0][0];
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            if(larg<a[i][j])
                larg=a[i][j];
        }
    }
    return larg;
}

Comments

Popular posts from this blog

Implementing a Fixed size Queue of maximum size 5 using an array

Implementing a Dynamically sized stack using array

Functions - Null Matrix