5 Replies - 373 Views - Last Post: 04 December 2017 - 11:12 PM Rate Topic: -----

#1 shive   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 17

Reading data from a file and performing matrix multiplication in c++

Posted 03 December 2017 - 10:54 PM

hii.. i am new to c++ and am trying to perform a matrix multiplication reading data from a file.But am unable to get the multiplication part.I got the reading section of the data and its showing the two mtrices for multiplication but in the multiplication its showing the result bu the result is not the exact answer.Someone please help me to fix this.


Code:
#include <iostream>
#include <fstream>
#include <array>
#include <sstream>
#include <string>
#include <stdio.h>
#include <algorithm>

int row=0;
int col=0;
int i,j,k;
using namespace std;

int main()
{
string line;
ifstream myfile;
int x;
int arrayA[100][100] = {{0}};
int arrayB[100][100] = {{0}};
int arrayD[100][100] = {{0}};
myfile.open("numeric.txt");
  if(myfile.fail())
  {
   cerr<<"file does not exist";
  }
  cout<<"\n"<<endl;
  while(myfile.good())
   {
    while(getline(myfile,line))
     {
      istringstream stream(line);
      col=0;
      while(stream >> x)
      {
        arrayA[row][col]=x;
        col ++;
      }
        row ++;
     }

  for(int i=0;i<row;i++)
   {
     for(int j=0;j<col;j++)
     {
      cout<<arrayA[i][j]<< " " ;
    
     }
      cout<<endl;
   }
  cout<<"\n"<<endl;
 }
cout<<"first Matrix"<<endl;
	for(i=1;i<=3;i++)
	{
		for(j=0;j<3;j++)
		{
		   
			cout<<arrayA[i][j]<<" ";
		}
		cout<<endl;
	}


	cout<<"Second Matrix"<<endl;
	for(i=2;i<5;i++)
	{
		for(j=2;j<5;j++)

		{       
                         
                        arrayB[i][j] = arrayA[i][j];
                        cout<<arrayB[i][j]<<" ";
		}
		cout<<endl;
	}
cout<<"Multiplication Result"<<endl;
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{       
                        int sum = 0;	
			for(k=0;k<3;k++)
			{
				sum=sum+arrayA[i][k]*arrayB[k][j];
			}
                    arrayD[i][j]=sum;
                 }
	}
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			cout<<arrayD[i][j]<<" ";
		}
		cout<<endl;
	}
	
return 0;
}



Output which i have got is:
1 2 3 4 5 6 7 8
5 6 8 9 4 2 0 1
4 5 8 7 9 6 0 1
5 6 9 7 4 1 2 8
2 0 3 1 4 8 7 9
2 0 1 3 6 5 4 7
0 5 4 7 2 4 7 6
2 4 8 7 9 6 4 8


first Matrix
5 6 8
4 5 8
5 6 9
Second Matrix
8 7 9
9 7 4
3 1 4
Multiplication Result
0 0 24
0 0 64
0 0 64

This post has been edited by andrewsw: 04 December 2017 - 02:00 AM
Reason for edit:: added missing [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Reading data from a file and performing matrix multiplication in c++

#2 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,527
  • Joined: 12-December 12

Re: Reading data from a file and performing matrix multiplication in c++

Posted 04 December 2017 - 02:02 AM

Please wrap your code in code tags, it shows how to do this directly in the area where you type your post.

Please provide a clear question, with details of input and output.

Added: For anyone reading, I just noticed that there is some output information at the end of the code. (Not the best place for it.)
Was This Post Helpful? 0
  • +
  • -

#3 GazinAtCode   User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 121
  • Joined: 26-September 16

Re: Reading data from a file and performing matrix multiplication in c++

Posted 04 December 2017 - 02:32 AM

The multiplication algorithm itself seems OK, but you're not taking proper care of dimensions and indexing.

Why do you set the size of each array to [100][100] if later on you extract two 3-by-3 matrices from the input data?

Let's say data represents the matrix with your input values, arrayA represents a matrix to be multiplied by arrayB, and result holds the output.
cout << "First Matrix" << endl;
for (i = 1; i <= 3; i++) {
    for (j = 0; j < 3; j++) {
        arrayA[i - 1][j] = data[i][j];
        cout << data[i][j] << " ";
    }
    cout << endl;
}
cout << "Second Matrix" << endl;
for (i = 2; i < 5; i++) {
    for (j = 2; j < 5; j++) {
        arrayB[i - 2][j - 2] = data[i][j];
        cout << data[i][j] << " ";
    }
    cout << endl;
}
cout << "Multiplication Result" << endl;
for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
        int sum = 0;
        for (k = 0; k < 3; k++) {
            sum += arrayA[i][k] * arrayB[k][j];
        }
        result[i][j] = sum;
    }
}
for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
        cout << result[i][j] << " ";
    }
    cout << endl;
}

First Matrix
5 6 8
4 5 8
5 6 9
Second Matrix
8 7 9
9 7 4
3 1 4
Multiplication Result
118 85 101
101 71 88
121 86 105

This post has been edited by GazinAtCode: 04 December 2017 - 02:38 AM

Was This Post Helpful? 1
  • +
  • -

#4 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Reading data from a file and performing matrix multiplication in c++

Posted 04 December 2017 - 08:07 AM

The whole thing could be more neatly done in a class with operator overloading such as..

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

class Matrix
{
private:
    
	char readData[20];
	ifstream myfile;
public:
	int row, column;
	int** data;



	Matrix(int Row, int Column)
	{
		row = Row; column = Column;

		if (!row == column)
		{
			cout << "Warning: Matrix Row must equal Column." << endl;
		}
		else
		{
			data = new int* [row];
			for (int i = 0; i < column; i++)
			{
				data[i] = new int[row];
				memset(data[i], 0, column * sizeof(int));
			}
			
		}
	};
	void readFromFile(string Filename)
	{
		myfile.open(Filename.c_str());
		if (myfile.fail())
			
		{
			
				cerr << "file does not exist";
			
		}
		for (int j = 0; j < column; j++)
		{
			for (int i = 0; i < row; i++)
			{
				myfile >> readData;
				stringstream ss(readData);
				ss >> data[i][j];
			}
		}
	};

	Matrix& operator=(const Matrix &rhs)
	{
		
		for (int j = 0; j < rhs.column; j++)
		{
			for (int i = 0; i < rhs.row; i++)
			{

				this->data[i][j] =  rhs.data[i][j];

			}

		}

		return *this;

	};
	
	
};

std::ostream& operator<<(std::ostream& out, Matrix& A)
{
	for (int j = 0; j < A.column; j++)
	{
		for (int i = 0; i < A.row; i++)
		{
			out << A.data[i][j] << " ";
		}
		out << endl;
	}
	return out;
}

Matrix& operator*(Matrix &lhs, Matrix &rhs)
{
	Matrix product(lhs.row, rhs.column);
	for (int j = 0; j < rhs.column; j++)
	{
		for (int i = 0; i < lhs.row; i++)
		{
			for (int inner = 0; inner < lhs.column; inner++)
			{
				product.data[j][i] += lhs.data[inner][i] * rhs.data[j][inner];
			}
		}

	}

	lhs = product;
	return lhs;

};


int main()
{

	Matrix One(3, 3);
	Matrix Two(3, 3);
	Matrix Three(3, 3);
	One.readFromFile("matrixA.txt");
	Two.readFromFile("matrixB.txt");
	Three = One*Two;

	cout << Three;


	cin.get();
	return 0;
}



This is just a rough example error checking needs to be added to it....
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Reading data from a file and performing matrix multiplication in c++

Posted 04 December 2017 - 10:57 AM

The above class is just for square matrices a more General class would be like this

class Matrix
{
private:
    
	char readData[20];
	ifstream myfile;
public:
	int row, column;
	int** data;
	
	Matrix::Matrix(int Row, int Column)
	{
		row = Row; column = Column;

		
			data = new int* [row];
			for (int i = 0; i < row; i++)
			{
				data[i] = new int[column];
				memset(data[i], 0, row * sizeof(int));
			}
			
	
	};
	void readFromFile(string Filename)
	{
		myfile.open(Filename.c_str());
		if (myfile.fail())
			
		{
			
				cerr << "file does not exist";
			
		}
		for (int j = 0; j < row; j++)
		{
			for (int i = 0; i < column; i++)
			{
				myfile >> readData;
				stringstream ss(readData);
				ss >> data[j][i];
			}
		}
	};

	Matrix& operator=(const Matrix &rhs)
	{
		
		for (int j = 0; j < rhs.row; j++)
		{
			for (int i = 0; i < rhs.column; i++)
			{

				this->data[j][i] =  rhs.data[j][i];

			}

		}

		return *this;

	};
	
	
};

std::ostream& operator<<(std::ostream& out, Matrix& A)
{
	for (int j = 0; j < A.row; j++)
	{
		for (int i = 0; i < A.column; i++)
		{
			out << A.data[j][i] << " ";
		}
		out << endl;
	}
	return out;
}

Matrix& operator*(Matrix &lhs, Matrix &rhs)
{
	if (lhs.row == rhs.column)
	{
		Matrix* product = new Matrix(lhs.row, rhs.column);
		for (int j = 0; j < lhs.row; j++)
		{
			for (int i = 0; i < rhs.column; i++)
			{
				for (int inner = 0; inner < lhs.column; inner++)
				{
					product->data[j][i] += lhs.data[j][inner] * rhs.data[inner][i];
				}
			}

		}


		return *product;
	}

	else
		cout << "Warning: Cannot Multiply Matrices." << endl;

	return lhs;

};


Was This Post Helpful? 0
  • +
  • -

#6 shive   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-December 17

Re: Reading data from a file and performing matrix multiplication in c++

Posted 04 December 2017 - 11:12 PM

View PostGazinAtCode, on 04 December 2017 - 02:32 AM, said:

The multiplication algorithm itself seems OK, but you're not taking proper care of dimensions and indexing.

Why do you set the size of each array to [100][100] if later on you extract two 3-by-3 matrices from the input data?

Let's say data represents the matrix with your input values, arrayA represents a matrix to be multiplied by arrayB, and result holds the output.
cout << "First Matrix" << endl;
for (i = 1; i <= 3; i++) {
    for (j = 0; j < 3; j++) {
        arrayA[i - 1][j] = data[i][j];
        cout << data[i][j] << " ";
    }
    cout << endl;
}
cout << "Second Matrix" << endl;
for (i = 2; i < 5; i++) {
    for (j = 2; j < 5; j++) {
        arrayB[i - 2][j - 2] = data[i][j];
        cout << data[i][j] << " ";
    }
    cout << endl;
}
cout << "Multiplication Result" << endl;
for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
        int sum = 0;
        for (k = 0; k < 3; k++) {
            sum += arrayA[i][k] * arrayB[k][j];
        }
        result[i][j] = sum;
    }
}
for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
        cout << result[i][j] << " ";
    }
    cout << endl;
}

First Matrix
5 6 8
4 5 8
5 6 9
Second Matrix
8 7 9
9 7 4
3 1 4
Multiplication Result
118 85 101
101 71 88
121 86 105



Thanku so much for your reply... problem got solved with your help. :bigsmile:/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1