+1 (315) 557-6473 

Program To Create a Functions on Matrices in C++ Language Assignment Solution.


Instructions

Objective
Write a program to create functions on matrices in C++ language.

Requirements and Specifications

Program to implement functions on matrices in C++

Source Code and Solution

#include <iostream>

#include <vector>

#include <fstream>

using namespace std;

class matrix

{

public:

 vector<vector<int> > M;

 matrix()

 {

  //TO_DO: Initialize a matrix of size 3 * 3

  vector<vector<int>> m;

  vector<int> row1 = { 0,0,0 };

  vector<int> row2 = { 0,0,0 };

  vector<int> row3 = { 0,0,0 };

  m.push_back(row1);

  m.push_back(row2);

  m.push_back(row3);

  M = m;

 }

 matrix(vector<vector<int> >& m)

 {

  M = m;

 }

 // TO_DO: Implement a function to multiply two matrices.

 int determinant()

 {

  // Extract elements (1,1), (1,2) and (1,3)

  int a = M[0][0];

  int b = M[0][1];

  int c = M[0][2];

# // Calculate determinat of submatrix without row 1 and col 1

  int detA = M.at(1).at(1)*M.at(2).at(2) - M.at(2).at(1)*M.at(1).at(2);

  // Calculate determinant of submatrix without row 1 and col 2

  int detB = M.at(1).at(0)*M.at(2).at(2) - M.at(2).at(0)*M.at(1).at(2);

  // Calculate the determinant of the submatrix without row 1 and col 3

  int detC = M.at(1).at(0)*M.at(2).at(1) - M.at(2).at(0)*M.at(1).at(1);

  // Finally, return the determinant of the matrix

  return a * detA - b * detB + c * detC;

 }

 int cofactor(int row, int col)

 {

  // This function calculates the minor determinan (of the 2x3 submatrix, excluding row and col)

  int m[2][2];

  int idx_row = 0;

  int idx_col;

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

  {

   if (i != row)

   {

    idx_col = 0;

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

    {

     if (j != col)

     {

      m[idx_row][idx_col] = M[i][j];

      idx_col += 1;

     }

    }

    idx_row += 1;

   }

  }

  int cof = m[0][0] * m[1][1] - m[1][0] * m[0][1];

  //cout << "Cofactor (" << row << "," << col << ") = " << cof << endl;

  return cof;

 }

 void transpose()

 {

  matrix m;

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

  {

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

   {

    int temp = M[j][i];

    m.M[j][i] = M[i][j];

    m.M[i][j] = temp;

   }

  }

  M = m.M;

 }

 // TO_DO: Implement a function to calculate the classic adjoint matrix.

 matrix adjoint()

 {

  // Calculate the cofactor matrix

  matrix cofM;

  int k = 1;

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

  {

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

   {

    cofM.M[i][j] = k*cofactor(i, j);

    if (k == 1)

     k = -1;

    else

     k = 1;

   }

  }

  // Now, the adjoint is the transpose of the cofactor matrix

  matrix adj(cofM);

  adj.transpose();

  return adj;

 }

 // TO_DO: Implement a function to calculate the determinant.

 matrix multiply(matrix A)

 {

  // Initialize 3x3 matrix of zeros that will contain the result

  matrix MA;

  // Multiply

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

  {

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

   {

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

    {

     MA.M[i][j] += M[i][k] * A.M[k][j];

    }

   }

  }

  return MA;

 }

 void print()

 {

  // prints matrix

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

  {

   cout << "[ ";

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

   {

    cout << M[i][j];

    if (j < 2)

     cout << ", ";

   }

   cout << " ]" << endl;

  }

 }

};

int main()

{

 fstream fin;

 fin.open("data.txt", ios::in);

 if (!fin) {

  cout << "not open" << endl;

  exit(0);

 }

 int n;

 fin >> n;

 int determinant;

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

  ////////////////////////

  // TO_DO: read the input and call the functions you've implemented to generate the output

  ////////////////////////

  // Initialize empty matrix

  int val;

  vector<vector<int>> mat;

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

  {

   vector<int> row;

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

   {

    fin >> val;

    row.push_back(val);

   }

   mat.push_back(row);

  }

  matrix M(mat);

  determinant = M.determinant();

  cout << "The determinant of the matrix is: " << determinant << endl;

 }

 return 0;

}