Matrix implementation problem
I am implementing a matrix program which create matrix according to the values on a file and write to or read from files the values of the matrix.
However, since I am not able to compile my program, I would like to ask you that whether my class si logically correct and whether it gives a compile time error or not.
If there is unexpeted situations in my code, would you please help e to fix them ?
class SymmetricMatrix {
public:
int index1;
int index2;
int **matrices;
/*
* Constructor that initializes an empty matrix with a given size.
*/
SymmetricMatrix(const int matrixSize){
size = matrixSize;
matrices = new int*[size];
for(int a = 0; a < size; a++)
matrices
= new int[size]; // assigning arry object to the pointers
}
/*
* Reads the contents of the matrix from the given text file.
* The file has the format:
* a_11 a_12 ... a_1n
* a_21 a_22 ... a_2n
* . . ... .
* . . ... .
* a_n1 a_n2 ... a_nn
* where a_ij is the element on the i’th row and j’th column and n is the
* size of the matrix. Each element is written with a width of 8 characters.
*
* You can assume that the file’s contents follow the specified format.
* In other words, you do not need to have validity checks for the read
* operations.
*/
void read(const string filename){
//Declare variables
string filename = filename;
int value;
//Open a stream for the input file
ifstream inputFile;
inputFile.open( filename.c_str(), ios_base::in );
...
//Read an integer value from the file stream just like reading from cin
while(inputFile.eof() == true){
inputFile >> value;
if(index1 % size == 0) {
index1++;
index2 = 0;
}
index2++;
matrices[index1][index2] = value;
}
...
//Close the input file stream
inputFile.close();
}
/*
* Writes the contents of the matrix to the given text file in the format
* specified by the read function. In other words, the read function should
* be able to read a matrix from a file created by the write function.
*/
void write(const string filename) {
//Open a stream for the output file
ofstream outputFile;
outputFile.open( filename.c_str(), ios_base::out );
...
//Write an integer value to the file stream just like writing to cout
for(int a = 0; a < index1) {
for(int b = 0; b < index2; b++){
value = matrices[index1][index2];
outputFile << value;
}
}
...
//Close the output file stream
outputFile.close();
}
/*
* Displays the contents of the matrix on the screen in the format specified
* by the read function.
*/
void print(){
cout << setprecision(4); // Set precision of numeric output (precision is retained)
cout.setf(ios::right); // set right justification
for (int i=0; i < index1; i++)
{
for (int j=0; j < index2; j++)
cout << setw(10) // set width (reset each call to no fixed width==0)
<< matrices
[j];
cout << '\n';
}
cout << "width is "<<cout.width() << " and precision is "<<cout.precision()<<'\n';
}
/*
* Adds the given input matrices (matrix1 and matrix2) and stores the result
* in the matrix member of the object for which the function is invoked.
*/
void add(const SymmetricMatrix &matrix1, const SymmetricMatrix &matrix2) {
/*SymmetricMatrix *first = matrix1;
SymmetricMatrix *second = matrix2; */
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrix::matrices![]()
= *matrix1::matrices![]()
+ *matrix2::matrices![]()
;
}
}
}
/*
* Adds the given input matrix (matrix1) to the matrix member of the object
* for which the function is invoked, and stores the result in the matrix
* member of the same object (similar to the += operation).
*/
void add(const SymmetricMatrix& matrix1){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrix::matrices![]()
+= *matrix::matrices![]()
;
}
}
}
/*
* Adds the given scalar value to every element of the matrix member of the
* object for which the function is invoked.
*/
void add(const int value){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrix::matrices![]()
+= value;
}
}
}
/*
* Multiplies the given input matrices (matrix1 and matrix2) and stores the
* result in the matrix member of the object for which the function is invoked.
*/
void multiply(const SymmetricMatrix& matrix1, const SymmetricMatrix& matrix2){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrix::matrices![]()
= (*matrix1::matrices![]()
) * (*matrix2::matrices![]()
);
}
}
}
/*
* Multiplies the given input matrix (matrix1) with the matrix member of the
* object for which the function is invoked, and stores the result in the
* matrix member of the same object (similar to the *= operation).
*/
void multiply(const SymmetricMatrix& matrix1){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrix::matrices![]()
*= *matrix1::matrices![]()
;
}
}
}
/*
* Multiplies every element of the matrix member of the object for which
* the function is invoked by the given scalar value.
*/
void multiply(const int value){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrix::matrices![]()
*= value;
}
}
}
/*
* Subtracts the second input matrix (matrix2) from the first input matrix
* (matrix1), and stores the result in the matrix member of the object for
* which the function is invoked.
* (Hint: You can use other functions of the class to implement this one.)
*/
void subtract(const SymmetricMatrix& matrix1, const SymmetricMatrix& matrix2){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrix::matrices![]()
= *matrix1::matrices![]()
- *matrix2::matrices![]()
;
}
}
}
/*
* Subtracts the given input matrix (matrix1) from the matrix member of the
* object for which the function is invoked, and stores the result in the
* matrix member of the same object (similar to the -= operation).
* (Hint: You can use other functions of the class to implement this one.)
*/
void subtract(const SymmetricMatrix& matrix1){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrix::matrices![]()
-= *matrix1::matrices![]()
;
}
}
}
/*
* Subtracts the given scalar value from every element of the matrix member
* of the object for which the function is invoked.
* (Hint: You can use other functions of the class to implement this one.)
*/
void subtract(const int value){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrix::matrices![]()
-= value;
}
}
}
/*
* Resizes the matrix member with the given new size.
5
* (Note: Contents of the initial matrix are lost after this operation.)
*/
void resize(const int newSize){
size = newSize;
}
/*
* Deallocates the memory used by the matrix member.
*/
void free(){
for(int a = 0; a < size; a++){
delete[] matrices
;
}
delete[] matrices;
}
/*
* Returns the size of the matrix member. For example, a 5-by-5 matrix will
* return the value 5.
*/
int getSize() {
return size * size;
}
/*
* Returns the value of the matrix element at the given row and column.
*/
int getValue(const int row, const int column){
return matrices[row][column];
}
/*
* Sets the value of the matrix element at the given row and column to the
* given value (newValue).
*/
void setValue(const int row, const int column, const int newValue){
matrices[row][column] = newValue;
}
private:
/*
* Size of the matrix.
*/
int size;
/*
* Define the matrix data member with the appropriate data type.
* This implementation uses a matrix of integer values.
*/
SymmetricMatrix matrix;
};
Thanks

