CSE1002 Row Maximum of a Matrix (Id-1246)
Given an nXn matrix with entries as numbers, write an algorithm and C program to print the maximum value in each row of the matrix.
Input Format
Value of 'n'
Element in first row first column
Element in first row second column
..
Element in the first row n-th column
Element in second row first column
Element in second row second column
..
Element in the second row n-th column
...
Element in nth row first column
Element in nth row second column
..
Element in nth row n-th column
Output Format
Maximum value in the first row
Maximum value in the second row
...
Maximum value in the n-th row
INPUT:
Read the value of n
Read the n*n elements of matrix
PROCESSING:
void main(){
int i,j,max,n;
scanf("%d",&n);
int a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(i==0||j==0)
{
max=a[i][j];
}
if(a[i][j]>max)
{
max=a[i][j];
}
}
printf("%d\n",max);
}
}
OUTPUT:
Display the maximum element in each row
PSUEDO CODE:
Step:1)Start
Step:2)Read the value of n
Step:3)Read the n*n values
Step:4)check for the maximum element in each row of the matrix
Step:5)Display the maximum element
Step:6)End.
C-CODE:
#include<stdio.h>
void main()
{
int i,j,max,n;
scanf("%d",&n);
int a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(i==0||j==0)
{
max=a[i][j];
}
if(a[i][j]>max)
{
max=a[i][j];
}
}
printf("%d\n",max);
}
}
0 Comments