You are on page 1of 2

#include <stdio.

h>
#define maxr 10
#define maxc 10
int main()
{
int r,i,j,c,choice,sum=0,sumdiag=0,min,max;
int a[maxr][maxc];
printf("Enter the number of row in the maxtrix: ");
scanf("%d",&r);
printf("Enter the number of column in the maxtrix: ");
scanf("%d",&c);
printf("Enter the element of maxtrix:\n");
for (i=0;i<r;i++)
{
for (j=0;j<c;j++)
{
printf("The value of the element a[%d],[%d]: ",i,j);
scanf("%d",&a[i][j]);
}
}
max = min = a[0][0];
for (i=0;i<r;i++)
{
for (j=0;j<c;j++)
{
if (a[i][j]>max) max=a[i][j];
if (a[i][j]<min) min=a[i][j];
if ((i+j)==(r-1)) sumdiag=sumdiag+a[i][j];
if (i==j) sumdiag=sumdiag+a[i][j];
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n");
printf("Choose the operation as follow:\n1. Calculate the total of elements of each rows.\n2.
Calculate the total of elements of each column.\n3. If m=n, calculate sum of all elements of the main
diagonal.\n4. Find the minimun and maximum element of the matrix.\n");
printf("\n");
scanf("%d",&choice);
printf("\n");
switch (choice)
{
case (1):
{
for (i=0;i<r;i++)
{
sum = 0;
for (j=0;j<c;j++)
{
sum = sum + a[i][j];
}
printf("Sum of row %d is: %d\n",i,sum);
}

break;
}
case (2):
{
for (j=0;j<c;j++)
{
sum = 0;
for (i=0;i<r;i++)
{
sum = sum + a[i][j];
}
printf("Sum of column %d is: %d\n",j,sum);
}
break;
}
case (3):
{
if (r != c) printf("Numbers of Rows and Columns must be equal to calculate");
else printf("Sum of the main diagonal: %d",sumdiag);
break;
}
case (4): printf("Max is %d\nMin is %d",max,min); break;
default: printf("Error");
}
return 0;
}

You might also like