I submitted a new question since I was not able to delete the mess of code that I had earlier upon debugging. The primary objective is to write four methods that will write in to my text-c file. I can't use data[i] or data[j] because the array is variable sized.Since we want the element at row i and column j, I will use instead use data[i*m+j].
For the first method;
I will read one number as a double from the file into the variable value.Which will then read in the data from filename into the data array and then use fopen to open the file and fsscanf to read the data. I called two nested loops that iterated over my matrix array going through the rows in the outer loop and columns in the inner loop. This seems fine so far.
The second method; Copy constructor.
I simply iterated over the original data and copied the values into the new object data or B.
For the third method; Overload operator;
Multiplied the current matrix and the b matrix with a series of nested loops. The first two I wanted to iterate over the i and j rows and columns of the ouput matrix out. The third and innermost loop will iterate over the i-th row of the current data and the j-th column of b data to do the dot product. This innermost loop will do an accumulation of the
multiplications to calculate the dot product and the accumulated sum will go into the
out.data element at row i and column j.
The fourth method; write
This method should write to the output file in the same format as the input matrix files.
I'm using the fprintf function to print the data to the file
fprintf(file, "%f ", value);
I iterated over the rows and columns of data to print each element of
the matrix to the file. I also attempted to put a newline at the end of each.
What I have tried:
I received these errors;
In constructor ‘Matrix::Matrix(int, char*)’:
<pre> error: expected primary-expression before ‘int’
*So with this it mentions that there needs to be a primary expression before 'int'. Wouldn't this not be changed from it's original matrix-cpp file?
cannot call constructor ‘Matrix::Matrix’ directly [-fpermissive]
*Stumped on this as I assumed the constructor could be called directly.
hw5-matrix.cpp:21:22: note: for a function-style cast, remove the redundant ‘::Matrix’
hw5-matrix.cpp:27:8: error: expected primary-expression before ‘(’ token
27 | Matrix(const Matrix &original)
| ^
hw5-matrix.cpp:27:9: error: expected primary-expression before ‘const’
27 | Matrix(const Matrix &original)
| ^~~~~
hw5-matrix.cpp:40:18: error: expected ‘;’ before ‘{’ token
40 | Matrix::~Matrix()
| ^
| ;
41 | {
| ~
hw5-matrix.cpp:45:28: error: qualified-id in declaration before ‘(’ token
45 | Matrix Matrix::operator * (Matrix & b) const
| ^
hw5-matrix.cpp:70:1: error: a function-definition is not allowed here before ‘{’ token
70 | {
| ^
hw5-matrix.cpp:88:1: error: expected ‘}’ at end of input
88 | }
| ^
hw5-matrix.cpp:5:1: note: to match this ‘{’
5 | {
| ^
#include "Matrix.h"
#include <stdio.h>
Matrix::Matrix(int m, char *filename): size(m)
{
double value;
data = new double[size *size];
FILE *file = fopen(filename, "r");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++);
{
fscanf(file, "%lf", &value);
}
}
{
Matrix::Matrix(int m)
: size(m)
{
data = new double[size *size];
}
Matrix(const Matrix &original)
: size(original.size)
{
data = new double[size *size];
for (int i = 0; i < size; i++)
{
data[i *m + j] = original.data[i *m + j];
}
}
}
Matrix::~Matrix()
{
delete[] data;
}
Matrix Matrix::operator * (Matrix & b) const
{
Matrix out(size);
int sum = 0;
int i = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
double sum = 0.0;
for (int k = 0; k < size; k++)
{
sum += original.data*[i*size + j]*b.data*[k*m + j]
}
}
out.data[i*size + j] = sum;
}
return out;
}
void write(char *filename)
{
int size = 0;
double value;
FILE *file = fopen(filename, "w");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
fprintf(file, "%.2f", value);
}
fprintf(file, "\n");
}
fclose(file);
}
Matrix.h file.
class Matrix {
public:
Matrix(int);
Matrix(int, char *);
Matrix(const Matrix &);
~Matrix();
Matrix operator * (Matrix&) const;
void write(char *);
private:
int size;
double *data;
};
Matrix Main file
#include <stdlib.h>
#include "Matrix.h"
int main(int argc, char **argv)
{
int m = atoi(argv[1]);
Matrix A(m, argv[2]);
Matrix B(m, argv[3]);
Matrix C = A * B;
C.write(argv[4]);
}
Matrix a-matrix.txt file
2.3 1.9 0.5
1.5 1.3 3.1
3.3 1.2 2.5
Matrix-b-matrix.txt file
1.2 2.3 3.2
2.6 0.3 2.1
3.1 1.1 2.7
These two matrixes should produce a 3x3 matrix of outputted values using the dot product in my blank matrix-c-matrix.txt file.
I'd appreciate any tips or suggestions to better improve my code and see what's going on in terms of my errors as I'm also trying to debug using gdb as a beginner.
Thanks.