0% found this document useful (0 votes)
85 views4 pages

1) C Program To Find Sum of Each Row of A Matrix Using 2D Array and Using User Defined Function

This document contains 3 C programs that calculate sums of rows and columns of a 2D array matrix. The first program calculates row sums by passing the matrix and dimensions to a function. The second program calculates column sums in a similar way. The third program enhances this by calculating both row and column sums using a single function, and displaying the results together.

Uploaded by

bkmurthy78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views4 pages

1) C Program To Find Sum of Each Row of A Matrix Using 2D Array and Using User Defined Function

This document contains 3 C programs that calculate sums of rows and columns of a 2D array matrix. The first program calculates row sums by passing the matrix and dimensions to a function. The second program calculates column sums in a similar way. The third program enhances this by calculating both row and column sums using a single function, and displaying the results together.

Uploaded by

bkmurthy78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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