Question

In: Computer Science

write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size...

write a c++ program.

Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.

       There should be two public methods within the class definition namely:

  • void setMatrixValue(int i, int j); that should set m[i][j] with user defined values
  • int getMatrixValue(int i, int j); that should return m[i][j]

Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication

Solutions

Expert Solution

Screenshot :-

Code:-

#include <iostream>

using namespace std;

class Matrix
{
int m[3][3];
public:
void setMatrixValue(int i, int j)
{
cout<<"\nEnter the value: ";
cin>>m[i][j];   
}
  
int getMatrixValue(int i, int j)
{
return m[i][j];
}
};

void CrossProduct(Matrix m1, Matrix m2)
{
  
int i,j,k;
  
int result[3][3];
  
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
result[i][j]=0;
}
  
}
  
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
for(k = 0; k < 3; k++)
{
result[i][j] += m1.getMatrixValue(i,k) * m2.getMatrixValue(k,j);
}
}
}
  
cout<<"\n";
cout<<"The Matrix multiplication is: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<result[i][j]<<"\t";
cout<<"\n";
  
}
  
}   

int main()
{
int i,j;
Matrix m1,m2;
cout<<"Enter first matrix:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
m1.setMatrixValue(i,j);
  
}
  
cout<<"\nEnter Second matrix:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
m2.setMatrixValue(i,j);
  
}
  
  
CrossProduct(m1,m2);
return 0;
}


Related Solutions

In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int register i, j; for (j = 0; j< Size; j ++) { { for (i = 0; i< Size; i++) C[i; j] = A[i; j] + B[i; j]; } } Assume that the program is running on a system using demand paging and the page size is 1 Kilobyte. Each integer is 4 bytes long. It is clear that each array requires a 16-page...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT