Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

C++ Program to Perform Arithmetic Operations on Matrix

by suresh

Write a C++ Program to Perform Arithmetic Operations on Matrix with an example. In this C++ matrix arithmetic operations example, we allow users to enter the matrix sizes and matrixes items. Next, we used the C++ nested for loop to iterate matrix from 0 to rows and columns. Within the nested for loop, we performed arithmetic operations such as addition, division, subtraction, multiplication, and modules on both the matrixes and assigned them to new matrixes. Finally, we used one more nested for loop to print the matrix items.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter the rows and Columns of a Multi-Dimensional Array =  ";
	cin >> i >> j;
	
	int arr1[i][j], arr2[i][j], add[i][j], sub[i][j], mul[i][j], mod[i][j];
	float div[i][j];
	
	cout << "\nPlease Enter the First Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> arr1[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> arr2[rows][columns];
		}		
	}
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			add[rows][columns] = arr1[rows][columns] + arr2[rows][columns];
			sub[rows][columns] = arr1[rows][columns] - arr2[rows][columns];
			mul[rows][columns] = arr1[rows][columns] * arr2[rows][columns];
			mod[rows][columns] = arr1[rows][columns] / arr2[rows][columns];
		}
	}
	cout << "\nAdd\t Sub\t Mul\t Div\tMod \n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			cout << add[rows][columns] << "\t";
			cout << sub[rows][columns] << "\t";
			cout << mul[rows][columns] << "\t";
			cout << mod[rows][columns] << "\t";
			cout << div[rows][columns] << "\t\n";
		}
	}

 	return 0;
}
C++ Program to Perform Arithmetic Operations on Matrix 1

C++ Program to Perform Arithmetic Operations on Matrix Example 2

This C++ Matrix arithmetic operations program is the same as above. Here, we used the cout statement to show the result of nested for loop in a row and column iteration-wise. 

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
	
	cout << "\nPlease Enter the rows and Columns of a Multi-Dimensional Array =  ";
	cin >> i >> j;
	
	int arr1[i][j], arr2[i][j], add[i][j], sub[i][j], mul[i][j], mod[i][j];
	float div[i][j];
	
	cout << "\nPlease Enter the First Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> arr1[rows][columns];
		}		
	}	
	cout << "\nPlease Enter the Second Multi-Dimensional Array Items =  ";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> arr2[rows][columns];
		}		
	}
	for(rows = 0; rows < i; rows++)	{
		cout << "\nThe Result of the " << rows + 1 << " Row Iteration\n";
		for(columns = 0; columns < j; columns++) {
			add[rows][columns] = arr1[rows][columns] + arr2[rows][columns];
			sub[rows][columns] = arr1[rows][columns] - arr2[rows][columns];
			mul[rows][columns] = arr1[rows][columns] * arr2[rows][columns];
			mod[rows][columns] = arr1[rows][columns] / arr2[rows][columns];
			div[rows][columns] = arr1[rows][columns] % arr2[rows][columns];
			
			cout << "\nThe Result of the " << columns + 1 << " Column Iteration\n";
			cout << arr1[rows][columns] << " + " << arr2[rows][columns] << " = " << add[rows][columns] << "\n";
			cout << arr1[rows][columns] << " - " << arr2[rows][columns] << " = " << sub[rows][columns] << "\n";
			cout << arr1[rows][columns] << " * " << arr2[rows][columns] << " = " << mul[rows][columns] << "\n";
			cout << arr1[rows][columns] << " / " << arr2[rows][columns] << " = " << mod[rows][columns] << "\n";
			cout << arr1[rows][columns] << " % " << arr2[rows][columns] << " = " << div[rows][columns] << "\n";
		}
	}
	cout << "\nAdd\t Sub\t Mul\t Div\tMod \n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < j; columns++) {
			cout << add[rows][columns] << "\t";
			cout << sub[rows][columns] << "\t";
			cout << mul[rows][columns] << "\t";
			cout << mod[rows][columns] << "\t";
			cout << div[rows][columns] << "\t\n";
		}
	}

 	return 0;
}
C++ Program to Perform Arithmetic Operations on Matrix 2

Placed Under: CPP Examples

  • C++ Print Hello World
  • C++ Add Two Numbers
  • C++ Count Notes in Amount
  • C++ Find Last Digit of a Number
  • C++ Largest of Two Nums
  • C++ Largest of Three Nums
  • C++ LCM of Two Numbers
  • C++ Leap Year
  • C++ Number Divisible by 5 & 11
  • C++ Palindrome Number
  • C++ Perfect Number
  • C++ Prime Factors of a Number
  • C++ Print 1 to 100
  • C++ Print Alphabets from A and Z
  • C++ Print Alphabets from a to z
  • C++ Print Even Numbers
  • C++ Print Multiplication Table
  • C++ Print N Natural Numbers
  • C++ Print Odd Numbers
  • C++ Product of Digits in a Num
  • C++ Profit or Loss Program
  • C++ Reverse a Number
  • C++ Simple Interest
  • C++ Square of a Number
  • C++ Square Root of a Number
  • C++ Standard Deviation
  • C++ Strong Number
  • C++ Student Marks Program
  • C++ Student Grade Program
  • C++ Sum of Digits in Number
  • C++ Sum of Even Numbers
  • C++ Sum of Even & Odd
  • C++ Sum of Odd Numbers
  • C++ Swap Two Numbers
  • C++ Natural Nums in Reverse
  • C++ Sum of Natural Numbers
  • C++ Sum of number 1st, last digit
  • C++ Swap of 1st & last digit
  • C++ Sum of Series 1²+2²+3²+n²
  • C++ Sum of Series 1³+2³+3³+n³
  • C++ ASCII Values of all Chars
  • C++ Sum of Char array ASCII val
  • C++ Sum of String ASCII values
  • C++ String Length
  • C++ Convert String to Lowercase
  • C++ Convert String to Uppercase
  • C++ Toggle String Char Cases
  • C++ Convert CM to Meters & KM
  • C++ Convert KM to M, CM & MM
  • C++ Convert days to year & Week
  • C++ Add Two Arrays
  • C++ Add Two Matrixes
  • C++ Array Arithmetic Operations
  • C++ Check 2 Matrixes are Equal
  • C++ Determinant of a Matrix
  • C++ Identity Matrix
  • C++ Matrix Arithmetic Operation
  • C++ Multiply Two Arrays
  • C++ Subtract two Matrixes
  • C++ Matrix Diagonal Interchange
  • C++ Matrix Lower Triangle
  • C++ Matrix Scalar Multiplication
  • C++ Matrix Transpose
  • C++ Matrix row & column sum
  • C++ Matrix Opp Diagonal sum
  • C++ Multiply two Matrixes
  • C++ Matrix Upper Triangle
  • C++ Sparse Matrix
  • C+ Symmetric Matrix
  • C++ Sum of Matrix Diagonal
  • C++ Vol & Surf Area of Cone
  • C++ Vol & Surf Area of Cube
  • C++ Vol & Surf Area of Cuboid
  • C++ Vol & Surf Area of Cylinder
  • C++ Vol & Surf Area of Sphere
  • C++ Inverted Star Pyramid
  • C++ Square Star Pattern
  • C++ Triangle is Equilateral Isosceles or Scalene
  • C++ Triangle is Valid using Angle
  • C++ Triangle is Valid using Sides
  • C++ Perimeter of a Rectangle
  • C++ Max Occur String Character
  • C++ Hollow Box Number Pattern
  • C++ Print 1 and 0 Row Pattern
  • C++ Print Pascal Triangle
  • C++ Rectangle Star Pattern
  • C++ Square Number Pattern
  • C++ Print 1, 0 Column Pattern

Copyright © 2021· All Rights Reserved.
About | Contact | Privacy Policy