


Dev Articles Community Forums
> Programming
> C/C++ Help

Help with a set function
Discuss Help with a set function in the C/C++ Help forum on Dev Articles. Help with a set function C/C++ Help forum discussing building and maintaining applications in C/C++. Find out why these languages are the foundation on which other languages are built.






Dev Articles Community Forums Sponsor:


June 19th, 2007, 05:33 PM

Contributing User


Join Date: Jun 2007
Posts: 128
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

June 20th, 2007, 09:31 AM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


do you have to use an array? i would just use vectors and the resize() function.

June 20th, 2007, 09:48 AM

Contributing User


Join Date: Jun 2007
Posts: 128
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

June 20th, 2007, 10:01 AM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


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.

June 20th, 2007, 10:02 AM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


ah, kk, sorry, im a bit slow on the posts

June 20th, 2007, 10:11 AM

Contributing User


Join Date: Jun 2007
Posts: 128
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

June 20th, 2007, 10:27 AM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


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.

June 20th, 2007, 10:32 AM

Contributing User


Join Date: Jun 2007
Posts: 128
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

June 20th, 2007, 11:05 AM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


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?

June 20th, 2007, 11:09 AM

Contributing User


Join Date: Jun 2007
Posts: 128
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??

June 20th, 2007, 11:11 AM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


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.

June 20th, 2007, 11:18 AM

Contributing User


Join Date: Jun 2007
Posts: 128
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.

June 20th, 2007, 11:20 AM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


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

June 20th, 2007, 11:24 AM

Contributing User


Join Date: Jun 2007
Posts: 128
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

June 20th, 2007, 11:31 AM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


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.

June 20th, 2007, 11:42 AM

Contributing User


Join Date: Jun 2007
Posts: 128
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

June 20th, 2007, 12:15 PM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


geez i hate matrices. sorry, im prolly the worst guy to ask about this

June 20th, 2007, 01:28 PM

Contributing User


Join Date: Jun 2007
Posts: 128
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

June 20th, 2007, 01:59 PM


Contributing Abuser


Join Date: Apr 2007
Location: Starkville, MS
Posts: 336
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12


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.

June 21st, 2007, 11:46 AM

Contributing User


Join Date: Jun 2007
Posts: 128
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

June 22nd, 2007, 07:56 AM

Contributing User


Join Date: May 2007
Posts: 110
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 LUfactorization or eigenvalue decomposition (inversion) If you do... well... drop me a line, I'd be glad to help.
EZE

Developer Shed Advertisers and Affiliates
Thread Tools 
Search this Thread 


Display Modes 
Rate This Thread 
Linear Mode


Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off




