You will create a program that is composed of five files. The program will have
ID: 3661500 • Letter: Y
Question
You will create a program that is composed of five files. The program will have two functions,
each with it’s own header file and source code (think .cpp and .hpp) and a main file to run
everything.
The first function is readMatrix(). It will have a pointer to a 2D array and an integer as
parameters. The integer is the size of the square. It will prompt the user to fill a square matrix.
For this lab the matrix will be 2x2 or 3x3.
The second function is determinant(). It will have 2 parameters, a 2D array and an integer, which
is the size of the square. It will return an integer value with is the determinant of the matrix. If
you don’t remember or know what a determinant is remember mathsifun.com!
In the main file, you will create a program that uses readMatrix() to prompt the user to enter 4 or 9
values to fill the array, calculate the determinant using determinant(), and display both the array
and the determinant to the user. The array should not be displayed in a single line.
Once your program is working and you have eliminated the bugs, create a makefile to build your
program. You do not need a complicated makefile. You can have a single target that just includes
what you typed into the command line for testing.
Include a second target clean that removes extraneous files such as .o files or a.out.
Explanation / Answer
Matrix.cpp(Main file)
// Matrix.cpp : Defines the entry point for the console application
#include<iostream>
#include "Read.h"
#include "MatrixOperation.h"
using namespace std;
int main()
{
int size = 0;
cout << "Enter the size of matrix either 2 or 3" << endl;
cin >> size;
//Dynamic allocation of 2-D array to accomodate variable sizes
//2-D array of integers is an array of int pointers with each one pointing to array of integers.
int** matrix = new int*[size];
for (int i = 0; i < size; ++i)
{
matrix[i] = new int[size];
}
Read r;
//Calling the readMatrix to take input from user for populating the matrix
r.readMatrix(matrix, size);
MatrixOperation m;
int m_det = m.determinant(matrix, size);
cout << "Matrix inputted is" << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << "Determinant of the matrix is " << m_det << endl;
return 0;
}
Read.h(For taking matrix input from user)
#pragma once
#include <iostream>
class Read
{
public:
Read();
~Read();
void readMatrix(int **array,int);
};
Read.cpp
#include "stdafx.h"
#include "Read.h"
using namespace std;
Read::Read()
{
}
Read::~Read()
{
}
void Read::readMatrix(int **matrix,int size)
{
cout << "Enter " << size*size << " elements in the matrix" << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cin >> matrix[i][j];
cout << endl;
}
}
}
MatrixOperation.h(For calculating the determinant)
#pragma once
class MatrixOperation
{
public:
MatrixOperation();
~MatrixOperation();
int determinant(int **matrix, int size);
};
MatrixOperation.cpp
#include "stdafx.h"
#include "MatrixOperation.h"
MatrixOperation::MatrixOperation()
{
}
MatrixOperation::~MatrixOperation()
{
}
int MatrixOperation::determinant(int **matrix, int size)
{
if (size == 2)
{
int det = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
return det;
}
else if (size == 3)
{
int a = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]);
int b = matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]);
int c = matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
int det = a - b + c;
return det;
}
}
Makefile
CXX = g++
VARNAME = value
CXXFLAGS = -Wall -g
main: Matrix.o Read.o MatrixOperation.o
$(CXX) $(CXXFLAGS) -o main Matrix.o Read.o MatrixOperation.o
Matrix.o:Matrix.cpp Read.h MatrixOperation.h
$(CXX) $(CXXFLAGS) -c main.cpp
Read.o:Read.h
MatrixOperation.o:MatrixOperation.h
clean:
m *.o
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.