Question

In: Computer Science

I need implement the operation bellow. Please add comment on some lines to compare with my...

I need implement the operation bellow. Please add comment on some lines to compare with my code.

The program will implement the following operations on the Matrix class:
1. Matrix addition
2. Matrix subtraction
3. Matrix multiplication
4. Matrix scalar multiplication
5. Matrix transpose

Vector class will implement the following operations:
1. Vector length
2. Vector inner (dot product)
3. Vector angle

Vector 3D Class
The class Vector3D is derived from the Vector class, and it implements a vector in a three
dimensional space. In this case, any instance of the Vector3D will have three dimensions, and
hence is a 3 x 1 matrix.

In the Vector3D class, you will implement the cross product operator, and you will have two
versions of it. The first version will be a member function that computes the cross product
between two vectors v1 and v2, and assigns the result to v1. The second version will be a nonmember
function that computes the cross product between v1 and v2, and then assigns the result
to a new vector.

Distribution Files
Your implementation will consist of adding C++ code to implement three modules: Matrix.cpp,
Vector.cpp, and Vector3D.cpp. You will receive all the .h files with declaration of the Matrix,
Vector and Vector3D classes. In addition, you will be provided with a main program that uses
the Matrix, Vector, and Vector3D classes, and interacts with the user to ask his/her input on the
operations to be performed. Finally, you will be given a Makefile with all the commands needed
to compile and submit your project.
1. Object.h – declaration of the Object type that will be stored in the matrix or vector.
2. Matrix.h – declaration of the Matrix class.
3. Matrix.cpp – implementation of the Matrix class. YOU MUST IMPLEMENT THE
METHODS TO APPEAR IN THIS FILE.
4. Vector.h – declaration of the Vector class.
5. Vector.cpp – implementation of the Vector class. YOU MUST IMPLEMENT THE
METHODS TO APPEAR IN THIS FILE.
6. Vector3D.h – declaration of the Vector3D class.
7. Vector3D.cpp – implementation of the Vector3D class. YOU MUST IMPLEMENT
THE METHODS TO APPEAR IN THIS FILE.
8. matrix_main.cpp – main program to the Matrix, Vector and Vector3D classes. DO
NOT MODIFY THIS FILE!!!!
9. Makefile – file with the commands to compile and submit you project.
10. test1.in – test input file 1.
NOTE: YOU PROGRAM MUST PASS THIS FILE WITHOUT ERRORS IN
ORDER TO BE CONSIDERED A RUNNING PROGRAM.
11. test1.out – expected output from test input file 1.
12. test2.in – test input file 2.
13. test2.out – expected output from test input file 2.
14. test3.in – test input file 3.
15. test3.out – expected output file from test input file 3.
16. prof_matrix_main – professor’s version of the matrix_main program. NOTE: Known
to be working correctly.

Matrix.cpp

/

#include "Matrix.h"

namespace Mat_Vectors {

/////////////////// DO NOT MODIFY THESE TWO FUNCTIONS/////////////
// Output operator (Friend function)
ostream& operator<<(ostream &out, const Matrix& M) {
int i = 0, j = 0, len1 = 0, len2 = 0;
  
len1 = M.get_rows();
len2 = M.get_cols();
  
for (i = 0; i < len1; ++i){
for (j = 0; j < len2; ++j){
   out << M.get_value(i,j);
   if (j != len2 - 1){
   out << " ";
   }
}
out << "\n";
}
return out;
}

// Input operator (Friend function)
istream& operator>>(istream &in, Matrix& M){
int i = 0, j = 0, rows = 0, cols = 0;
int new_val;
  
in >> rows;
in >> cols;
  
M = Matrix(rows, cols);
  
for (i = 0; i < rows; ++i){
for (j = 0; j < cols; ++j){
   in >> new_val;
   M.set_value(i,j,new_val);
}
}
return in;
}
///////////////////////////////////////////////////////////////

// ADD YOUR CODE HERE

}

--------------------------------------------------------------------------------------------------------------

Matrix.h

#ifndef _MATRIX_H
#define _MATRIX_H

#include <iostream>
#include <cassert>
#include <cstdio>
#include "Object.h"

using namespace std;
using namespace Mat_Vectors;

namespace Mat_Vectors{
class Matrix {
public:
// Various constants related to matrix size
static const int MAX_ROWS = 100;
static const int MAX_COLUMNS = 100;
  
// Define size type for matrix
typedef std::size_t size_type;
// Define value type for matrix
typedef Object value_type;
  
// Public methods for the matrix class
  
// Constructor
Matrix (size_type rows = 1, size_type cols = 2);
  
// Accessor methods
  
// Returns the element [i,j]
value_type get_value(size_type i, size_type j) const;
// Set the element [i,j]
void set_value(size_type i, size_type j, value_type val);
// Return current number of rows in the matrix
size_type get_rows() const;
// Set current number of rows
void set_rows(size_type new_r);
// Return the current number of columns
size_type get_cols() const;
// Set current number of columns
void set_cols(size_type new_c);

// Matrix operations
  
// Matrix equality
bool operator==(const Matrix& M) const;
  
// Matrix self-addition
const Matrix& operator+=(const Matrix& M);
// Matrix self-substraction
const Matrix& operator-=(const Matrix& M);
// Matrix self-multiplication
const Matrix& operator*=(const Matrix& M);
// Scalar self-multiplication
const Matrix& operator*=(int r);
  
// Matrix transpose
Matrix transpose() const;

// Input operator (Friend function)
friend istream& operator>>(istream &in, Matrix& M);
// Output operator (Friend function)
friend ostream& operator<<(ostream &out, const Matrix& M);
  
private:
// Elements in the matrix
value_type elements[MAX_ROWS][MAX_COLUMNS];
// Current number of rows in the matrix
size_type curr_rows;
// Current number of columns in the matrix
size_type curr_cols;
};
  
// Non-member matrix algebra operations
  
// Matrix addition
Matrix operator+(const Matrix& M1, const Matrix& M2);
// Matrix substraction
Matrix operator-(const Matrix& M1, const Matrix& M2);
// Matrix multiplication
Matrix operator*(const Matrix& M1, const Matrix& M2);
}

#endif

----------------------------------------------------------------------------------------------------------------

MatrixMain.cpp

/

#include <iostream>
#include <cstdlib>
#include "Matrix.h"
#include "Vector.h"
#include "Vector3D.h"

using namespace std;
using namespace Mat_Vectors;

void print_menu();
void matrix_addition();
void matrix_substraction();
void matrix_multiplication();
void matrix_scalar_mult();
void matrix_transpose();
void vector_length();
void vector_inner_prod();
void vector_cross_prod();
void vector_angle();

int main(){
int opt = 0;
cout << "Welcome to the Matrix Test Program" << endl;
do{
print_menu();
cout << "Enter option: ";
cin >> opt;
cout << "Option entered: " << opt << endl;
switch(opt){
case 1:
matrix_addition();
break;
case 2:
matrix_substraction();
break;
case 3:
matrix_multiplication();
break;
case 4:
matrix_scalar_mult();
break;
case 5:
matrix_transpose();
break;
case 6:
vector_length();
break;
case 7:
vector_inner_prod();
break;
case 8:
vector_cross_prod();
break;
case 9:
vector_angle();
break;
case 10:
opt = -1;
break;
default:
cout << "Illegal option: " << opt << endl;
}
}
while(opt != -1);
  
cout << "Thanks for using the Matrix Test Program." << endl;
exit(0);
  
}

void print_menu(){
cout << "***************************" << endl;
cout << "Available Operations:"<< endl;
cout << "\t 1. Matrix Addition" << endl;
cout << "\t 2. Matrix Substraction" << endl;
cout << "\t 3. Matrix Multiplication" << endl;
cout << "\t 4. Matrix Scalar Multiplication" << endl;
cout << "\t 5. Matrix Transpose" << endl;
cout << "\t 6. Vector lenght" << endl;
cout << "\t 7. Vector Inner (dot) Product" << endl;
cout << "\t 8. Vector Cross Product" << endl;
cout << "\t 9. Vector angle " << endl;
cout << "\t 10. Exit" << endl;
}

void matrix_addition(){
Matrix M1, M2, M3;
cout << "Enter the first matrix: " << endl;
cin >> M1;
cout << "Enter the second matrix: " << endl;
cin >> M2;
  
cout << "Result: " << endl;
cout << M1;
cout << "\t + " << endl;
cout << M2;
cout << "\t = " << endl;
M3 = M1 + M2;
cout << M3;
}


void matrix_substraction(){
Matrix M1, M2, M3;
cout << "Enter the first matrix: " << endl;
cin >> M1;
cout << "Enter the second matrix: " << endl;
cin >> M2;
  
cout << "Result: " << endl;
cout << M1;
cout << "\t - " << endl;
cout << M2;
cout << "\t = " << endl;
M3 = M1 - M2;
cout << M3;
}

void matrix_multiplication(){
Matrix M1, M2, M3;
cout << "Enter the first matrix: " << endl;
cin >> M1;
cout << "Enter the second matrix: " << endl;
cin >> M2;
  
cout << "Result: " << endl;
cout << M1;
cout << "\t * " << endl;
cout << M2;
cout << "\t = " << endl;
M3 = M1 * M2;
cout << M3;
}

void matrix_scalar_mult(){
Matrix M1;
int scalar = 0;
cout << "Enter the matrix: " << endl;
cin >> M1;
cout << "Enter the scalar value: " << endl;
cin >> scalar;
  
cout << "Result: " << endl;
cout << M1;
cout << "\t * " << endl;
cout << scalar << endl;
cout << "\t = " << endl;
M1*=scalar;
cout << M1;
}

void matrix_transpose(){
Matrix M1, M2;
  
cout << "Enter the matrix: " << endl;
cin >> M1;
cout << "The tranpose of matrix: " << endl;
cout << M1;
cout << "is the matrix: " << endl;
M2 = M1.transpose();
cout << M2;
}

void vector_length(){
Vector V1;
  
cout << "Enter the vector: " << endl;
cin >> V1;
cout << "The length of the vector: " << endl;
cout << V1;
cout << "is: " << V1.length() << endl;
}

void vector_inner_prod(){
Vector V1, V2;
double result = 0.0;
  
cout << "Enter the first vector: " << endl;
cin >> V1;
cout << "Enter the second vector: " << endl;
cin >> V2;
cout << "Result: " << endl;
cout << V1;
cout << " | " << endl;
cout << V2;
cout << " = ";
result = V1 | V2;
cout << result << endl;
}

void vector_cross_prod(){
Vector3D V1, V2, V3;
  
cout << "Enter the first vector: " << endl;
cin >> V1;
cout << "Enter the second vector: " << endl;
cin >> V2;
cout << "Result: " << endl;
cout << V1;
cout << " & " << endl;
cout << V2;
cout << " = " << endl;
V3 = V1 & V2;
cout << V3;
}

void vector_angle(){
Vector V1, V2;
double result = 0.0;

cout << "Enter the first vector: " << endl;
cin >> V1;
cout << "Enter the second vector: " << endl;
cin >> V2;
cout << "The angle between: " << endl;
cout << V1;
cout << " and " << endl;
cout << V2;
cout << " is " << endl;
result = V1.angle(V2);
cout << result << endl;
}

------------------------------------------------------------------------------------------------------------------------

Object.h

#ifndef _OBJECT_H
#define _OBJECT_H

namespace Mat_Vectors{
// Int elements will be stored in the matrix
typedef int Object;
}

#endif

---------------------------------------------------------------------------------------------------------

Vector.cpp

#include "Vector.h"

namespace Mat_Vectors{
// ADD YOUR CODE HERE
}

------------------------------------------------------------------------------------------------------

Vector.h


#ifndef _VECTOR_ND_H
#define _VECTOR_ND_H

#include <cmath>
#include "Matrix.h"

namespace Mat_Vectors{
  
class Vector : public Matrix {
public:
// Constructor
Vector(int dimensions = 3);
  
// Return number of dimensions
size_type get_dimensions() const {return this->get_rows();}
// Returns a given coordinate
value_type get_coord(int coord) const {return this->get_value(coord,0);}
// Sets a given coordinate
void set_coord(int coord, value_type new_val)
{this->set_value(coord, 0, new_val);}
// Vector Magnitude
double length() const;
  
// Vector Inner Product
double operator|(const Vector& V) const;

// Angle between vectors
double angle(const Vector& V) const;
};

  
}
#endif

----------------------------------------------------------------------------------------------------------

Vector3D.cpp

#include "Vector3D.h"

namespace Mat_Vectors {

// ADD YOUR CODE HERE

}

-------------------------------------------------------------------------------------------------------

Vector3D.h

#ifndef _VECTOR_3D_H
#define _VECTOR_3D_H

#include "Vector.h"

namespace Mat_Vectors{
  
class Vector3D : public Vector {
public:
// Constructor
Vector3D();
  
// Vector Self-cross product
const Vector3D& operator&=(const Vector3D& V);

};

// Non-member function
Vector3D operator&(const Vector3D& V1, const Vector3D& V2);
}
#endif

  

Solutions

Expert Solution

#include <stdio.h>

#define N 4

  

// This function stores transpose of A[][] in B[][]

void transpose(int A[][N], int B[][N])

{

    int i, j;

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

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

            B[i][j] = A[j][i];

}

  

int main()

{

    int A[N][N] = { {1, 1, 1, 1},

                    {2, 2, 2, 2},

                    {3, 3, 3, 3},

                    {4, 4, 4, 4}};

  

    int B[N][N], i, j;

  

    transpose(A, B);

  

    printf("Result matrix is \n");

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

    {

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

           printf("%d ", B[i][j]);

        printf("\n");

    }

  

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

  

// maximum size of matrix

#define MAX 4

  

// maximum number of threads

#define MAX_THREAD 4

  

int matA[MAX][MAX];

int matB[MAX][MAX];

int matC[MAX][MAX];

int step_i = 0;

  

void* multi(void* arg)

{

    int core = step_i++;

  

    // Each thread computes 1/4th of matrix multiplication

    for (int i = core * MAX / 4; i < (core + 1) * MAX / 4; i++)

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

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

                matC[i][j] += matA[i][k] * matB[k][j];

}

  

// Driver Code

int main()

{

    // Generating random values in matA and matB

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

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

            matA[i][j] = rand() % 10;

            matB[i][j] = rand() % 10;

        }

    }

  

    // Displaying matA

    cout << endl

         << "Matrix A" << endl;

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

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

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

        cout << endl;

    }

  

    // Displaying matB

    cout << endl

         << "Matrix B" << endl;

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

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

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

        cout << endl;

    }

  

    // declaring four threads

    pthread_t threads[MAX_THREAD];

  

    // Creating four threads, each evaluating its own part

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

        int* p;

        pthread_create(&threads[i], NULL, multi, (void*)(p));

    }

  

    // joining and waiting for all threads to complete

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

        pthread_join(threads[i], NULL);    

  

    // Displaying the result matrix

    cout << endl

         << "Multiplication of A and B" << endl;

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

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

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

        cout << endl;

    }

    return 0;

}


Related Solutions

Please comment on my answer, what do you think? I need insight, thank you! Q: Answer...
Please comment on my answer, what do you think? I need insight, thank you! Q: Answer both questions below about the EOQ model. (a) In the EOQ model, unit product cost or selling price, C, is not included in the formula we use to solve for the economic order quantity. Explain why it is not necessary to include this information in the EOQ formula. (b) What are the major limitations of the EOQ model? A: A). In the EOQ model,...
i need a conclusion for my paper on ADHD. I need this asap please have to...
i need a conclusion for my paper on ADHD. I need this asap please have to turn my paper in one hour!!!! thank you            In the recent times, ADHD is being seen in the light of cerebral dysfunction with too much focus on clinical treatment using medication at the same time keeping psychotherapy at bay which shows a dangerous trend. Psychotherapy even though cannot explain the cause of the condition, but it can help the children in coping his...
I need a positive replay for this comment without plagirism please: "I think EHRs are a...
I need a positive replay for this comment without plagirism please: "I think EHRs are a positive for the medical profession because they provide an electronic database for the institution. All of the information is in one place and the information can be shared easily between doctors, nurses, and staff. This is beneficial because the doctors or nurses do not have to search for the paper files and then physically share them with each other. Instead, each doctor or nurse...
My bacteria is staphylococcus epidermidis and these are the tests I need some help with:
# Microbiology  My bacteria is staphylococcus epidermidis and these are the tests I need some help with: PR-mannitol PR-raffinose I need to describe what results I would expect to get if I was completing this test in the lab.
I need direct answer just to compare with my solution Thanks ================================ A partial listing of...
I need direct answer just to compare with my solution Thanks ================================ A partial listing of costs incurred during March at Febbo Corporation appears below: Factory supplies $ 9,000 Administrative wages and salaries $ 85,000 Direct materials $ 126,000 Sales staff salaries $ 30,000 Factory depreciation $ 33,000 Corporate headquarters building rent $ 43,000 Indirect labor $ 26,000 Marketing $ 65,000 Direct labor $ 99,000 The total of the period costs listed above for March is: ===================================== A partial listing...
I am taking history of psychology the bellow is my question Identify something new that you...
I am taking history of psychology the bellow is my question Identify something new that you learned about Darwin in this unit. For example, beyond his famous theory of evolution, did you know his thoughts on geography or slavery, or were you aware of his five-year voyage? Additionally, use this forum to brainstorm with your professor and classmates on contributions of early theorists, such as how Weber’s two-point threshold speaks to modern neurology.
Sorry, I made a big mistake in my question bellow posted yesturday. The while statement is...
Sorry, I made a big mistake in my question bellow posted yesturday. The while statement is x<10000 not x < 2. Also for thread A: the printf is: printf("I am Thread 1: var_A = %d, var_B = %d \n", var_A, var_B) ; Please help me and consider the new question below: . Compile and run the deadlockFree.c program below and report if this program does go into deadlock or not. Modify this deadlockFree.c program so that you change the lock/unlock...
Please Typing this My Summary from reading Chapter I need to fix My Words and make...
Please Typing this My Summary from reading Chapter I need to fix My Words and make Clear for understanding Thanks In section 4, it discusses how to adequately deal with your time. Time the board is a significant resource that you have to cause you to have as an understudy. This Part expresses half of understudies experience difficulty dealing with their time in their first year of school. School is a major distinction from secondary school, and a great deal...
I need a reply for this comment: "In a perfect world if I was working for...
I need a reply for this comment: "In a perfect world if I was working for a local hospital or doctors office, I believe that the hospital/office would need to make and keep their employees happy. This can be with incentives and good benefits. However, a main reason that I would keep a position is the leadership of co-workers, management, and the doctors. This would make work pleasant and would flow into employees working better with customers and patients. The...
What are some ways that I can cut costs and stay within my budget? I need...
What are some ways that I can cut costs and stay within my budget? I need a 175 word count please. Thank you very much for your time and effort.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT