Write a C++ Program to Perform Scalar Matrix Multiplication with an example. This C++ scalar matrix multiplication program allows entering rows, columns, matrix items, and the multiplication number. Next, it uses a nested for loop to multiply each row and column with this number. And the final nested for loop is to print the Scalar Matrix Multiplication result.
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns, number;
cout << "\nPlease Enter the Matrix rows and Columns = ";
cin >> i >> j;
int scalarMulArr[i][j], multi[i][j];
cout << "\nPlease Enter the Matrix Items = ";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> scalarMulArr[rows][columns];
}
}
cout << "\nPlease Enter the Multiplication Value\n";
cin >> number;
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
multi[rows][columns] = number * scalarMulArr[rows][columns];
}
}
cout << "\nThe Result of a Scalar Matrix Multiplication is :\n";
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
cout << multi[rows][columns] << " ";
}
cout << "\n";
}
return 0;
}
C++ Program to Perform Scalar Matrix Multiplication Example 2
Instead of using another nested for loop to display the Scalar Matrix Multiplication result, we used cout (cout << multi[rows][columns] << ” “;) within the same loop.
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns, number;
cout << "\nPlease Enter the Matrix rows and Columns = ";
cin >> i >> j;
int scalarMulArr[i][j], multi[i][j];
cout << "\nPlease Enter the Matrix Items\n";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> scalarMulArr[rows][columns];
}
}
cout << "\nPlease Enter the Multiplication Value = ";
cin >> number;
cout << "\nThe Result of a Scalar Matrix Multiplication is :\n";
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
multi[rows][columns] = number * scalarMulArr[rows][columns];
cout << multi[rows][columns] << " ";
}
cout << "\n";
}
return 0;
}
In this C++ scalar matrix multiplication example, we used extra cout statements to show you the row, column, and multiplication values at each iteration.
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns, number;
cout << "\nPlease Enter the Matrix rows and Columns = ";
cin >> i >> j;
int scalarMulArr[i][j], multi[i][j];
cout << "\nPlease Enter the Matrix Items\n";
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < i; columns++) {
cin >> scalarMulArr[rows][columns];
}
}
cout << "\nPlease Enter the Multiplication Value = ";
cin >> number;
for(rows = 0; rows < i; rows++)
{
cout << "\nRow Iteration = " << rows + 1 << ", Row Number = " << rows;
for(columns = 0; columns < j; columns++)
{
cout << "\nColumn Iteration = " << columns + 1 << ", Column Number = " <<
columns << ", and Row Number = " << rows;
multi[rows][columns] = number * scalarMulArr[rows][columns];
cout << "\nscalarMulArr["<<rows<<"]["<< columns <<"] = " << scalarMulArr[rows][columns] <<
" and number * scalarMulArr["<<rows<<"]["<< columns <<"] = " << multi[rows][columns] << endl;
}
}
cout << "\nThe Result of a Scalar Matrix Multiplication is :\n";
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
cout << multi[rows][columns] << " ";
}
cout << "\n";
}
return 0;
}