#include<stdio.h>
void read(int a[50][50],int r,int c);
void display(int a[50][50],int r,int c);
int sum(int a[50][50],int r,int c);
int main()
{
int a[50][50],r,c,Sum;
printf("Enter the row size of the matrix\n");
scanf("%d",&r);
printf("Enter the column size of the matrix\n");
scanf("%d",&c);
printf("Enter the matrix elements\n");
read(a,r,c);
printf("The matrix is:\n");
display(a,r,c);
Sum=sum(a,r,c);
printf("The sum of matrix elements is: %d\n",Sum);
}
void read(int a[50][50],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
}
void display(int a[50][50],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\n");
}
int sum(int a[50][50],int r,int c)
{
int i,j,sum=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sum=sum+a[i][j];
}
}
return sum;
}
Output:
void read(int a[50][50],int r,int c);
void display(int a[50][50],int r,int c);
int sum(int a[50][50],int r,int c);
int main()
{
int a[50][50],r,c,Sum;
printf("Enter the row size of the matrix\n");
scanf("%d",&r);
printf("Enter the column size of the matrix\n");
scanf("%d",&c);
printf("Enter the matrix elements\n");
read(a,r,c);
printf("The matrix is:\n");
display(a,r,c);
Sum=sum(a,r,c);
printf("The sum of matrix elements is: %d\n",Sum);
}
void read(int a[50][50],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
}
void display(int a[50][50],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\n");
}
int sum(int a[50][50],int r,int c)
{
int i,j,sum=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sum=sum+a[i][j];
}
}
return sum;
}
Output:
Comments
Post a Comment