Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to iterate over the rows and columns of the data to print each element of each matrix to the file, which will be
hw5-c-matrix.txt
. The code compiles, yet nothing is outputted to that file. Would want to know, where I am going wrong. Thanks

What I have tried:

This is my matrix file.
#include "Matrix.h"
#include <stdio.h>

Matrix::Matrix(int m, char *filename)
: size(m)
{
    data = new double [size*size];

    // TODO read in the data from filename into the data array
    // use fopen to open te file and fsscanf to read te data
}

Matrix::Matrix(int m)
: size(m)
{
    data = new double [size*size];
}

Matrix::Matrix(const Matrix &original)
: size(original.size)
{
    data = new double [size*size];

    // 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);

    // TODO do the multiply and put the result into the out matrix

    return out;
}

void Matrix::write(char *filename)
{
    FILE *file = fopen(filename, "w");

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

    fclose(file);
}


My 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]);
}


HW5 A file which contains the first matrix for A.
2.3 1.9 0.5
1.5 1.3 3.1
3.3 1.2 2.5


HW5 B file which contains the second matrix for B. 

1.2 2.3 3.2
2.6 0.3 2.1
3.1 1.1 2.7

My Header 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;
};


#include <stdio.h>
#include <stdlib.h>


int main(int argc, char** argv) {
   int m = atoi(argv[1]); // matrix size
   char* afilename = argv[2]; // input matrix a
   char* bfilename = argv[3]; // input matrix b
   char* cfilename = argv[4]; // output matrix c

   // open files
   FILE* afile = fopen(afilename, "r");
   FILE* bfile = fopen(bfilename, "r");
   FILE* cfile = fopen(cfilename, "w");

   // create arrays to store matrix data
   double* amat = (double*)malloc(m * m * sizeof(double));
   double* bmat = (double*)malloc(m * m * sizeof(double));
   double* cmat = (double*)malloc(m * m * sizeof(double));

   double data;
   // read data for matrix a
   for (int i = 0; i < m; i++) {
       for (int j = 0; j < m; j++) {
           fscanf(afile, "%lf", &data);
           // store data in array
           amat[i * m + j] = data;
       }
   }
   // read data for matrix b
   for (int i = 0; i < m; i++) {
       for (int j = 0; j < m; j++) {
           fscanf(bfile, "%lf ", &data);
           // store data in array
           bmat[i * m + j] = data;
       }
   }

   // calculate dot product
   // read data for matrix a
   for (int i = 0; i < m; i++) {
       for (int j = 0; j < m; j++) {
           double sum = 0.0;
           for (int k = 0; k < m; k++) {
               sum += amat[i * m + k] * bmat[k * m + j];
           }
           // store result in c matrix
           cmat[i * m + j] = sum;
       }
   }

   // write result matrix to output file file
   // read data for matrix a
   for (int i = 0; i < m; i++) {
       for (int j = 0; j < m; j++) {
           data = cmat[i * m + j];
           fprintf(cfile, "%.2f ", data); // write upto 2 decimal points
       }
       fprintf(cfile, "\n"); // print newline
   }

   // release memory
   free(amat);
   free(bmat);
   free(cmat);

   return 0;
}
Posted
Updated 29-Apr-21 7:04am
Comments
Member 15084336 25-Apr-21 19:22pm
   
I meant to say written to the file which should be the output data in the hw5-c-matrix.txt file in the notepad editor. My text editor is Notepad++.

This
C++
void Matrix::write(char *filename)
{
    FILE *file = fopen(filename, "w");

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

    fclose(file);
}

suggest that you have to write code for the class Matrix.

C++
// write result matrix to output file file
// read data for matrix a
for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
        data = cmat[i * m + j];
        fprintf(cfile, "%.2f ", data); // write upto 2 decimal points
    }
    fprintf(cfile, "\n"); // print newline
}
// I would fclose the output file at this point.


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
v2
Comments
CPallini 26-Apr-21 3:49am
   
5.
Patrice T 26-Apr-21 3:52am
   
Thank you
Member 15084336 29-Apr-21 13:07pm
   
See below, went over some bugs.
#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::Matrix(const Matrix &original)	
: size(original.size)
{
	data = new double[size *size];
		for (int k = 0; k < original.size; k++){ //iterate rows.
			for (int d = 0; d < original.size; d++){ // iterates columns.
				data[k*size + d] = original.data[k*size + d]; //copies over it.
			}
		}
}

	// 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);
  for (int c = 0; c < size; c++) { //iterate overs rows
    for (int e = 0; e < size; e++) { //iterate over columns
      //Do the multiplication
     out.data[c*size +e] = 0;
      for (int l = 0; l < size; l++) {
        out.data[c*size + e] += data[c*size+e]* b.data[l*size + e];
		}
    }
// 
  }
  return out;
}

void Matrix::write(char *filename)
{	
	int size = 0;
	double value;
	FILE *file = fopen(filename, "w");
	for (int a = 0; a < size; a++)
	{
		for (int b = 0; b < size; b++)
		{
			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);
}




g++ -o hw5 hw5-matrix-main.cpp hw5-matrix.cpp

./hw5 3 hw5-a-matrix.txt hw5-b-matrix.txt more hw5-c-matrix.txt.


Updated version. Only issue I have now, is nothing is written to the hw5-c-matrix.txt file which should show in the command line as the dot product of the a and b matrix txt files. Any help would be appreciated as this is the last part of the code i find issues with. My Notepad++ is currently in Administrator mode, I don't know if this could possibly be an issue.
   

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