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; }
Please Enter the Matrix rows and Columns = 3 3
Please Enter the Matrix Items
11 22 33
44 55 66
77 88 99
Please Enter the Multiplication Value = 8
The Result of a Scalar Matrix Multiplication is :
88 176 264
352 440 528
616 704 792
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; }
Please Enter the Matrix rows and Columns = 2 2
Please Enter the Matrix Items
10 20
30 40
Please Enter the Multiplication Value = 9
Row Iteration = 1, Row Number = 0
Column Iteration = 1, Column Number = 0, and Row Number = 0
scalarMulArr[0][0] = 10 and number * scalarMulArr[0][0] = 90
Column Iteration = 2, Column Number = 1, and Row Number = 0
scalarMulArr[0][1] = 20 and number * scalarMulArr[0][1] = 180
Row Iteration = 2, Row Number = 1
Column Iteration = 1, Column Number = 0, and Row Number = 1
scalarMulArr[1][0] = 30 and number * scalarMulArr[1][0] = 270
Column Iteration = 2, Column Number = 1, and Row Number = 1
scalarMulArr[1][1] = 40 and number * scalarMulArr[1][1] = 360
The Result of a Scalar Matrix Multiplication is :
90 180
270 360