Question

Given two 2-dimensional arrays, A and B, calculate the three arrays: A + B, A*B and...

Given two 2-dimensional arrays, A and B, calculate the three arrays: A + B, A*B and A*B-1 . That is to say find the sum of the two arrays, the outer-product of the two arrays and the outer-product of A times B inverse.

You can use the procedure illustrated on the web site:

https://www.thecrazyprogrammer.com/2017/02/c-c-program-find-inverse-matrix.html (Links to an external site.)

  • make the arrays 3X3, choose any values you wish
  • make sure the determinant of B is not zero, if so change the values in B.
  • when you run the inverse program from the website listed it will tell you if the determinant is zero.
  • add the output to the end of the .cpp file.
  • add a comment on the calculation of the inverse array.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

#include <iostream>
#include <iomanip>

using namespace std;

//Printing matrix
void printMatrix(double res[3][3])
{
int i, j;

cout << fixed << setprecision(2);

//Iterating over rows
for(i=0; i<3; i++)
{
//Iterating over columns
for(j=0; j<3; j++)
{
cout << left << setw(6) << res[i][j] << " ";
}

cout << "\n";
}
}

//Matrix Addition
void matrixAddition(double mat1[3][3], double mat2[3][3], double res[3][3])
{
int i, j;

//Iterating over rows
for(i=0; i<3; i++)
{
//Iterating over columns
for(j=0; j<3; j++)
{
res[i][j] = mat1[i][j] + mat2[i][j];
}
}
}

//Matrix Multiplication
void matrixMultiplication(double mat1[3][3], double mat2[3][3], double res[3][3])
{
int i, j, k;

//Iterating over rows
for(i=0; i<3; i++)
{
//Iterating over columns
for(j=0; j<3; j++)
{
res[i][j] = 0;

for(k=0; k<3; k++)
{
res[i][j] += mat1[i][k] + mat2[k][j];
}
}
}
}

void inverse(double mat[3][3], double res[3][3])
{
int i, j;
double det=0;

//finding determinant
   for(i = 0; i < 3; i++)
{
det = det + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] - mat[1][(i+2)%3] * mat[2][(i+1)%3]));
}

cout << "\n\n Determinant: " << det << "\n\n";

   for(i = 0; i < 3; i++)
{
       for(j = 0; j < 3; j++)
       {
res[i][j] = ((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) - (mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/(double)det;
       }
}
}

//Main function
int main()
{
//Declaring arrays
double A[3][3] = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};
double B[3][3] = {{3, 0, 2},{2, 0, -2},{0, 1, 1}};
double C[3][3];
double BInv[3][3];

cout << "\n\n Matrix A: \n\n";
printMatrix(A);

cout << "\n\n Matrix B: \n\n";
printMatrix(B);

//Addition
matrixAddition(A, B, C);

cout << "\n\n Matrix C = A+B: \n\n";
printMatrix(C);

//Multiplication
matrixMultiplication(A, B, C);

cout << "\n\n Matrix C = A*B: \n\n";
printMatrix(C);

//Inverse
inverse(B, BInv);

cout << "\n\n Matrix B-1: \n\n";
printMatrix(BInv);

matrixMultiplication(A, BInv, C);

cout << "\n\n Matrix C = A+B-1: \n\n";
printMatrix(C);

cout << "\n\n";
return 0;
}

___________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
Given two 2-dimensional arrays, A and B, calculate the three arrays: A + B, A*B and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays...

    In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional arrays of integer and returns true (boolean value) if the arrays contain...

  • C++Implement only (source code) a program to determine whether two two-dimensional arrays are equivalent or not....

    C++Implement only (source code) a program to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional arrays of integer and returns true (boolean value) if the arrays contain the same values in...

  • Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is...

    Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is a two dimensional array. The function should add the first two arrays a and b (entry by entry), storing the results in the third array c. In the main function, call the add function with appropriate values. The main function should print the sum of the two arrays stored in the third array. Sample run: float al...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • Write a C function named add with the following parameters: three float arrays a, b and...

    Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is a two dimensional array. The function should add the first two arrays a and b (entry by entry), storing the results in the third array c. In the main function, call the add function with appropriate values. The main function should print the sum of the two arrays stored in the third array. Sample run: float al...

  • i need help with this web page project and i need the code too for this web page Include two or three HTML features that...

    i need help with this web page project and i need the code too for this web page Include two or three HTML features that make the website more robust, dynamic, and professional. Which features you opt to add can be discussed in the discussions. Some ideas might be to have all three types of links (internal to the page, links to your other pages, or links to other websites). Complete a Contact Us page which includes a form. Try...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • Write a python program that takes two arrays a and b and prints the dot product....

    Write a python program that takes two arrays a and b and prints the dot product. Example:: Array a with ['apple' , 'banana' , 'carrot', ‘turtle’, ‘zebra’] Array b with ['red', 'blue', 'green'] A * B generates [ ['apple', 'red'] , ['apple' , 'blue'] , ['apple' , 'green'] , ['banana', 'red'], ...] Put dot product in method with 2 arrays as parameters. Make sure works with and empty list for A or B or both. Make sure works for above...

  • Discrete Cosine Transform. a. On the 256x256 grayscale image kobi.png, calculate the two-dimensional Discrete Cosine Transform...

    Discrete Cosine Transform. a. On the 256x256 grayscale image kobi.png, calculate the two-dimensional Discrete Cosine Transform using dct2. Output the results: the input image, and the DCT image. b. Take the DCT image in (a), and apply the two-dimensional inverse DCT image. Subtract that from the original kobi.png image, and plot the image of the absolute value of the differences. Does the entire image have zero values? Why not?

  • JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT