Hi everyone,
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
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
}
/*
* 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
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
}
}
}
/*
* 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
}
}
}
/*
* 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
}
}
}
/*
* 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
}
}
}
/*
* 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
}
}
}
/*
* 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
}
}
}
/*
* 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
}
}
}
/*
* 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
}
}
}
/*
* 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
}
}
}
/*
* 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

Matrix implementation problem
Ron J51027
Yep, this is exactly correct.
sl0140
Again and aain for clarifications.
Best wishes and Regards.
Kay-Davis
In this assignment, you are supposed to implement a class for integer symmetric matrices that keeps
ONLY the lower triangle of a matrix (i.e., that keeps a matrix in the format given in Equation 3) and that
defines functions on such matrices. On the other hand, your assignment should hide this implementation
such that the user could not realize that you keep only the lower part of the matrix. For that, the format
of a file from which you read your matrix will obey the format given in Equation 2 (the details of the file
format are given in the class definition). Similarly, you should write matrices to a file or display them on
the screen in the format given in Equation 2.
The operations (add, multiply, and subtract) that you will implement in this assignment also yield
symmetric matrices. Thus, it is legitimate to use symmetric matrices for storing the results of these operations.
When you are debugging your program, we recommend you to check whether or not the result is a
symmetric matrix at the end of each operation.
In your implementation, you must also be careful about validating the input in any operation. For
example, addition operation is only defined for matrices with the same size. As another example, if a matrix
is deallocated using the free function, it is not possible to perform an add operation because there is no
space to store the result of addition. The only exception to this is the read function where you can assume
that the input file exists and it contains a matrix with the correct size (i.e., same size as the matrix in the
object for which the function is invoked) and correct format.
Jarret
In your constructor, you allocate memory to store the entire matrix, im not sure this is what the assignment wishes you to do.
Dean Stewart
So given the matrix:
1 0 2
0 1 0
2 0 1
Your assignment is to only store the lower portion ie:
1
0 1
2 0 1
Is this correct
ReiXou
I also would like everyone to examine the codes that I send since there is again errors occured in sipre of changing the part that dear iccle refered.
So would you please help me
Thanks,
Regards and Best wishes
RafaBotero
Row major and Column major refer to the method you use to access each element within your matrix, row major you access by row first, then column, whilst column major you access by column then row.
Currently your data is stored internally in row major order, so when you access the data, you must access it in row major order to get correct results in and out.
Say we have a 3x3 matrix stored as a 9 element array (int array[9] for simplicity):
Row Major elements are numbered:
0 1 2
3 4 5
6 7 8
Column Major elements are numbered
0 3 6
1 4 7
2 5 8
Say we use your code as provided and we call your setValue method like so:
setValue(0,1,10);
The result of this function will set 2 different elements in your matrix! ie it will set both
row 0, column 1 and column 1, row 0 which is an error.
A better plan would be to provide 2 different "set" methods, one for row major, and the other for column major, but bear in mind that it is important that both methods refer to the same element! ie:
void setValueRowMajor(int row, int col, int value)
{
matrix[row][col] = value
}
void setValueColMajor(int col, int row, int value)
{
matrix[row][col] = value
}
If your instructor wants you to store your data internally in column major and row major, you will need to either write 2 different classes, or be very carefull about how you access your matrix elements.
Frank Uray
But the error continue so how can I fix the errors that I sent
Again thanks for your all clarifications.
Regards and best wishes
AndyL
Thanks for your reply.
This my headr file for matrix.
class SymmetricMatrix{
// Data Members
public:
int **matrices;
// Operations - Member Functions
SymmetricMatrix(const int);
void read(const string );
void write(const string );
void print();
void add(const SymmetricMatrix& , const SymmetricMatrix& );
void add(const SymmetricMatrix& matrix1);
void add(const int );
void multiply(const SymmetricMatrix& , const SymmetricMatrix& );
void multiply(const SymmetricMatrix& );
void multiply(const int );
void subtract(const SymmetricMatrix& , const SymmetricMatrix& );
void subtract(const SymmetricMatrix& );
void subtract(const int );
void resize(const int );
void free();
int getSize();
int getValue(const int , const int );
void setValue(const int , const int , const int );
// Data Members
private:
int size;
};
This is my .cpp file for my matrix
#include <ifstream>
#include <iostream>
#include "SymmetricMatrix.h"
#include <iomanip>
using namespace std;
/*
* Constructor that initializes an empty matrix with a given size.
*/
SymmetricMatrix::SymmetricMatrix(const int matrixSize){
size = matrixSize;
matrices = new int*[size];
for(int a = 0; a < size; a++)
matrices
}
/*
* 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 SymmetricMatrix::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
int end = 0;
for(int a = 0; a < size; && end != 1; a++){
for(int b = 0; b < a + 1; && end != 1; b++){
if(inputFile.eof() == true){
inputFile >> value;
matrices
}
else {
end = 1;
}
}
}
//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 SymmetricMatrix::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
outputFile << value;
for(int a = 0; a < size; a++) {
for(int b = 0; b < a + 1; b++){
value = matrices
outputFile << value;
}
for(int b = a + 1; b < size; b++){
value = matrices
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 SymmetricMatrix::print(){
for(int a =0; a < size; a++){
for(int b = 0; b < a + 1; b++){
cout << setw(10)<< matrices
}
for(int b = a + 1; b < size; b++){
cout << setw(10)<< matrices
}
cout<<endl;
}
}
/*
* 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 SymmetricMatrix::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 < a + 1; b++){
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 SymmetricMatrix::add(const SymmetricMatrix& matrix1){
for(int a = 0; a < size; a++){
for(int b = 0; b < a + 1; b++){
matrices
}
}
}
/*
* Adds the given scalar value to every element of the matrix member of the
* object for which the function is invoked.
*/
void SymmetricMatrix::add(const int value){
for(int a = 0; a < size; a++){
for(int b = 0; b < a + 1; b++){
matrices
}
}
}
/*
* 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 SymmetricMatrix::multiply(const SymmetricMatrix& matrix1, const SymmetricMatrix& matrix2){
for(int a = 0; a < size; a++){
for(int b = 0; b < a + 1; b++){
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 SymmetricMatrix::multiply(const SymmetricMatrix& matrix1){
for(int a = 0; a < size; a++){
for(int b = 0; b < a + 1; b++){
matrices
}
}
}
/*
* Multiplies every element of the matrix member of the object for which
* the function is invoked by the given scalar value.
*/
void SymmetricMatrix::multiply(const int value){
for(int a = 0; a < size; a++){
for(int b = 0; b < a + 1; b++){
matrices
}
}
}
/*
* 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 SymmetricMatrix::subtract(const SymmetricMatrix& matrix1, const SymmetricMatrix& matrix2){
for(int a = 0; a < size; a++){
for(int b = 0; b < a + 1; b++){
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 SymmetricMatrix::subtract(const SymmetricMatrix& matrix1){
for(int a = 0; a < size; a++){
for(int b = 0; b < a + 1; b++){
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 SymmetricMatrix::subtract(const int value){
for(int a = 0; a < size; a++){
for(int b = 0; b < a + 1; b++){
matrices
}
}
}
/*
* Resizes the matrix member with the given new size.
* (Note: Contents of the initial matrix are lost after this operation.)
*/
void SymmetricMatrix::resize(const int newSize){
size = newSize;
}
/*
* Deallocates the memory used by the matrix member.
*/
void SymmetricMatrix::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 SymmetricMatrix::getSize() {
return size * size;
}
/*
* Returns the value of the matrix element at the given row and column.
*/
int SymmetricMatrix::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 SymmetricMatrix::setValue(const int row, const int column, const int newValue){
matrices[row][column] = newValue;
matrices[column][row] = newValue;
}
As you see, I changed them very much but I still take a lot of syntax errors about the majority of the codes. I do not know the reason and can not find.
Wold you please help me in detecting errors in my .cpp file
Thanks
newgreen
1) In the private: section you declare a value variable "SymmetricMatrix matrix;" why This is an error as you are currently defining the SymmetricMatrix class, and cannot use it by value within the SymmetricMatrix class, you have already defined data fields as public so i dont see why you have done this.
2) You access the matrices "matrices" field using "::" which is either for namespace resolution or accessing a static member, the "matrices" field is not static so this will not work, use the "." member selection operator instead ie: matrix1.matrices[ a ][ b ]
3) When reading from the stream you use the following while loop "while(inputFile.eof() == true){ ... };" the code in between the {} will never execute unless the file is empty, as it is testing for the end of file, i assume you meant to put "while(!inputFile.eof()) { ... };" ie to execute the code within the braces while the file position has not reached the end.
4) You use the following code "*matrix1::matrices[ a ][ b ]" there are 2 errors here, the first was covered in point 2 above, the second error is your using the dereference operator *, this is unneccesary as you implicitly dereference the pointers when using the double subscript [][],
the correct method to access this data field would be "matrix1.matrices[ a ][ b ]"
5) In the stream functions (ie read(string filename)), you declare a variable "filename" which has the same name as the parameter "filename" this is an error, dont bother declaring the second variable, just use the parameter passed to the function.
6) Your resize method, other than storing the new size does not reallocate memory to accomodate the requested size.
7) You define a "free()" method, this implies that the user of the matrix must remember to call free, before variables drop out of scope, otherwise any allocated memory will not be returned to the system, you should declare a destructor, and call the "free()" member from within it, to ensure your code is exception safe (ie deallocates memory if an exception is thrown).
8) You define 2 variables, "index1 and index2" you never assign a value to either, thus any code which uses them will have undefined behaviour. You use them both as a counter to access elements (without first setting them to zero), and as a limit within for loops.
9) Your constructor takes a single integer parameter, you should define this as explicit so that the compiler will not assume that your matrix can be converted to an int.
10) Why are you using std::setprecision Your matrix consists of integer values, and setprecision only affects floating point values, i assume you meant it to contain floating point .
11) When writing out your matrix values, you do not separate each value with whitespace, thus when reading in the values things will become confused.
Given the above points, i hope that the following implementation fits what you intended:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
class SymmetricMatrix
{
public:
int size;
float **matrices;
explicit SymmetricMatrix(const int matrixSize)
:size(0),
matrices(0)
{
resize(matrixSize);
}
~SymmetricMatrix()
{
free();
};
void read(const std::string filename){
float value;//, index1=0, index2=0;
std::ifstream inputFile;
inputFile.open( filename.c_str(), std::ios_base::in );
while(!inputFile.eof()){
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
if(!(inputFile >> value))
return;
matrices[ i ][ j ] = value;
}
}
}
inputFile.close();
}
void write(const std::string filename) {
std::ofstream outputFile;
outputFile.open( filename.c_str(), std::ios_base::out );
for(int a = 0; a < size; a++) {
for(int b = 0; b < size; b++){
outputFile << matrices[ a ][ b ] << " ";
}
}
outputFile.close();
}
void print(){
for (int i=0; i < size; i++) {
for (int j=0; j < size; j++)
{
std::cout << std::fixed << std::setprecision(4) << std::setw(10);
std::cout.setf(std::ios::right);
std::cout << matrices[ i ][ j ];
}
std::cout << '\n';
}
std::cout << "width is "<<std::cout.width() << " and precision is "<<std::cout.precision()<<'\n';
}
void add(const SymmetricMatrix &matrix1, const SymmetricMatrix &matrix2){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrices[ a ][ b ] = matrix1.matrices[ a ][ b ] + matrix2.matrices[ a ][ b ];
}
}
}
void add(const SymmetricMatrix& matrix1){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrices[ a ][ b ] += matrix1.matrices[ a ][ b ];
}
}
}
void add(const int value){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrices[ a ][ b ] += value;
}
}
}
void multiply(const SymmetricMatrix& matrix1, const SymmetricMatrix& matrix2){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrices[ a ][ b ] = matrix1.matrices[ a ][ b ] * matrix2.matrices[ a ][ b ];
}
}
}
void multiply(const SymmetricMatrix& matrix1){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrices[ a ][ b ] *= matrix1.matrices[ a ][ b ];
}
}
}
void multiply(const int value){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrices[ a ][ b ] *= value;
}
}
}
void subtract(const SymmetricMatrix& matrix1, const SymmetricMatrix& matrix2){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrices[ a ][ b ] = matrix1.matrices[ a ][ b ] - matrix2.matrices[ a ][ b ];
}
}
}
void subtract(const SymmetricMatrix& matrix1){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrices[ a ][ b ] -= matrix1.matrices[ a ][ b ];
}
}
}
void subtract(const int value){
for(int a = 0; a < size; a++){
for(int b = 0; b < size; b++){
matrices[ a ][ b ] -= value;
}
}
}
void resize(const int newSize){
if(newSize != size)
{
free();
matrices = new float*[newSize];
for(int a = 0; a < newSize; a++)
matrices[ a ] = new float[newSize];
size = newSize;
};
}
void free(){
for(int a = 0; a < size; a++){
delete[] matrices[ a ];
}
delete[] matrices;
matrices = 0;
size = 0;
}
int getTotalSize()const {
return size * size;
}
float getValue(const int row, const int column)const {
return matrices[row][column];
}
void setValue(const int row, const int column, const float newValue){
matrices[row][column] = newValue;
}
};
cheesy test program:
int main(int argc, char* argv[])
{
SymmetricMatrix m(4);
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(i != j)
m.setValue(i,j,0);
else
m.setValue(i,j,1);
}
};
m.write("matrix_out.txt");
m.read("matrix_out.txt");
m.print();
std::cout << std::endl;
std::cin.get();
return 0;
};
AlexDP
Please read over the points i made earlier regarding your code, the code i pasted earlier has been tested and compiles along with the test program.
Ill summerize what i have noticed here:
1) Again you are defining a local variable with the same name as the argument passed to the function, this is an error:
void SymmetricMatrix::read(const string filename){
//Declare variables
string filename = filename; << dont do this! just use the "const string filename" passed to the function
2) Again your using the wrong test whilst reading from your file
in void SymmetricMatrix::read(const string filename){
...
if(inputFile.eof() == true){ <<code in these braces will only execute when we reach the end of file! This should read "if(inputFile.eof() != true){"
3) Your for loops are messed up:
for(int a = 0; a < size; a++){
for(int b = 0; b < a + 1; b++){ << why a+1 just use "for(int b=0; b<size; b++)"
...
}
}
4) Why did you do the following it looks like nonsense to me, unless you intend always to set 2 different elements to the same value, you will get some strange results:
void SymmetricMatrix::setValue(const int row, const int column, const int newValue){
matrices[row][column] = newValue;
matrices[column][row] = newValue; << this line looks unneccesary and unwanted
}
I notice that you have corrected some of the errors you made earlier, keep at it you will get there!
mattyw87
C++HomeWork.cpp:140: error: syntax error before `++' token
C++HomeWork.cpp:150: error: syntax error before `void'
C++HomeWork.cpp:152: error: `size' was not declared in this scope
C++HomeWork.cpp:152: error: syntax error before `;' token
C++HomeWork.cpp:152: error: syntax error before `++' token
C++HomeWork.cpp:153: error: `a' was not declared in this scope
C++HomeWork.cpp:153: error: syntax error before `;' token
C++HomeWork.cpp:153: error: syntax error before `++' token
C++HomeWork.cpp:163: error: syntax error before `void'
C++HomeWork.cpp:165: error: `size' was not declared in this scope
C++HomeWork.cpp:165: error: syntax error before `;' token
C++HomeWork.cpp:165: error: syntax error before `++' token
C++HomeWork.cpp:166: error: `a' was not declared in this scope
C++HomeWork.cpp:166: error: syntax error before `;' token
C++HomeWork.cpp:166: error: syntax error before `++' token
C++HomeWork.cpp:175: error: syntax error before `void'
C++HomeWork.cpp:177: error: `size' was not declared in this scope
C++HomeWork.cpp:177: error: syntax error before `;' token
C++HomeWork.cpp:177: error: syntax error before `++' token
C++HomeWork.cpp:178: error: `a' was not declared in this scope
C++HomeWork.cpp:178: error: syntax error before `;' token
C++HomeWork.cpp:178: error: syntax error before `++' token
C++HomeWork.cpp:190: error: syntax error before `void'
C++HomeWork.cpp:192: error: `size' was not declared in this scope
C++HomeWork.cpp:192: error: syntax error before `;' token
C++HomeWork.cpp:192: error: syntax error before `++' token
C++HomeWork.cpp:193: error: `a' was not declared in this scope
C++HomeWork.cpp:193: error: syntax error before `;' token
C++HomeWork.cpp:193: error: syntax error before `++' token
C++HomeWork.cpp:204: error: syntax error before `void'
C++HomeWork.cpp:205: error: `size' was not declared in this scope
C++HomeWork.cpp:205: error: syntax error before `;' token
C++HomeWork.cpp:205: error: syntax error before `++' token
C++HomeWork.cpp:206: error: `a' was not declared in this scope
C++HomeWork.cpp:206: error: syntax error before `;' token
C++HomeWork.cpp:206: error: syntax error before `++' token
C++HomeWork.cpp:217: error: syntax error before `void'
C++HomeWork.cpp:219: error: `size' was not declared in this scope
C++HomeWork.cpp:219: error: syntax error before `;' token
C++HomeWork.cpp:219: error: syntax error before `++' token
C++HomeWork.cpp:220: error: `a' was not declared in this scope
C++HomeWork.cpp:220: error: syntax error before `;' token
C++HomeWork.cpp:220: error: syntax error before `++' token
C++HomeWork.cpp:229: error: syntax error before `void'
C++HomeWork.cpp:235: error: syntax error before `void'
C++HomeWork.cpp:236: error: `size' was not declared in this scope
C++HomeWork.cpp:236: error: syntax error before `;' token
C++HomeWork.cpp:236: error: syntax error before `++' token
C++HomeWork.cpp:245: error: syntax error before `int'
C++HomeWork.cpp:251: error: syntax error before `int'
C++HomeWork.cpp:259: error: syntax error before `void'
C++HomeWork.cpp:262: error: `column' was not declared in this scope
C++HomeWork.cpp:262: error: `row' was not declared in this scope
C++HomeWork.cpp:262: error: ISO C++ forbids declaration of `matrices' with no type
C++HomeWork.cpp:262: error: `newValue' was not declared in this scope
C++HomeWork.cpp:262: error: assignment (not initialization) in declaration
C++HomeWork.cpp:263: error: syntax error before `}' token
C++HomeWork.cpp:41: error: storage size of `inputFile' isn't known
[m_ozkaya@knuth Homework]$ C++HomeWork.cpp:42: error: syntax error before `.' token
> C++HomeWork.cpp:47: error: `size' was not declared in this scope
> C++HomeWork.cpp:47: error: syntax error before `;' token
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 3: syntax error: unexpected end of file
-bash: C++HomeWork.cpp:42:: command not found
[m_ozkaya@knuth Homework]$ C++HomeWork.cpp:47: error: syntax error before `++' token
> C++HomeWork.cpp:48: error: `a' was not declared in this scope
> C++HomeWork.cpp:48: error: syntax error before `;' token
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 3: syntax error: unexpected end of file
-bash: C++HomeWork.cpp:47:: command not found
[m_ozkaya@knuth Homework]$ C++HomeWork.cpp:48: error: syntax error before `++' token
> C++HomeWork.cpp:51: error: `a' was not declared in this scope
> C++HomeWork.cpp:51: error: `b' was not declared in this scope
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 3: syntax error: unexpected end of file
-bash: C++HomeWork.cpp:48:: command not found
[m_ozkaya@knuth Homework]$ C++HomeWork.cpp:51: error: ISO C++ forbids declaration of `matrices' with no type
> C++HomeWork.cpp:51: error: assignment (not initialization) in declaration
> C++HomeWork.cpp:52: error: syntax error before `}' token
> C++HomeWork.cpp:60: error: syntax error before `.' token
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 4: syntax error: unexpected end of file
-bash: C++HomeWork.cpp:51:: command not found
C++HomeWork.cpp:67: error: syntax error before `void'
[m_ozkaya@knuth Homework]$ C++HomeWork.cpp:67: error: syntax error before `void'
> C++HomeWork.cpp:71: error: syntax error before `.' token
> C++HomeWork.cpp:74: error: syntax error before `<<' token
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 3: syntax error: unexpected end of file
-bash: C++HomeWork.cpp:67:: command not found
[m_ozkaya@knuth Homework]$ C++HomeWork.cpp:75: error: `size' was not declared in this scope
> C++HomeWork.cpp:75: error: syntax error before `;' token
> C++HomeWork.cpp:75: error: syntax error before `++' token
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 3: syntax error: unexpected end of file
-bash: C++HomeWork.cpp:75:: command not found
-bash: token
C++HomeWork.cpp:75: error: syntax error before `++: command not found
[m_ozkaya@knuth Homework]$ C++HomeWork.cpp:76: error: `a' was not declared in this scope
> C++HomeWork.cpp:76: error: syntax error before `;' token
> C++HomeWork.cpp:76: error: syntax error before `++' token
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 3: syntax error: unexpected end of file
-bash: C++HomeWork.cpp:76:: command not found
C++HomeWork.cpp:78: error: syntax error before `<<' token
-bash: token
C++HomeWork.cpp:76: error: syntax error before `++: command not found
[m_ozkaya@knuth Homework]$ C++HomeWork.cpp:78: error: syntax error before `<<' token
> C++HomeWork.cpp:80: error: `size' was not declared in this scope
> C++HomeWork.cpp:80: error: syntax error before `;' token
-bash: command substitution: line 1: unexpected EOF while looking for matching `''
-bash: command substitution: line 3: syntax error: unexpected end of file
-bash: C++HomeWork.cpp:78:: command not found
[m_ozkaya@knuth Homework]$ C++HomeWork.cpp:80: error: syntax error before `++' token
> C++HomeWork.cpp:82: error: syntax error before `<<' token
> C++HomeWork.cpp:88: error: syntax error before `.' token
> C++HomeWork.cpp:94: error: syntax error before `void'
> C++HomeWork.cpp:96: error: `size' was not declared in this scope
> C++HomeWork.cpp:96: error: syntax error before `;' token
> C++HomeWork.cpp:96: error: syntax error before `++' token
> C++HomeWork.cpp:97: error: `a' was not declared in this scope
> C++HomeWork.cpp:97: error: syntax error before `;' token
> C++HomeWork.cpp:97: error: syntax error before `++' token
> C++HomeWork.cpp:100: error: `size' was not declared in this scope
> C++HomeWork.cpp:100: error: syntax error before `;' token
> C++HomeWork.cpp:100: error: syntax error before `++' token
> C++HomeWork.cpp:103: error: syntax error before `<<' token
> C++HomeWork.cpp:110: error: syntax error before `void'
> C++HomeWork.cpp:113: error: `size' was not declared in this scope
> C++HomeWork.cpp:113: error: syntax error before `;' token
> C++HomeWork.cpp:113: error: syntax error before `++' token
> C++HomeWork.cpp:114: error: `a' was not declared in this scope
> C++HomeWork.cpp:114: error: syntax error before `;' token
> C++HomeWork.cpp:114: error: syntax error before `++' token
> C++HomeWork.cpp:125: error: syntax error before `void'
> C++HomeWork.cpp:126: error: `size' was not declared in this scope
> C++HomeWork.cpp:126: error: syntax error before `;' token
> C++HomeWork.cpp:126: error: syntax error before `++' token
> C++HomeWork.cpp:127: error: `a' was not declared in this scope
> C++HomeWork.cpp:127: error: syntax error before `;' token
> C++HomeWork.cpp:127: error: syntax error before `++' token
> C++HomeWork.cpp:138: error: syntax error before `void'
> C++HomeWork.cpp:139: error: `size' was not declared in this scope
> C++HomeWork.cpp:139: error: syntax error before `;' token
> C++HomeWork.cpp:139: error: syntax error before `++' token
> C++HomeWork.cpp:140: error: `a' was not declared in this scope
> C++HomeWork.cpp:140: error: syntax error before `;' token
> C++HomeWork.cpp:140: error: syntax error before `++' token
> C++HomeWork.cpp:150: error: syntax error before `void'
> C++HomeWork.cpp:152: error: `size' was not declared in this scope
> C++HomeWork.cpp:152: error: syntax error before `;' token
> C++HomeWork.cpp:152: error: syntax error before `++' token
> C++HomeWork.cpp:153: error: `a' was not declared in this scope
> C++HomeWork.cpp:153: error: syntax error before `;' token
> C++HomeWork.cpp:153: error: syntax error before `++' token
> C++HomeWork.cpp:163: error: syntax error before `void'
> C++HomeWork.cpp:165: error: `size' was not declared in this scope
> C++HomeWork.cpp:165: error: syntax error before `;' token
> C++HomeWork.cpp:165: error: syntax error before `++' token
> C++HomeWork.cpp:166: error: `a' was not declared in this scope
> C++HomeWork.cpp:166: error: syntax error before `;' token
> C++HomeWork.cpp:166: error: syntax error before `++' token
> C++HomeWork.cpp:175: error: syntax error before `void'
> C++HomeWork.cpp:177: error: `size' was not declared in this scope
> C++HomeWork.cpp:177: error: syntax error before `;' token
> C++HomeWork.cpp:177: error: syntax error before `++' token
> C++HomeWork.cpp:178: error: `a' was not declared in this scope
> C++HomeWork.cpp:178: error: syntax error before `;' token
> C++HomeWork.cpp:178: error: syntax error before `++' token
> C++HomeWork.cpp:190: error: syntax error before `void'
> C++HomeWork.cpp:192: error: `size' was not declared in this scope
> C++HomeWork.cpp:192: error: syntax error before `;' token
> C++HomeWork.cpp:192: error: syntax error before `++' token
> C++HomeWork.cpp:193: error: `a' was not declared in this scope
> C++HomeWork.cpp:193: error: syntax error before `;' token
> C++HomeWork.cpp:193: error: syntax error before `++' token
> C++HomeWork.cpp:204: error: syntax error before `void'
> C++HomeWork.cpp:205: error: `size' was not declared in this scope
> C++HomeWork.cpp:205: error: syntax error before `;' token
> C++HomeWork.cpp:205: error: syntax error before `++' token
> C++HomeWork.cpp:206: error: `a' was not declared in this scope
> C++HomeWork.cpp:206: error: syntax error before `;' token
> C++HomeWork.cpp:206: error: syntax error before `++' token
> C++HomeWork.cpp:217: error: syntax error before `void'
> C++HomeWork.cpp:219: error: `size' was not declared in this scope
> C++HomeWork.cpp:219: error: syntax error before `;' token
> C++HomeWork.cpp:219: error: syntax error before `++' token
> C++HomeWork.cpp:220: error: `a' was not declared in this scope
> C++HomeWork.cpp:220: error: syntax error before `;' token
> C++HomeWork.cpp:220: error: syntax error before `++' token
> C++HomeWork.cpp:229: error: syntax error before `void'
> C++HomeWork.cpp:235: error: syntax error before `void'
> C++HomeWork.cpp:236: error: `size' was not declared in this scope
> C++HomeWork.cpp:236: error: syntax error before `;' token
> C++HomeWork.cpp:236: error: syntax error before `++' token
> C++HomeWork.cpp:245: error: syntax error before `int'
> C++HomeWork.cpp:251: error: syntax error before `int'
> C++HomeWork.cpp:259: error: syntax error before `void'
> C++HomeWork.cpp:262: error: `column' was not declared in this scope
> C++HomeWork.cpp:262: error: `row' was not declared in this scope
> C++HomeWork.cpp:262: error: ISO C++ forbids declaration of `matrices' with no type
> C++HomeWork.cpp:262: error: `newValue' was not declared in this scope
> C++HomeWork.cpp:262: error: assignment (not initialization) in declaration
> C++HomeWork.cpp:263: error: syntax error before `}' token
> C++HomeWork.cpp:41: error: storage size of `inputFile' isn't known
>
So how can I fix them
Please help me to fix them .
Thanks and Regards
Pekuja
I did whay you adviced me to do but there is no any missing brackets or something lese that I saw.
Reta