Write the C++ program to find the sum of each row and columns of a matrix.
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)?
Logic
Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Latest posts by Prof. Fazal Rehman Shamil (see all)
- How Students Can Pick a Good Custom-Paper Writing Service? - March 5, 2021
- List of Public service commissions - August 31, 2020
- Comparison of fee structure of Pakistani Universities - June 1, 2020