You are on page 1of 4

1) C program to find Sum of each row of a matrix using 2D array and using user

defined function.
#include <stdio.h>
#include <string.h>
void sum_of_rows(int a[20][20], int m, int n)
{
int i,j,sum; int rows[10];
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum+=a[i][j];
}
rows[i]=sum;
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]) ;
}
printf("=%d",rows[i]);
printf("\n");
}

// function definition/ called definition


// loops to calculate sum of rows

// loops to display sum of rows

}
void main()
{
int m=2,n=2;
int a[20][20]={ {1,1},{2,2}};
sum_of_rows(a,m,n);
}
Output:
Executing the program....

1 1 =2
2 2 =4

//calling function

2) C program to find Sum of each column of a matrix using 2D array and using user
defined function.
#include <stdio.h>
#include <string.h>
void sum_of_columns(int a[20][20], int m, int n);
void main()
{
int m=2,n=2;
int a[20][20]={ {1,1},{2,2}};
sum_of_columns(a,m,n);
}

//calling function

void sum_of_columns(int a[20][20], int m, int n)


// function definition/ called definition
{
int i,j,sum; int columns[10];
for(j=0;j<n;j++)
// loops to calculate sum of columns
{
sum=0;
for(i=0;i<m;i++)
{
sum+=a[i][j];
}
columns[j]=sum;
}
for(j=0;j<n;j++)
// loops to display sum of columns
{
printf("addition of %d column\n",j+1);
for(i=0;i<m;i++)
{
printf("%d\n",a[i][j]) ;
}
printf("---------------------\n");
printf("%d\n",columns[j]);
}
}
Output:
addition of 1 column
1
2
--------------------3
addition of 2 column
1
2
--------------------3

3) C program to find Sum of each row and column of a matrix using 2D array and
using user defined function.
#include <stdio.h>
#include <string.h>
void sum(int a[10][10], int m,int n)
{
int i,j,sum;
int rows[10]={0},cols[10]={0};
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum+=a[i][j];
}
rows[i]=sum;
}
for(j=0;j<n;j++)
{
sum=0;
for(i=0;i<m;i++)
{

// function definition/ called definition

// loops to calculate sum of rows

// loops to calculate sum of columns

sum+=a[i][j];
}
cols[j]=sum;
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t\t\t",a[i][j]);
}
printf("=%d\n",rows[i]);
}
printf("------------------------\n");
for(j=0;j<n;j++)
{
printf("%d\t",cols[j]);
}
}
void main()
{
int m=3, n=3;
int a[10][10]={{1,2,3},{4,5,6},{7,8,9}};
sum(a,m,n);
}

// loops to display sum of rows and columns

//calling function

Output:
Executing the program....

1
2 3
=6
4
5 6
=15
7
8 9
=24
-----------------------12 15 18

You might also like