Question

Write a C++ program to define a class having a 4x4 matrix. Generate 32 random numbers...

Write a C++ program to define a class having a 4x4 matrix. Generate 32 random numbers 1-10, in an output.txt file. Fill the generated numbers in two such class objects (first 16 numbers in one matrix and next 16 numbers in another. Create member functions each for adding, subtracting and multiplication of matrices. Write the algorithm defining steps of your program.

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

ANSWER:

GIVEN THAT:

Program:

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include <fstream>

#include <ctime>

using namespace std;

class Matrix

{

public:

int mat[4][4];

Matrix() {}

Matrix *addition(Matrix *m2)

{

Matrix *res = new Matrix();

int i, j;

for (i = 0; i < 4; i++)

{

for (j = 0; j < 4; j++)

{

res->mat[i][j] = mat[i][j] + m2->mat[i][j];

}

}

return res;

}

Matrix *subtract(Matrix *m2)

{

Matrix *res = new Matrix();

int i, j;

for (i = 0; i < 4; i++)

{

for (j = 0; j < 4; j++)

{

res->mat[i][j] = mat[i][j] - m2->mat[i][j];

}

}

return res;

}

Matrix *muplication(Matrix *m2)

{

Matrix *res = new Matrix();

int i, j, k;

for (i = 0; i < 4; i++)

{

for (j = 0; j < 4; j++)

{

int sum = 0;

for (k = 0; k < 4; k++)

{

sum += mat[i][k] + m2->mat[k][j];

}

res->mat[i][j] = sum;

}

}

return res;

}

void print()

{

int i, j;

for (i = 0; i < 4; i++)

{

for (j = 0; j < 4; j++)

{

cout << setw(4) << mat[i][j];

}

cout << endl;

}

}

};

int main()

{

srand(time(NULL));

ofstream out("output.txt");

int i = 0, j;

while (i < 32)

{

out << (rand() % 10) + 1 << endl;

i++;

}

out.close();

ifstream in("output.txt");

if (in.is_open())

{

Matrix *mat1 = new Matrix();

Matrix *mat2 = new Matrix();

for (i = 0; i < 4; i++)

{

for (j = 0; j < 4; j++)

{

in >> mat1->mat[i][j];

}

}

for (i = 0; i < 4; i++)

{

for (j = 0; j < 4; j++)

{

in >> mat2->mat[i][j];

}

}

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

mat1->print();

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

mat2->print();

cout << "Addition of Matrix-1 and Matrix-2: " << endl;

Matrix *addition = mat1->addition(mat2);

addition->print();

cout << "Substraction of Matrix-1 and Matrix-2: " << endl;

Matrix *subtract = mat1->subtract(mat2);

subtract->print();

cout << "Multiplication of Matrix-1 and Matrix-2: " << endl;

Matrix *multiplication = mat1->muplication(mat2);

multiplication->print();

}

else

{

cout << "Error opening file" << endl;

}

}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program to define a class having a 4x4 matrix. Generate 32 random numbers...
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
  • Write a C++ program and algorithm to create a class of fraction having variables numerator and...

    Write a C++ program and algorithm to create a class of fraction having variables numerator and denominator, and member functions setting and getting values of the fractions. Also create a static member variable of the class having fraction value tolerance of 0.1. Instantiate 10 fraction objects and find out how many of them are below and above tolerance level. Decrement the tolerance to 0.01 by a static member function and find out how many of them are now above and...

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

  • Write a program in C to generate random numbers. The program recieves 4 items on activation...

    Write a program in C to generate random numbers. The program recieves 4 items on activation through argv: 1. Name of the data file 2. Number of items for program to generate 3. Range of numbers 4. Seed of the random number generator Example run: ./rand datafile.txt 20 1000 3127 Program must convert all numbers to ints using atoi(), must save all parameters into variables, opens the data file to write the random numbers into it, program loops to generate...

  • Write a MIPS program to that will take two 4x4 matrices, and calculate their sum and...

    Write a MIPS program to that will take two 4x4 matrices, and calculate their sum and product, using row major, and column major math (so this is actually 4 problems, but obviously they’re all pretty related). I generated two sample arrays to test 2   1       9       2 7   9       10      10 3   4       4       4 2   5       4       4 8 7 1 2 2 7 8 6 7 5 6 8 9 4 8 9 The output of your program...

  • C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

  • Write a C++ program that does the following : Define a class myInt that has as...

    Write a C++ program that does the following : Define a class myInt that has as its single attribute an integer variable and that contains member functions for determining the following information for an object of type myInt: A. Is it multiple of 7 , 11 , or 13. B. Is the sum of its digits odd or even. C. What is the square root value. D.Is it a prime number. E. Is it a perfect number ( The sum...

  • Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you...

    Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal)....

  • C++ program Create a class Time having data members HH, MM, SS of type integer. Write...

    C++ program Create a class Time having data members HH, MM, SS of type integer. Write a C++ program to do the following: 1. Use a Constructor to initialize HH, MM, SS to 0. 2. Create two Objects of this class and read the values of HH, MM, SS from the user for both objects (Using proper member functions). 3. Use a binary + operator to add these two objects created (Store & display result using separate object). 4. Use...

  • C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...

    C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...

  • 06) Write a C program to perform matrix operations based of threads. The program accepts from...

    06) Write a C program to perform matrix operations based of threads. The program accepts from the user a positive integer N. A menu is given to the user: 1. generate matrices: generates three NxN matrices A, B, C with random integer numbers between 0 and 9 2. add: matrix D- A+B+C 3. subtract: matrix D A-B-C 4. display matrices: A, B, C, D 5. display count of result values more than 8. 6. exit: terminate the program When 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
ADVERTISEMENT