C++ Dynamically allocate two 2D arrays of appropriate sizes and print the result
ID: 3575408 • Letter: C
Question
C++
Dynamically allocate two 2D arrays of appropriate sizes and print the result of matrix subtraction. Ask the user to enter the sizes and the initial values for your arrays. The arrays must be allocated dynamically (using pointers) and you must use pointers to access the arrays throughout your program. Write a function that computes the matrix subtraction.
The arguments should include both array parameters. Print both input arrays, their sizes, and the results. Also see few other requirements:
• Your program should always run unless asked by the user.
• If the user enters incorrect sizes, you must ask for alternate sizes.
• Arrays should allow both positive and negative values.
Explanation / Answer
Here is the code for you:
#include <iostream>
#include <cstdlib>
using namespace std;
//Write a function that computes the matrix subtraction.
void MatrixSubtract(int **A, int **B, int **C, int numRows, int numCols)
{
for(int i = 0; i < numRows; i++)
{
for(int j = 0; j < numCols; j++)
*(*(C+i)+j) = *(*(A+i)+j) - *(*(B+i)+j);
}
}
int main()
{
//Ask the user to enter the sizes and the initial values for your arrays.
int numRows1, numRows2, numCols1, numCols2;
cout<<"Enter the number of rows for matrix 1: ";
cin>>numRows1;
cout<<"Enter the number of columns for matrix 1: ";
cin>>numCols1;
cout<<"Enter the number of rows for matrix 2: ";
cin>>numRows2;
cout<<"Enter the number of columns for matrix 2: ";
cin>>numCols2;
while(numRows1 != numRows2 || numCols1 != numCols2)
{
cout<<"Matrix dimensions must agree to perform subtraction. Please enter valid dimensions."<<endl;
cout<<"Enter the number of rows for matrix 1: ";
cin>>numRows1;
cout<<"Enter the number of columns for matrix 1: ";
cin>>numCols1;
cout<<"Enter the number of rows for matrix 2: ";
cin>>numRows2;
cout<<"Enter the number of columns for matrix 2: ";
cin>>numCols2;
}
int **A = (int **)malloc((numRows1)*sizeof(int *));
for (int i=0;i<numRows1;i++)
A[i] = (int *)malloc((numCols1)*sizeof(int));
int **B = (int **)malloc((numRows1)*sizeof(int *));
for (int i=0;i<numRows1;i++)
B[i] = (int *)malloc((numCols1)*sizeof(int));
int **C = (int **)malloc((numRows1)*sizeof(int *));
for (int i=0;i<numRows1;i++)
C[i] = (int *)malloc((numCols1)*sizeof(int));
cout<<"Enter the values for matrix A:"<<endl;
for(int i = 0; i < numRows1; i++)
for(int j = 0; j < numCols1; j++)
{
cout<<"A["<<i<<"]["<<j<<"]: ";
cin>>*(*(A+i)+j);
}
cout<<"Enter the values for matrix B:"<<endl;
for(int i = 0; i < numRows1; i++)
for(int j = 0; j < numCols1; j++)
{
cout<<"B["<<i<<"]["<<j<<"]: ";
cin>>*(*(B+i)+j);
}
MatrixSubtract(A, B, C, numRows1, numCols1);
cout<<"The resultant matrix C:"<<endl;
for(int i = 0; i < numRows1; i++)
{
for(int j = 0; j < numCols1; j++)
cout<<*(*(A+i)+j)<<" ";
cout<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.