Click here to Skip to main content
15,887,485 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Drawing 32 bit bitmaps - Solution [modified] Pin
softwaremonkey3-Jan-10 20:42
softwaremonkey3-Jan-10 20:42 
GeneralRe: Drawing 32 bit bitmaps - Solution Pin
Naveen4-Jan-10 16:43
Naveen4-Jan-10 16:43 
GeneralRe: Drawing 32 bit bitmaps - Solution Pin
softwaremonkey4-Jan-10 20:02
softwaremonkey4-Jan-10 20:02 
QuestionHelp Help Help Pin
richard cyper3-Jan-10 4:29
richard cyper3-Jan-10 4:29 
AnswerRe: Help Help Help Pin
Luc Pattyn3-Jan-10 4:38
sitebuilderLuc Pattyn3-Jan-10 4:38 
AnswerRe: Help Help Help Pin
Jimmanuel3-Jan-10 4:50
Jimmanuel3-Jan-10 4:50 
AnswerRe: Help Help Help Pin
LunaticFringe3-Jan-10 4:56
LunaticFringe3-Jan-10 4:56 
AnswerRe: Help Help Help [fixed - thanks Nelek] Pin
CPallini3-Jan-10 7:22
mveCPallini3-Jan-10 7:22 
I made some modifications to you code.
Note that your multiplication operation is out of standard (I haven't touched it and the division one): matrix multiplication is usually defined this way:
m[i,j] = a[i,0] * b[0,j] +  a[i,1] * b[1,j] + ... +  a[i,N] * b[N,j]

Beware, returning, as you did, element 0 when the requested index is out of bounds, it is not a good practice.



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


class matrix
{
public:
	struct Exception
	{
		string msg;
		Exception(string emsg)
		{
			this->msg=msg;
		}
	};
	matrix();
	matrix(int m,int n);
        ~matrix(){ delete [] data; }
	int getRows();
	int getCols();

	double& operator()(int, int);

	friend ostream& operator<<(ostream& os, matrix& m);
	matrix operator + (matrix&);
	matrix operator * (matrix&);
	matrix operator - (matrix&);
	matrix operator / (matrix&);

private:
	void init(int, int);
	int nrows,ncols;
	double *data;
};

matrix::matrix(){
	init(1,1);
}

matrix::matrix(int m, int n){
	init(m,n);
}

void matrix::init(int m, int n){
	nrows=m;
	ncols=n;
	data= new double[m*n];
	for(int i=0; i<m*n; i++)
		data[i]=.0;
}

int matrix::getRows() { return nrows;}

int matrix::getCols() { return ncols;}

double& matrix::operator ()(int r, int c){
	if (r <0 || r>= nrows || c<0 || c>= ncols) 
		throw matrix::Exception("out of bounds");
	return data[r*ncols+c];
}

ostream& operator<<(ostream& os, matrix &m)
{
	for(int r=0; r<m.nrows; r++)
	{
		for(int c=0; c < m.ncols; c++)
			os<<m(r,c) << " ";
		os<<endl;
	}
	return os;
}

matrix matrix::operator+(matrix& a)
{
	if ( nrows != a.nrows || ncols!= a.ncols )
		throw Exception("invalid operands");

	matrix sum(nrows, ncols);
	for (int r=0; r<nrows; r++)
		for(int c=0; c<ncols; c++)
		{
			int i = r * ncols + c;
			sum.data[i] = data[i] + a.data[i];
		}
	return sum;
}

matrix matrix::operator*(matrix& a)
{
	matrix product(nrows, ncols);
	for (int r=0; r<nrows; r++)
		for(int c=0; c<ncols; c++)
		{
			product(r,c) = (r,c) * a(r,c);
		}
	return product;
}

matrix matrix::operator-(matrix& a)
{
	
	if ( nrows != a.nrows || ncols!= a.ncols )
	throw Exception("invalid operands");

	matrix difference(nrows, ncols);
	for (int r=0; r<nrows; r++)
		for(int c=0; c<ncols; c++)
		{
			int i = r * ncols + c;
			difference.data[i] = data[i] - a.data[i];
		}
	return difference;
}


matrix matrix::operator/(matrix& a)
{
	matrix divide(nrows, ncols);
	for (int r=0; r<nrows; r++)
		for(int c=0; c<ncols; c++)
			divide(r,c) = (r,c) / a(r,c);
	return divide;
}
int main()
{
	matrix a(2,2);
	a(1,1)=5.0;
	a(0,0)=6.0;
	a(0,1)=7.0;
	a(1,0)=8.0;
	cout<<a(0,0)<<endl;
	cout<<a(1,1)<<endl;
	cout<<a<<endl;

	matrix b(2,2);
	b(0,0) = 5.0;
	b(0,1) = 5.0;
	b(1,0) = 5.0;
	b(1,1) = 5.0;
	cout<<b<<endl;

	matrix c,d,e,f;

	c=a+b;
	d=a*b;
	e=a-b;
	f=a/b;

	cout<<c<<endl;
	cout <<"\n\n";
	cout<<d<<endl;

	cout<<e<<endl;
	cout <<"\n\n";
	cout<<f<<endl;


	system("pause");
	return 0;
}


Smile | :)

If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.

This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke

[My articles]

modified on Sunday, January 3, 2010 2:18 PM

GeneralRe: Help Help Help Pin
Nelek3-Jan-10 7:56
protectorNelek3-Jan-10 7:56 
GeneralRe: Help Help Help Pin
CPallini3-Jan-10 8:23
mveCPallini3-Jan-10 8:23 
JokeRe: Help Help Help Pin
Nelek3-Jan-10 11:24
protectorNelek3-Jan-10 11:24 
GeneralRe: Help Help Help Pin
CPallini3-Jan-10 11:54
mveCPallini3-Jan-10 11:54 
GeneralRe: Help Help Help [fixed - thanks Nelek] Pin
ThatsAlok3-Jan-10 22:12
ThatsAlok3-Jan-10 22:12 
GeneralOT Pin
CPallini4-Jan-10 0:39
mveCPallini4-Jan-10 0:39 
QuestionStructures Printing Pin
Razanust3-Jan-10 1:47
Razanust3-Jan-10 1:47 
AnswerRe: Structures Printing Pin
Richard MacCutchan3-Jan-10 3:07
mveRichard MacCutchan3-Jan-10 3:07 
AnswerRe: Structures Printing Pin
«_Superman_»3-Jan-10 6:40
professional«_Superman_»3-Jan-10 6:40 
AnswerRe: Structures Printing Pin
kanduripavan5-Jan-10 5:04
kanduripavan5-Jan-10 5:04 
QuestionHelp!Can anyone mail a copy of 'Photoshop File Format Document'? Pin
WrestSoft3-Jan-10 1:32
WrestSoft3-Jan-10 1:32 
AnswerRe: Help!Can anyone mail a copy of 'Photoshop File Format Document'? Pin
WrestSoft6-Jan-10 3:25
WrestSoft6-Jan-10 3:25 
QuestionRtlCopyMemory() question Pin
Mattzimmerer2-Jan-10 19:25
Mattzimmerer2-Jan-10 19:25 
AnswerRe: RtlCopyMemory() question Pin
«_Superman_»2-Jan-10 19:35
professional«_Superman_»2-Jan-10 19:35 
QuestionAccess CPU register Pin
ludi04232-Jan-10 18:51
ludi04232-Jan-10 18:51 
AnswerRe: Access CPU register Pin
«_Superman_»2-Jan-10 19:28
professional«_Superman_»2-Jan-10 19:28 
GeneralRe: Access CPU register Pin
ludi04232-Jan-10 19:46
ludi04232-Jan-10 19:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.