6 Replies - 3269 Views - Last Post: 05 September 2009 - 10:02 PM Rate Topic: -----

#1 tinychip  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 03-December 08

operator overload to add 2D-arrays

Posted 05 September 2009 - 12:28 AM

i am having problem with using operator overload to add 2D arrays then out put the result
please walk me through this.
Thanks in advance!
my code

header file
//plus.h

// vectors: overloading operators example
#include <iostream>
using namespace std;

class CVector 
{
  public:
				int a[3][3];
	int b[3][3];
	int add[3][3]; //result array
	

	CVector ();
//pass read data and store 2d arrays
  void read_data(char*filename, int a[][3], int b[][3]);
	
//overload operator +
	CVector operator + (CVector);
};




implementation
//file: plus.cpp

#include<iostream>
#include<fstream>
using std::ifstream;
using namespace std;

#include "plus.h"
 
CVector::CVector()
{
	
}
//get data
void CVector::read_data(char*filename, int a[][3], int b[][3])
{
	ifstream in(filename);

	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			in >> a[i][j];
			cout << a[i][j];
		}
	}

	for(int i=0; i<2; i++)
	{
		for(int j =0; j<3; j++)
		{
			in >> b[i][j];
		}
	}
	in.close();

}

// use operator overload to add 2 d arrays
CVector CVector::operator+ (CVector param) 
{
	 CVector temp;
	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{

// I DONT KNOW IF THIS IS RIGHT , 
//add one by one then assign it to add[][] which is the result of additon
		 temp.add[i][j] = param.a[i][j] + param.b[i][j];
//

		}
	}

  return temp;
}




main
// main.cpp

#include<iostream>
using namespace std;
#include "plus.h"

int main (int argc, char*argv[]) 
{
	

	//create 2 d arrays to pass to read_dat()
	
	int m[3][3];
	int n[3][3];
	
	CVector read;
	read.read_data(argv[1], m, n);

// i DONT KNOW IF THIS IS RIGHT , im trying to use the overload to add 2d arrays of m and n
			  
				CVector c;
				 c = m + n; // error here, cannot add two pointers
			   
	
// OUT PUT RESULT
  for(int i =0; i<3; i++)
  {
		for(int j=0; j<0; i++)
		{
			 cout << c.add[i][j]; // i DONT KNOW WHAT TO REPLACE WITH
		}
  }

  return 0;
}


This post has been edited by tinychip: 05 September 2009 - 12:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: operator overload to add 2D-arrays

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: operator overload to add 2D-arrays

Posted 05 September 2009 - 03:14 AM

you only need one array in each CVector than add the different objects, e.g.
//plus.h

// vectors: overloading operators example
#include <iostream>
using namespace std;

class CVector
{
  public:
				int a[3][3];

	CVector() {};
	CVector (int a[][3]);
//pass read data and store 2d arrays
  void read_data(char*filename, int a[][3]);

//overload operator +
	CVector operator + (CVector);
};


//file: plus.cpp

#include<iostream>
#include<fstream>
using std::ifstream;
using namespace std;

#include "plus.h"

CVector::CVector(int a[][3])
{
   for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			this->a[i][j] =a[i][j];
		   cout << a[i][j] << " ";
		}
		cout << endl;
	 }

}
//get data
void CVector::read_data(char*filename, int a[][3])
{
	ifstream in(filename);

	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			in >> a[i][j];
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	in.close();

}

// use operator overload to add 2 d arrays
CVector CVector::operator+ (CVector param)
{
	 CVector temp;
	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{

// I DONT KNOW IF THIS IS RIGHT ,
//add one by one then assign it to add[][] which is the result of additon
		 temp.a[i][j] = param.a[i][j] + a[i][j];
//

		}
	}

  return temp;
}


then you can
				 c = x + y; 


where c, x and y are type CVector

This post has been edited by horace: 05 September 2009 - 03:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 tinychip  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 03-December 08

Re: operator overload to add 2D-arrays

Posted 05 September 2009 - 01:29 PM

Thanks for your help. The program runs. But it out put some memory locations. (my infile.txt is error free)
I try to initialize a[][] = 0 inside the constructure but it doest get me any where.
Any ideas?


//plus.h

// vectors: overloading operators example
#include <iostream>
using namespace std;

class CVector
{
  public:
				int a[3][3];

	CVector() {};
	CVector (int a[][3]);
//pass read data and store 2d arrays
  void read_data(char*filename, int a[][3]);

//overload operator +
	CVector operator + (CVector);
	   void show();

};


//file: plus.cpp

#include<iostream>
#include<fstream>
using std::ifstream;
using namespace std;

#include "plus.h"

CVector::CVector(int a[][3])
{
   for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			this->a[i][j] =a[i][j];
		   cout << a[i][j] << " ";
		}
		cout << endl;
	 }

}
//get data
void CVector::read_data(char*filename, int a[][3])
{
	ifstream in(filename);

	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			in >> a[i][j];
			
		}
		cout << endl;
	}
	in.close();

}

// use operator overload to add 2 d arrays
CVector CVector::operator+ (CVector param)
{
	 CVector temp;
	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{

// I DONT KNOW IF THIS IS RIGHT ,
//add one by one then assign it to add[][] which is the result of additon
		 temp.a[i][j] = param.a[i][j] + a[i][j];
//

		}
	}

  return temp;
}
//display result
void CVector::show()
{
	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			cout << a[i][j] << " ";
			cout << "\n";
			
		}
	}
}



#include<iostream>
using namespace std;
#include "plus.h"

int main (int argc, char*argv[]) 
{
	
	
	int m[3][3];
	//int m[3][3];
	
	
	CVector aMatrix(m), bMatrix(m), read, c;
	read.read_data(argv[1], m);

	
	// c = array m + array n
	c = aMatrix + bMatrix;
	
	
 
	c.show(); 


  return 0;
}
				


This post has been edited by tinychip: 05 September 2009 - 01:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: operator overload to add 2D-arrays

Posted 05 September 2009 - 01:54 PM

try this test program
int main (int argc, char*argv[])
{


	//create 2 d arrays to pass to read_dat()

	int m[3][3]={{1,2,3},{1,2,3},{1,2,3}};
	int n[3][3]={{1,2,3},{1,2,3},{1,2,3}};;

	CVector x(m);
	CVector y(n);
	//read.read_data(argv[1], m, n);

// i DONT KNOW IF THIS IS RIGHT , im trying to use the overload to add 2d arrays of m and n

				CVector c;
				 c = x + y; // error here, cannot add two pointers


// OUT PUT RESULT
  for(int i =0; i<3; i++)
  {
		for(int j=0; j<3; j++)
		{
			 cout << c.a[i][j] << " "; // i DONT KNOW WHAT TO REPLACE WITH
		}
		cout << endl;
  }

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 tinychip  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 03-December 08

Re: operator overload to add 2D-arrays

Posted 05 September 2009 - 02:30 PM

DONT KNOW Why the program outputs weird number:
my infile.txt contains:
1 0 0
0 0 1
1 0 -1

1 2 3
2 3 4
2 4 3
2 4 6

my updated code:
#include <iostream>
using namespace std;

class CVector
{
  public:
	int a[3][3];
	
	
	CVector() {};
	CVector (int x[][3])
	{
		

		for(int i=0; i<3; i++)
		{
			for(int j =0; j<3; j++)
			{
				
				a[i][j] = x[i][j];

				
			}
			
		}

	};
//pass read data and store 2d arrays
  void read_data(char*filename, int a[][3]);

//overload operator +
	CVector operator + (CVector);
	CVector operator * (CVector);
//show result of additon
	void show();
//show result of multiplication
	void show2();
 };




implemention
//file: plus.cpp

#include<iostream>
#include<fstream>
using std::ifstream;
using namespace std;

#include "plus.h"


//get data
void CVector::read_data(char*filename, int a[][3])
{
	ifstream in(filename);

	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			in >> a[i][j];
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			in >> a[i][j];
			cout << a[i][j] << " ";
		}
		cout << endl;
	}

	in.close();

}

// use operator overload to add 2 d arrays
CVector CVector::operator+ (CVector param)
{
	 CVector temp;
	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			

		//add one by one then assign it to add[][] which is the result of additon
		 temp.a[i][j] = a[i][j] + param.a[i][j];
		

		}
	}

	return temp;
}

CVector CVector::operator * (CVector param2)
{
	CVector temp2;
	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
			for(int k=0; k<3;k++)
			{
				temp2.a[i][j] += a[i][k] * param2.a[k][j];
			}
		}
	}
	return temp2;
}

void CVector::show()
{
	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			cout << a[i][j] << " ";
			cout << "\n";
			
		}
	}
	
}
void CVector::show2()
{
	for(int i=0; i<3; i++)
	{
		for(int j =0; j<3; j++)
		{
			cout << a[i][j] << " ";
			cout << "\n";
			
		}
	}
}



main
#include<iostream>
using namespace std;
#include "plus.h"

int main (int argc, char*argv[]) 
{
	
	
	int m[3][3];
	int n[3][3];
	
	
	CVector aMatrix(m), bMatrix(n), read, c, d;
	read.read_data(argv[1], m);

	c.show();
	// c = array m + array n
	c = aMatrix + bMatrix;
	
	

	c.show();
	d = aMatrix * bMatrix;
	c.show2();


  return 0;
}


This post has been edited by tinychip: 05 September 2009 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#6 tinychip  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 03-December 08

Re: operator overload to add 2D-arrays

Posted 05 September 2009 - 03:43 PM

the objective of this program is get data from a file contains intergers, then make 2 2D matrix from that data.
use overload operator + to add 2 2D matrix: aMatrix[3][3] + bMatrix[3][3], and multiplication: aMatrix[3][3] * bMatrix[3][3], then out put the result .

I got to there and my program outputs weird numbers. Please HELP
Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 tinychip  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 03-December 08

Re: operator overload to add 2D-arrays

Posted 05 September 2009 - 10:02 PM

I have figured it out. Everything is working fine now. Thanks for your help horace.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1