Question

In c++ create a static and stack dynamic matrices. Fill the matrices with random number from...

In c++ create a static and stack dynamic matrices. Fill the matrices with random number from 1-100. Optional, in subprogram, perform matrix multiplication on static matrices (Matrix class with overloaded operator for matrix multiplication provided below

class Matrix
{
int a[2][2];
public:
Matrix operator * (Matrix x);
};

Matrix
Matrix::operator * (Matrix x)
{
Matrix t;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
{
   t.a[i][j] = 0;
   for (int k = 0; k < 2; k++)
   t.a[i][j] += a[i][k] * x.a[k][j];
}
return t;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>

#include <cstdlib>

using namespace std;

class Matrix

{

int a[2][2];

public:

Matrix();

Matrix operator*(Matrix x);

void printMatrix();

};

Matrix::Matrix(){

for(int i=0; i<2; i++){

for(int j=0; j<2; j++){

a[i][j] = (rand()%100)+1;

}

}

}

Matrix Matrix::operator*(Matrix x)

{

Matrix t;

for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)

{

t.a[i][j] = 0;

for (int k = 0; k < 2; k++)

t.a[i][j] += a[i][k] * x.a[k][j];

}

return t;

}

void Matrix::printMatrix(){

for(int i=0; i<2; i++){

for(int j=0; j<2; j++){

cout<<a[i][j]<<" ";

}

cout<<endl;

}

}

int main()

{

Matrix mat1, mat2, mat3;

mat3 = mat1*mat2;

cout<<"Matrix 1: "<<endl;

mat1.printMatrix();

cout<<"Matrix 2: "<<endl;

mat2.printMatrix();

cout<<"Matrix 3: "<<endl;

mat3.printMatrix();

return 0;

}

Add a comment
Know the answer?
Add Answer to:
In c++ create a static and stack dynamic matrices. Fill the matrices with random number from...
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
  • Now, your objective is to rewrite the same Stack class with a Generic ArrayList and make...

    Now, your objective is to rewrite the same Stack class with a Generic ArrayList and make the entire class support using Generic types. You should be able to create a Stack of any primitive wrapper class, and show us that your Generic Stack implementation (push, pop, search, display, etc.) works with: • Character (Stack) • Integer (Stack) • Float (Stack • Double (Stack) • String (Stack) You can create these Stack objects in main() and perform operations on them to...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • public class Test { private static int i =0; private static int j =0; public static...

    public class Test { private static int i =0; private static int j =0; public static void main(String[] args) { int i = 2; int k = 3; { int j =3; System.out.println("i + j is : " + i + j); } k = i + j; System.out.println("K is " + k ); System.out.println("K is " + j); } } why is the output i + j = 23 K =2 K =0 Please explain a step by step...

  • Why is my multiplication wrong when i do a matrix of 3 x 5 and 2...

    Why is my multiplication wrong when i do a matrix of 3 x 5 and 2 x 2? code below import java.util.*; public class matrix { public static void main(String[] args) { int m, n, i, j; Random rand = new Random(); Scanner scan = new Scanner(System.in); System.out.print("enter how many rows:"); m = scan.nextInt(); System.out.print("enter how many columns:"); n=scan.nextInt(); int matrix_1[][] = new int[m][n]; //Initialize matrixes int maritx_2[][] = new int[m][n]; int matrix_add[][] = new int[m][n]; int matrix_mul[][] = new...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

  • IN JAVA: List or draw out the memory contents for BOTH of the slides below. (you do not need to s...

    IN JAVA: List or draw out the memory contents for BOTH of the slides below. (you do not need to show addresses)Clearly list out what memory is stored in each region under the bolded (Stack, Heap, or Globals). Assume the garbage collector has not yet run. Multiple Objects Stack //Multiple dynamic objects class objx f int i void print ) f system.out.println ("i-" + i)i Heap / same as before public class DynamicExample3 f public static void main (Stringl] argv)...

  • Please help me out with this assignment. Please read the requirements carefully. And in the main...

    Please help me out with this assignment. Please read the requirements carefully. And in the main function please cout the matrix done by different operations! Thanks a lot! For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...

  • I have a Graph.java which I need to complete four methods in the java file: completeGraph(),...

    I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...

  • c) public class Test { public static void main(String[] args) { T t1 = new T();...

    c) public class Test { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); System.out.println("t1's i = " + t1.i + " and j = " + t1.j); System.out.println("t2's i = " + t2.i + " and j = " + t2.j); } } class T { static int i = 0; int j = 0; T() { i++; j = 1; } } Why is  t1's i = 2 ? It should be...

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
ADVERTISEMENT