This program allows the user to input three integers on one dimensional array, nine integers on multidimensional array with three rows and columns. Third array will add the product on two array.
-
Run the Program: Execute the compiled program named matrix_algebra.
-
Input Values:
- One Dimensional Array
- The program will prompt you to enter three integers for Array A.
- Multidimensional Array
- Next Array will prompt you to enter nine integers for Array B which consists of 3 rows and 3 columns.
-
Processing:
- The program will create additional Array which is the product of two other matrices with the following formula:
C[0] = {(A[0] * B[0][0]) + (A[1] * B[0][1]) + (A[2] * B[0][2])} C[1] = {(A[0] * B[1][0]) + (A[1] * B[1][1]) + (A[2] * B[1][2])} C[2] = {(A[0] * B[2][0]) + (A[1] * B[2][1]) + (A[2] * B[1][2])}
-
Output Display:
-
Array N and Array B will be displayed.
-
The matrix algebra (matrix_algebra) will also be displayed.
-
ARRAY A:
Enter element for A[0]: 1
Enter element for A[1]: 2
Enter element for A[2]: 3
ARRAY B:
Enter element for B[0][0]: 1
Enter element for B[0][1]: 2
Enter element for B[0][2]: 3
Enter element for B[1][0]: 4
Enter element for B[1][1]: 5
Enter element for B[1][2]: 6
Enter element for B[2][0]: 7
Enter element for B[2][1]: 8
Enter element for B[2][2]: 9
ARRAY A:
A[0] = 1 A[1] = 2 A[2] = 3
ARRAY B:
B[0][0] = 1 B[0][1] = 2 B[0][2] = 3
B[1][0] = 4 B[1][1] = 5 B[1][2] = 6
B[2][0] = 7 B[2][1] = 8 B[2][2] = 9
ARRAY C:
C[0] = 14 C[1] = 32 C[2] = 50
-
Only integer values should be entered.
-
No sorting is applied to the final merged array.
Write a program that will input 3 integers in an array A(one dimensional - 3 digits) and another 9 integers in an array B(multidimensional - 3 rows and 3 columns). After which process two arrays in one array called matrix_algebra such that on each elements of Array C are the products of two matrices:
Formula for Array C:
C[0] = {(A[0] * B[0][0]) + (A[1] * B[0][1]) + (A[2] * B[0][2])}
C[1] = {(A[0] * B[1][0]) + (A[1] * B[1][1]) + (A[2] * B[1][2])}
C[2] = {(A[0] * B[2][0]) + (A[1] * B[2][1]) + (A[2] * B[1][2])}
C++ Programming
Ariel Rivera
================================================================
Author Date Description Version
----------------------------------------------------------------
Ariel Rivera 2025-03-23 Initial Build 1.0.0
================================================================
Working...
-
Declaration: Constant Declaration:
_size = 3
: Defines the maximum array size to ensure sufficient storage space for merging.
Variable Declaration:
A[_size]
: Stores the one dimensional array.B[_size][_size]
: Stores the multidimensional array.C[_size]
: Stores the processed array.
-
User Input:
- The user is prompted to enter
three
integers for arrayA
. - The user is then prompted to enter
nine
integers for arrayB
. - No Error Validation added. User must refrain from entering out-of-bound data from the array. Can be enhancement feature
- The user is prompted to enter
-
Process:
- Each element of
C
is the product of elements ofA[index]
andB[rows][columns]
.
C[0] = {(A[0] * B[0][0]) + (A[1] * B[0][1]) + (A[2] * B[0][2])} C[1] = {(A[0] * B[1][0]) + (A[1] * B[1][1]) + (A[2] * B[1][2])} C[2] = {(A[0] * B[2][0]) + (A[1] * B[2][1]) + (A[2] * B[1][2])}
- Each element of
-
Output:
- The program prints the original
A
andB
arrays. - The program then prints the
matrix_algebra
containing the product of two matrices.
- The program prints the original
-
Error Handling:
- User input where separated on each line to prevent the user on entering out of bound data in an array.
#include <iostream>
using namespace std;
- using
iostream
library to handle input/output program. - using
namespace std
to allow the developer to use standard library features (likecout
,cin
, andendl
) without having to prefix them withstd::
.
// asking user to input data in one dimensional array
cout << "ARRAY A: " << endl;
for (int i = 0; i < _size; i++)
{
cout << "Enter elements for A[" << i << "]: ";
cin >> A[i];
}
// asking user to input data in multidimensional array
cout << endl
<< "ARRAY B: " << endl;
for (int rows = 0; rows < _size; rows++)
{
for (int cols = 0; cols < _size; cols++)
{
cout << "Enter elements for B[" << rows << "][" << cols << "]: ";
cin >> B[rows][cols];
}
}
- The program will ask the user to input
3
elements on Array A. - The program will ask the user to input
9
elements on Array B.
for (int rows = 0; rows < _size; rows++)
{
C[rows] = 0; // initialize current element to 0 to prevent from adding unnecessary value
for (int cols = 0; cols < _size; cols++)
{
C[rows] += (A[cols] * B[rows][cols]); // on each element of C, value of A[cols] * to value of B[rows][cols]
}
}
C[rows] = 0
was define to initialize the value of C[rows] to0
, preventing from adding unnecessary value.C[rows] += (A[cols] * B[rows][cols])
to get product of two arrays on each repitition.
// printing the one dimensional array
cout << endl
<< "ARRAY A: " << endl;
for (int i = 0; i < _size; i++)
{
cout << "A[" << i << "] = " << A[i] << "\t";
}
// printing the multidimensional array
cout << endl
<< endl
<< "ARRAY B: " << endl;
for (int rows = 0; rows < _size; rows++)
{
for (int cols = 0; cols < _size; cols++)
{
cout << "B[" << rows << "][" << cols << "] = " << B[rows][cols] << "\t";
}
cout << endl;
}
// printing the matrix algebra
cout << endl
<< "ARRAY C: " << endl;
for (int i = 0; i < _size; i++)
{
cout << "C[" << i << "] = " << C[i] << "\t";
}
Array A
,Array B
, andArray C
were display to ensure the program captured the correct data and process.
ARRAY A:
Enter element for A[0]: 1
Enter element for A[1]: 2
Enter element for A[2]: 3
ARRAY B:
Enter element for B[0][0]: 1
Enter element for B[0][1]: 2
Enter element for B[0][2]: 3
Enter element for B[1][0]: 4
Enter element for B[1][1]: 5
Enter element for B[1][2]: 6
Enter element for B[2][0]: 7
Enter element for B[2][1]: 8
Enter element for B[2][2]: 9
ARRAY A:
A[0] = 1 A[1] = 2 A[2] = 3
ARRAY B:
B[0][0] = 1 B[0][1] = 2 B[0][2] = 3
B[1][0] = 4 B[1][1] = 5 B[1][2] = 6
B[2][0] = 7 B[2][1] = 8 B[2][2] = 9
ARRAY C:
C[0] = 14 C[1] = 32 C[2] = 50
- The program ensures that
matrix_algebra
get the product of two matrices. - Future enhancements could include sorting the merged array or allowing dynamic input sizes instead of fixed 5-element arrays.
- Future enhancements could include error handling when user input out-of-bound data in an array.