Finding Maximum Element in Diagonal of a Metrics

Problem Statement
Write a C program to find out the largest element in the main diagonal and the smallest element in the secondary diagonal in a matrix .
#include<stdio.h> #define maxi 3 /* i denote row */ #define maxj 3 /* j denotes column */ int main() { int a[maxi][maxj]; int k,p,sum,max,min,n; printf("enter the dimention of metrics i.e. in case of n*n only enter n \n"); scanf("%d",&n); printf("enter the values row vise \n "); for(k=0;k<n;k++) { for(p=0;p<n;p++) { scanf("%d",&a[k][p]); } } sum=0; max=a[0][0]; for(k=0,p=0;k<n;k++,p++) { sum=sum+a[k][p]; if(a[k][p]>max) { max=a[k][p]; } } printf("the sum of main diagonal is %d and the max value is %d \n",sum,max); sum=0; min=a[n-1][0]; for(k=n-1,p=0;p<n;k--,p++) { sum=sum+a[k][p]; if(a[k][p]<min) { min=a[k][p]; } } printf("the sum of secound diagonal is %d and the min element is %d \n",sum,min); return 0; }

No comments:

Post a Comment