C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 19th, 2007, 05:33 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
Help with a matrix class program

Hey all. I'm working on a matrix program. The matrix is a class, and I have the constructor set to initialize the matrix to all zero values.

My question is; I ask the user for the size of their matrix, how do I set the matrix to that size?

Below is the code....it's a bit long.

Code:

#include <iostream>
#include <ctype.h>
using namespace std;
int innerprod(int s[][10],int t[][10],int r,int c,int size)
{
	int temp=0,i;
	for (i=0;i<=size;++i)
	{
		temp=temp+s[r][i]*t[i][c];
	}
	return temp;
}
class Matrix
{
public:
	~Matrix(){};
	Matrix();
	void readm();
	void addm(Matrix);
	void multm(Matrix);
	void print();
	void setMatrix(int r,int t);
private:
	int a[10][10];
	int size;
};
Matrix::Matrix()
{
	cout<<"Constructor"<<endl;
	int i=0, j=0;
	for (i=0;i<2;++i)
		for (j=0;j<2;++j)
		{
			a[i][j]=0;
		}
}
void Matrix::setMatrix(int r,int t)
{
	a[2][2]=a[r][t];							
}
void Matrix::readm()
{
	int i=0,j=0;
	for (i=0;i<2;++i)
		for (j=0;j<2;++j)
		{
			cin>>a[i][j];
		}
}
void Matrix::addm(Matrix B)
{
	int i=0,j=0;
	for (i=0;i<2;++i)
		for (j=0;j<2;++j)
		{
			a[i][j]=a[i][j]+B.a[i][j];
		}
}
void Matrix::multm(Matrix B)
{
	int i=0,j=0,temp[10][10],n=2;
	for(i=0;i<2;++i)
		for (j=0;j<2;++j)
		{
			temp[i][j]=innerprod(a,B.a,i,j,n);
		}
	for (i=0;i<2;++i)
		for (j=0;j<2;++j)
		{
			a[i][j]=temp[i][j];
		}
}
void Matrix::print()
{
	int i=0,j=0;
	for (i=0;i<2;++i)
	{
		cout<<endl<<endl;
		for (j=0;j<2;++j)
		{
			cout<<"    "<<a[i][j];
		}
	}
	cout<<endl;
}
void main()
{
	int r=0, t=0;
	char choice;
	Matrix x,y;
	cout<<"   Welcome to Matrix Manipulations."<<endl;
	x.print();
	cout<<"   What size matrix do you have??."<<endl;
	cin>>r>>t;
	x.setMatrix(r,t);
	cout<<"   You may add or multiply a matrix."<<endl;
	cout<<"   Enter choice: (a)dd or (m)multiply.  "<<endl;
	cin>>choice;
	choice>>tolower(choice);
	cout<<"   Enter a square matrix from 2x2 to 10x10 in row order:"<<endl;
	x.readm();
	switch(choice)
	{
	case 'a':cout<<"Enter a square matrix from size 2x2 to 10x10 in row order:"<<endl;
		y.readm();
		x.print();
		cout<<endl<<"    +";
		y.print();
		cout<<endl<<"    =";
		x.addm(y);
		x.print();
		break;
	case 'm':cout<<"Enter a square matrix from size 2x2 to 10x10 in row order:"<<endl;
		y.readm();
		x.print();
		x.multm(y);
		cout<<endl<<"    *";
		y.print();
		cout<<endl<<"    =";
		x.print();
		break;
	default: cout<<"    INVALID CHOICE!";
		break;
	};
}


Thanks a lot,

J

Reply With Quote
  #2  
Old June 20th, 2007, 09:31 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
do you have to use an array? i would just use vectors and the resize() function.

Reply With Quote
  #3  
Old June 20th, 2007, 09:48 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
yeah I have to use a matrix.


I figured out the problem...now I'm working on another problem. There is a problem in my multiplication somewhere.


THanks,

j

Reply With Quote
  #4  
Old June 20th, 2007, 10:01 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
well, you can create a matrix using vectors

you declare it like this:

vector< vector<int> > a;

and you can resize it like this:

Code:
setMatrix(int x, int y)
{
   a.resize( x );
   for ( int i = 0; i < x; ++i )
   {
     a[i].resize( y );
   }
}


and you can set values just like you would a normal 2d array

Last edited by Bobidybob : June 21st, 2007 at 04:45 PM.

Reply With Quote
  #5  
Old June 20th, 2007, 10:02 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
ah, kk, sorry, im a bit slow on the posts

Reply With Quote
  #6  
Old June 20th, 2007, 10:11 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
Quote:
Originally Posted by Bobidybob
ah, kk, sorry, im a bit slow on the posts


No problem.

Here is the code I'm working with now:

Code:

#include <iostream>
#include <ctype.h>
using namespace std;
int innerprod(int s[][10],int t[][10],int r,int c,int size)
{
	int temp=0,i;
	for (i=0;i<=size;++i)
	{
		temp=temp+s[r][i]*t[i][c];
	}
	return temp;
}
class Matrix
{
public:
	~Matrix(){};
	Matrix();
	void readm(int r, int t);
	void addm(Matrix,int r,int t);
	void multm(Matrix,int r, int t);
	void print(int r,int t);
	Matrix(int r,int t);
private:
	int a[10][10];
	int size;
};
Matrix::Matrix()
{
	cout<<"Constructor"<<endl;
	int i=0, j=0;
	for (i=0;i<2;++i)
		for (j=0;j<2;++j)
		{
			a[i][j]=0;
		}
}
Matrix::Matrix(int r,int t)
{
	int i=0,j=0;
	cout<<"   Initializing new Matrix....."<<endl;
	for (i=0;i<r;++i)
		for (j=0;j<t;++j)
		{
			a[r][t]=0;
		}
	for (i=0;i<r;++i)
	{
		cout<<endl<<endl;
		for (j=0;j<t;++j)
			cout<<"   "<<a[r][t];
	}
	cout<<endl;
	
}
void Matrix::readm(int r, int t)
{
	int i=0,j=0;
	for (i=0;i<r;++i)
		for (j=0;j<t;++j)
		{
			cin>>a[i][j];
		}
}
void Matrix::addm(Matrix B,int r, int t)
{
	int i=0,j=0;
	for (i=0;i<r;++i)
		for (j=0;j<t;++j)
		{
			a[i][j]=a[i][j]+B.a[i][j];
		}
}
void Matrix::multm(Matrix B,int r, int t)
{
	int i=0,j=0,temp[10][10],n=r;
	for(i=0;i<r;++i)
		for (j=0;j<t;++j)
		{
			temp[i][j]=innerprod(a,B.a,r,t,n);
		}
	for (i=0;i<r;++i)
		for (j=0;j<t;++j)
		{
			a[i][j]=temp[i][j];
		}
}
void Matrix::print(int r,int t)
{
	int i=0,j=0;
	for (i=0;i<r;++i)
	{
		cout<<endl<<endl;
		for (j=0;j<t;++j)
		{
			cout<<"    "<<a[i][j];
		}
	}
	cout<<endl;
}
void main()
{
	int r=0, t=0;
	char choice;
	Matrix x,y;
	cout<<"   Welcome to Matrix Manipulations."<<endl;
	x.print(r,t);
	cout<<"   What size matrix do you have??."<<endl;
	cin>>r>>t;
	Matrix(r,t);
	cout<<"   You may add or multiply a matrix."<<endl;
	cout<<"   Enter choice: (a)dd or (m)multiply.  "<<endl;
	cin>>choice;
	choice>>tolower(choice);
	cout<<"   Enter a square matrix from 2x2 to 10x10 in row order:"<<endl;
	x.readm(r,t);
	switch(choice)
	{
	case 'a':cout<<"Enter a square matrix from size 2x2 to 10x10 in row order:"<<endl;
		y.readm(r,t);
		x.print(r,t);
		cout<<endl<<"    +";
		y.print(r,t);
		cout<<endl<<"    =";
		x.addm(y,r,t);
		x.print(r,t);
		break;
	case 'm':cout<<"Enter a square matrix from size 2x2 to 10x10 in row order:"<<endl;
		y.readm(r,t);
		x.print(r,t);
		x.multm(y,r,t);
		cout<<endl<<"    *";
		y.print(r,t);
		cout<<endl<<"    =";
		x.print(r,t);
		break;
	default: cout<<"    INVALID CHOICE!";
		break;
	};
}


I get a "garbage" answer when I chose to multiply 3x3 matrices when entering 1 for all values.

I think the error is in my innerprod function but I haven't figured out what it is yet.

Thanks,
J

Reply With Quote
  #7  
Old June 20th, 2007, 10:27 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
in the loop

Code:
	for (i=0;i<=size;++i)
	{
		temp=temp+s[r][i]*t[i][c];
	}


should it be this instead?

for(i=0;i<size;++i)

Last edited by Bobidybob : June 20th, 2007 at 10:45 AM.

Reply With Quote
  #8  
Old June 20th, 2007, 10:32 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
Quote:
Originally Posted by Bobidybob
in the loop

Code:
	for (i=0;i<=size;++i)
	{
		temp=temp+s[r][i]*t[i][c];
	}


should it be a post decrement instead?

for(i=0;i<size;i++)


I did forget to change that loop, yes it should just be =. That gives me a different garbage number though. Although I would have had to change that loop eventually.

J

Reply With Quote
  #9  
Old June 20th, 2007, 11:05 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
im doing my best to understand this, but in the multm() function

Code:
	for(i=0;i<r;++i)
		for (j=0;j<t;++j)
		{
			temp[i][j]=innerprod(a,B.a,r,t,n);
		}


since the values passed to innerprod arent changing, i think its gonna return the same value for every value in temp. it's been a while since i've messed with matrices, but is this what you intended?

Reply With Quote
  #10  
Old June 20th, 2007, 11:09 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
Quote:
Originally Posted by Bobidybob
im doing my best to understand this, but in the multm() function

Code:
	for(i=0;i<r;++i)
		for (j=0;j<t;++j)
		{
			temp[i][j]=innerprod(a,B.a,r,t,n);
		}


since the values passed to innerprod arent changing, i think its gonna return the same value for every value in temp. it's been a while since i've messed with matrices, but is this what you intended?


The innerprod function calculates the products within the matrix, the multm function moves "across" the rows and columns by incrementing i and j.

Is that what you were asking??

Reply With Quote
  #11  
Old June 20th, 2007, 11:11 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
sort of...

are the garbage values you're getting all the same number?

EDIT: well, now that i think about it, they should be :P

Last edited by Bobidybob : June 20th, 2007 at 11:16 AM.

Reply With Quote
  #12  
Old June 20th, 2007, 11:18 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
Quote:
Originally Posted by Bobidybob
sort of...

are the garbage values you're getting all the same number?

EDIT: well, now that i think about it, they should be :P


Yes the ouput is the same. It's performing some type of math, because if I change all of the inputs in the matrices from 1 to 2, the output "garbage" number changes.

Reply With Quote
  #13  
Old June 20th, 2007, 11:20 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
change the values of the matrix so that they are all different

i.e.

1 2 1
3 7 2
3 2 6

then look and see if the resulting matrix has the same number for each index

i.e.

8 8 8
8 8 8
8 8 8

or some other incorrect number

Reply With Quote
  #14  
Old June 20th, 2007, 11:24 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
When I enter:
4 3
2 1

and

4 5
6 7

I receive:

1374389536 1374389536
1374389536 1374389536

Reply With Quote
  #15  
Old June 20th, 2007, 11:31 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
try this:

Code:
int innerprod(int x,int y,int size)
{
	int temp=0,i;
	for (i=0;i<size;++i)
	{
		temp=temp+x*y;
	}
	return temp;
}



Code:
void Matrix::multm(Matrix B,int r, int t)
{
	int i=0,j=0,temp[10][10],n=r;
	for(i=0;i<r;++i)
		for (j=0;j<t;++j)
		{
			temp[i][j]=innerprod(a[i][j],B.a[j][i],n);
		}
	for (i=0;i<r;++i)
		for (j=0;j<t;++j)
		{
			a[i][j]=temp[i][j];
		}
}


EDIT: edited to show only what i changed
EDIT2: yeah, still some bugs, but less of garbage :P

Last edited by Bobidybob : June 20th, 2007 at 11:41 AM.

Reply With Quote
  #16  
Old June 20th, 2007, 11:42 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
That doesn't work either. It doesn't give a huge number, although the numbers are still wrong. I've gotta take a break from this program for a few. Been staring at this thing for over 2 hours.


With your changes when I enter:

1 2
3 4

*

4 5
6 7

I get:

8 24
30 56

J

Reply With Quote
  #17  
Old June 20th, 2007, 12:15 PM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
geez i hate matrices. sorry, im prolly the worst guy to ask about this

Reply With Quote
  #18  
Old June 20th, 2007, 01:28 PM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
Quote:
Originally Posted by Bobidybob
geez i hate matrices. sorry, im prolly the worst guy to ask about this


It's cool. I appreciate your help,and time of looking at the problem.

Thanks a lot,
J

Reply With Quote
  #19  
Old June 20th, 2007, 01:59 PM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
HAHAHAHAHA Ugliest code EVER! but it works!

Code:
void Matrix::multm(Matrix B,int r, int t)
{
	int z, w=0, k=0;
	int i=0,j=0,temp=0,n=r, temp2[10][10];
     for(k=0;k<r;++k)
     {
	for(w=0;w<r;++w)
	{
		z=0;
		temp=0;
			for(int l=0;l<n;++l)
			{
				temp=temp+a[k][j+z]*B.a[j+z][w];
				z++;
			}
			temp2[k][w]=temp;
	}
     }
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			a[i][j]=temp2[i][j];
		}
	}
}


edit: yeah, this is all the same function, just forget about that helper function :P

Last edited by Bobidybob : June 20th, 2007 at 02:03 PM.

Reply With Quote
  #20  
Old June 21st, 2007, 11:46 AM
Hypnotik Hypnotik is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 128 Hypnotik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 10 h 30 m 28 sec
Reputation Power: 12
Problem fixed.

I was passing the incorrect value to the innerprod function. I was passing the size of the function (r,t) instead of the actual values to the function (i,j).


Thanks for everyones help and time, I appreciate it.

J

Reply With Quote
  #21  
Old June 22nd, 2007, 07:56 AM
nosale nosale is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 110 nosale User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 23 m 10 sec
Reputation Power: 12
Hypnotik,

Glad you got help! Hope to god you don't have to do LU-factorization or eigenvalue decomposition (inversion) If you do... well... drop me a line, I'd be glad to help.

EZ-E

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with a set function


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap