Question

Topic Ch. 10 : Write a class definition for a 2x2 matrix, with methods for get(i,j),...

Topic Ch. 10 : Write a class definition for a 2x2 matrix, with methods for get(i,j), set(i,j,v) for getting and setting (i,j)-the entry where i is the row index 0 or 1, and j is the column index 0 or 1, and set() sets the entry in row i and column j to be v, a method for adding a matrix y to given x (e.g. x.add(y) will set x=x+y where the corresponding matrix entries are added), for multiplying( e.g. x.mul(y) will set x=x*y,) and x.det() will return the determinant of x (given that the determinant of x is x[0][0]*x[1][1]-x[1][0]*x[0][1]).  Also add 2 constructors, one that initializes all elements to 0, and another that initializes to a given matrix  double y[2][2] (e.g. mat x(y) will set x=y; Note that x is of class type mat, but y is a 2x2 matrix here.). Also add the methods x.read() and x.print()  for reading (all the 4 elements of the matrix) using cin and for printing (all the 4 elements of the matrix) using cout. Write a main() function that reads two matirces x,y, sets x=x+y, y=y*x, prints x, y, and determinant of x to cout. The data member double mat[2][2] must be a private member.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

class matrices

{

public:

void set(int i,int j,double v){

    x[i][j]=v;

}

double get(int i,int j){

        return x[i][j];

}

double add(double y[2][2]){

    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            x[i][j]+=y[i][j];
        }
    }

}

double multiply(double y[2][2]){

    int sum=0;

    for (int c = 0; c < 2; c++) {
      for (int d = 0; d < 2; d++) {
        for (int k = 0; k < 2; k++) {
          sum = sum + x[c][k]*y[k][d];
        }

        mul[c][d] = sum;
        sum = 0;
      }
    }


}

double det(double x[2][2]){


    double deter = x[0][0]*x[1][1]-x[0][1]*x[1][0];
    return deter;

}

matrices(){

    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            x[i][j]=0;
        }
    }


}

matrices(double y[2][2]){

    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            x[i][j]=y[i][j];
        }
    }


}

private:

    double x[2][2];
    double mul[2][2];
};

int main(){

    matrices mat;

    double y[2][2],x[2][2];

    cout<<"enter the matrix x (2x2) : "<<endl;

    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            cin>>x[i][j];
        }
    }


    cout<<"enter the matrix y (2x2) : "<<endl;

    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            cin>>y[i][j];
        }
    }

    mat.add(y);
    mat.multiply(y);

    cout<<"determinant of matrix X is "<<mat.det(x)<<endl;

    return 0;
}

ㅃ ppp.cpp-Code: Blocks 17.12 File Edit View Search Project Build Debug Fortran wSmith Tools Tools+ Plugins DoxyBlocks Settings Help gobal> main0: int Start here unnaturalCondtions.cpp pairOffoys.app X lamberjack.cpp Ppp.cppX Projects Symb Workspace 105 106 107 108 109 110 ·3.DIC programippp.ee nter the matrix x (2x2) cout<< enter the matrix y ( for (int i-:12:1++) for (int j#0ホ2か cin>yii1051 nter the matrix y (2x2) 112 113 114 115 116 determinant of matrix X is -1 mat.add (y) mat.maltiply (y) rocess returned θ (exe) execution time : 6.952 s ress any key to continue cout<cdeterminant of matri 118 119 120 121 122 123 124 125 retarn : Logs & others Code:Blocks Search results Ccc File Line Nessag. = Build file: no tar Bruild finished: 0 e Activate Windows Go to Settings to activate Windows CIC++ Windows (CR+LF) default Line 117, Col 42, Pos 1542 Insert Read/Write default O Type here to search 10/5/2018

Add a comment
Know the answer?
Add Answer to:
Topic Ch. 10 : Write a class definition for a 2x2 matrix, with methods for get(i,j),...
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
  • ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the...

    ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the attachments below and would greatly appreciate some help. ---------------------------------------            case 6:                printf("You chose determinate!\n\n");                printf("Press 2 for 2x2\n OR \n Press 3 for 3x3 Matrix: ");                scanf("%d", &detChoice);                printf("\n");                if (detChoice == 2) {                    printf("Matrix: ");...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • need help with e f and g please 2x2 + x3 0 (1 pts) write the...

    need help with e f and g please 2x2 + x3 0 (1 pts) write the linear system in the format, A x = b (2 pts) Find the determinant of the matrix A by using an expansion along row 1 (2 pts) Find the determinant of the matrix A by using an expansion along column 2 Compare the result with that of (b). Based on your result of b and/or c is matrix A singular or invertible (2 pts)...

  • Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I...

    Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I have also given the main_problem3.cpp and problem3.h to test. problem3.cpp::: #include "problem3.h" // A function to rotate a matrix mat[][MAX] void rotatematrix(int m, int n, int mat[][MAX]) { // your code here    } Makefile::: all : problem3.o    g++ -o mainp3 # your code here problem3.o : problem3.h problem3.cpp    # your code here clean:    rm -f *.o mainp3 main_problem3.cpp::: #include <iostream>...

  • PLEASE help! There should be three files a (.h file), a (main.cpp file), and a (test.cpp...

    PLEASE help! There should be three files a (.h file), a (main.cpp file), and a (test.cpp file) please do not miss any of the questions!! I'm going to try this myself too thanks for any and all help Write a class called array2x2 which includes the following members: Private members: o A two dimensional array to store the data: double data[2][2]; Public members: o A default constructor which initializes data to having entries all 0. [i.e., data[i][j]=0.0 for all i...

  • Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods...

    Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods described: 1. read: reads in a matrix from the command line 2. display: prints the matrix to the command line 3. getRows: returns the number of rows 4. getCols: returns the number of columns 5. set: sets the double value at a particular column/row position 6. get: returns the value stored at a particular column/row position 7. Plus: returns a new Matrix object that...

  • I need help modifying this program. How would I make sure that the methods is being...

    I need help modifying this program. How would I make sure that the methods is being called and checked in my main method? Here is what the program needs to run as: GDVEGTA GVCEKST The LCS has length 4 The LCS is GVET This is the error that I'm getting: The LCS has length 4 // I got this right The LCS is   //the backtrace is not being called for some reason c++ code: the cpp class: /** * calculate...

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

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