Click here to Skip to main content
15,035,723 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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) //Iterate through original data and copy these values into the new object data.
      |        ^
hw5-matrix.cpp:27:9: error: expected primary-expression before ‘const’
   27 |  Matrix(const Matrix &original) //Iterate through original data and copy these values into the new object data.
      |         ^~~~~
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 | { // Will write to the output file in the same format as the input matrix files. Will use the fprintf function to the print the data to the file; fprintf(file,"%f", value);
      | ^
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");
	//This will read one number as a double from the file into the variable value.
	// TODO read in the data from filename into the data array
	// use fopen to open te file and fsscanf to read te data
	for (int i = 0; i < size; i++)	// for our rows.
	{
		for (int j = 0; j < size; j++);
		{
			fscanf(file, "%lf", &value);
		}
 }
	{
	//Create two nested loops that iterate over matrix array going through our rows in the outer loop and the columns in the inner loop to read the data from the file into the data array of data[i*m+j];
	Matrix::Matrix(int m)
	: size(m)
	{
		data = new double[size *size];
	}

	Matrix(const Matrix &original)	//Iterate through original data and copy these values into the new object data.
		: size(original.size)
		{
			data = new double[size *size];
			for (int i = 0; i < size; i++)
			{
				data[i *m + j] = original.data[i *m + j];
			}
		}

	// TODO copy te data from the original matrix into this object's matrix
}

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++)
		{
			//Do the multiplication
			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;	// 
	}

	// TODO do the multiply and put the result into the out matrix
	return out;
}

void write(char *filename)
{	// Will write to the output file in the same format as the input matrix files. Will use the fprintf function to the print the data to the file; fprintf(file,"%f", value);
	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");	// print a new line
	}

	// TODO write the matrix out to the file with each row on one line with
	// the data separated by spaces.

	fclose(file);
}

//I will iterate over the rows and columns of data to print each element of the matrix to the file. Place a newline at the end of each row. 

//The data field is an array that will store the 2-D data for our matrix.
//We 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, we instead use data[i*m+j]
// Within the first class constructor; File open only for file a.
// Nested for loop with the outside for loop iterating through rows and inner through the columns. The inner one will have the fscanfcode.
//The copy construct is similar to the vector lab.
//For the dot product use the original data and b data. 
// write; Open a new file to write a function, then do two for loops for the rows and columns, the the innerone will consist for the fprintf for the numbers and the outer one will consist of the fprintf for newlines.


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; // data array
    double *data; //data variable
};


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.
Posted
Updated 27-Apr-21 16:43pm

1 solution

The errant line is :
C++
Matrix operator * (Matrix&) const;
The statement does not have the correct form of return type. It should look like this :
C++
Matrix * operator(Matrix&) const;
I'm not sure that's the form you want but that is correct.
   
v2
Comments
Member 15084336 29-Apr-21 13:06pm
   
I went over this error. Everything has compiled but nothing is in actually shown in the the terminal.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900