An instance of the Matrix class will contain one matrix, with the size (rows x c
ID: 3666611 • Letter: A
Question
An instance of the Matrix class will contain one matrix, with the size (rows x columns) stored as shown in the example code using private data members. Memory must be allocated dynamically to contain the actual matrix elements. Your code must allocate the proper amount of memory, not an arbitrarily large block.
For reasons of computational efficiency, you are required to implement your 2-dimensional matrix using a 1-dimensional C++ array. This approach is very common in practice, and a simple formula is often used to decide where in the array each matrix element is placed. If i represents the row location and j represents the column location, both starting at 0, then matrix element (i, j) is typically stored as array element data[i*columns + j].
Your constructor should accept two integer arguments and should create a matrix of the size specified by those arguments. The constructor should also initialize the values of the matrix such that matrix element (i, j) contains the value (i – j), using an appropriate typecast. Following this approach, the statement Matrix a(3, 4); would create object a that is initialized as shown below. In the class you define an output function to display the Matrix data, we should see a result similar to the following on the standard output: also correct destructor should be built to return memory back to OS.
a =
0.0 -1.0 -2.0 -3.0
1.0 0.0 -1.0 -2.0
2.0 1.0 0.0 -1.0
//////////////////////////////////////////////////////// //
// File name: matrix.h
// Description: Header file for the ADT Matrix
// Date: 2/1/2013 //
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
class Matrix
{
public:
// Constructor: create a matrix of size (row_size x column_size)
Matrix(int row_size, int column_size);
// Destructor: deallocate memory that was allocated dynamically
~Matrix(); // Set/Get the value of an element at (row, column), where
// ‘row’ ranges from 0 to (row_size-1), and ‘column’ ranges
// from 0 to (column_size-1)
void setElement(int row, int column, MatrixItem value);
MatrixItem getElement(int row, int column) const;
// output the matrix data to console Void outputMatrix();
private:
int rows; // the number of rows in the matrix
int columns; // the number of columns in the matrix
MatrixItem *data; // dynamically allocated array storage
};
// End Matrix class
// End header file
Try to write your code to handle matrices of arbitrary size. However, the largest case that the grader will test is 4 x 4. For example, consider the following statements:
Matrix a(2, 3);
Matrix b(3, 4);
Matrix c(2, 4);
File naming conventions: The name of your header (“specification”) file must be exactly matrix.h, and the name of your source (“implementation”) file must be exactly matrix.cpp. This latter file must contain your implementation of the Matrix ADT. You also need to provide client code to input Matrix size and output the Matrix data as you did before.
What to hand in: After your program is complete, upload your 3 files (matrix.h , matrix.cpp and client.cpp)
Explanation / Answer
int [] original = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; int columns = 3; int rows = original.Length/columns; int [] rowValues = new int [columns]; //to store the item values at the given row index! int index = 2; //the row of which the values are to be accessed. Any valid row index will do! for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.