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...

  • 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...

  • 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();   ...

  • Assignment is designed to develop your ability to create static methods and manipulate 1D and 2D...

    Assignment is designed to develop your ability to create static methods and manipulate 1D and 2D arrays in Java. Create a single Java class Matrix and inside the class create the specified static methods as described Task # Description 1 Create a matrix (known components) 2 Create a matrix (random components) 3 Create a matrix from vectors 4 Compare two matrices 5 Add two matrices 6 Subtract two matrices 7 Multiply a matrix by a scalar 8 Multiply two matrices...

  • 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...

  • C# programming using System;                         public class Matrix {      public int[,] elements;   &nb

    C# programming using System;                         public class Matrix {      public int[,] elements;           public Matrix(int[,] init) {           elements = init;      }           public void Display() {           for (int i=0;i<elements.GetLength(0);i++) {               for (int j=0;j<elements.GetLength(1);j++) {                    Console.Write(elements[i,j]+" ");               }               Console.WriteLine();           }      } } public class Program {      public static void Main()      {           Matrix m = new Matrix(new int[,] {{3,4,5},{6,7,8},{9,0,0}});           m.Display();      } } In the method...

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