Site icon T4Tutorials.com

C++ program to find sum of each row and columns of a matrix

Write the C++ program to find the sum of each row and columns of a matrix.

Logic

logic of C++ prgoram to show sum of each row and columns of a matrix 2d array

Program

#include <iostream>
using namespace std;
int main()
{
	int size=2;
    int a[size][size];
    int row, col, sum;
    cout<<"Please Enter elements in array of size "<<size<<"x"<<size<<endl<<endl;
    for(row=0; row<size; row++)
    {
        for(col=0; col<size; col++)
        {
            cin>>a[row][col];
        }
    }
     for(row=0; row<size; row++)
    {
        for(col=0; col<size; col++)
        {
            cout<<a[row][col]<<" ";
        }
        cout<<endl;
    }
    for(row=0; row<size; row++)
    {
        sum = 0;
        for(col=0; col<size; col++)
        {
            sum = sum + a[row][col];
        }
        cout<<"Sum of elements of Row: "<< row+1<<" is "<< sum<<endl;
    }    
    for(col=0; col<size; col++)
    {
        sum = 0;
        for(row=0; row<size; row++)
        {
            sum += a[row][col];
        }
        cout<<"Sum of elements of Column: " <<row+1<<" is "<<sum<<endl;
    }
}

 

Output

C++ program to find sum of each row and columns of a matrix

 1

sum of rows in 2d array C++
sum of rows in 2d array C++

 2

sum of rows in two dimensinal array C++
sum of rows in two dimensinal array C++

3

sum of column in 2d array in C++
sum of column in 2d array in C++

4

sum of columns in array C++
sum of columns in array C++

5

matrices with given row and column sums C++
matrices with given row and column sums C++

6

EXPLANATION OF row sum and column sum of a matrix in c++
EXPLANATION OF row sum and column sum of a matrix in c++

7

sum of elements in 2d array with for loop
sum of elements in 2d array with for loop

8

C++ Array complex examples
C++ Array complex examples

9

C++ array examples for papers and examinations
C++ array examples for papers and examinations

10

2 dimensional array C++ programming
2 dimensional array C++ programming

11

exercise of 2 dimensional array
exercise of 2 dimensional array

12

dry run explanation of 2 dimensional array
dry run explanation of 2 dimensional array

13

two dimensional array diagram
two-dimensional array diagram

14

C++ adding rows and colums of two dimensional array
C++ adding rows and colums of two dimensional array

YouTube video player

C++ program to find sum of each row and columns of a matrix using user define function

// C++ program to find the Addition_Result of each row and column of a matrix

#include <iostream>
using namespace std;

// Get the size m and n
#define m 6
#define n 6

// Function to find Addition_Result of each row
void Adding_Rows(int T4Tutorials[m][n])
{

	int i,j,Addition_Result = 0;

	cout << "\nFinding Addition_Result of each row:\n\n";

	// finding the row Addition_Result
	for (i = 0; i < m; ++i) {
		for (j = 0; j < n; ++j) {

			// Add the element
			Addition_Result = Addition_Result + T4Tutorials[i][j];
		}

		// display the row Addition_Result
		cout
			<< "Addition_Result of the row "
			<< i << " = " << Addition_Result
			<< endl;

		// Reset the Addition_Result
		Addition_Result = 0;
	}
}

// User define Function to find Addition_Result of each column
void column_Addition_Result(int T4Tutorials[m][n])
{

	int i,j,Addition_Result = 0;

	cout << "\nFinding Addition_Result of each column:\n\n";

	// finding the column Addition_Result
	for (i = 0; i < m; ++i) {
		for (j = 0; j < n; ++j) {

			// Add the element
			Addition_Result = Addition_Result + T4Tutorials[j][i];
		}

		// display the column Addition_Result
		cout
			<< "Addition_Result of the column "
			<< i << " = " << Addition_Result
			<< endl;

		// Reset the Addition_Result
		Addition_Result = 0;
	}
}

// Driver code
int main()
{

	int i,j;
	int T4Tutorials[m][n];

	// Get the matrix elements
	int x = 1;
	for (i = 0; i < m; i++)
		for (j = 0; j < n; j++)
			T4Tutorials[i][j] = x++;

	// Get each row Addition_Result
	Adding_Rows(T4Tutorials);

	// Get each column Addition_Result
	column_Addition_Result(T4Tutorials);

	return 0;
}

Output

Finding Addition_Result of each row:

Addition_Result of the row 0 = 21
Addition_Result of the row 1 = 57
Addition_Result of the row 2 = 93
Addition_Result of the row 3 = 129
Addition_Result of the row 4 = 165
Addition_Result of the row 5 = 201

Finding Addition_Result of each column:

Addition_Result of the column 0 = 96
Addition_Result of the column 1 = 102
Addition_Result of the column 2 = 108
Addition_Result of the column 3 = 114
Addition_Result of the column 4 = 120
Addition_Result of the column 5 = 126

——————————–

Write a C++ program to read elements in a two-dimensional array (2D Array) and find the sum of elements of each row and columns of the matrix.

Write a C++ program to read numbers in a matrix and show the sum of each row and columns of a two-dimensional array (2D Array).

C++ program to calculate the sum of rows and columns of a matrix. What is the Logic to find the sum of each row and columns in a two-dimensional array (2D Array)?
shamil memory table

Exit mobile version