Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > General Computer Support > Help Needed with this code...its URGENT!!

Reply
Thread Tools

Help Needed with this code...its URGENT!!

 
 
ankita.iitk ankita.iitk is offline
Junior Member
Join Date: Jul 2008
Posts: 1
 
      07-07-2008
hi..
i am new to c++ programming..and i am supposed to create a matrix class for a project that i m doing...i created one which handles the sparse matrix in compressed row format..and m trying to test it..but i m getting errors during the runtime..
A:all the non zero elements
B: position in A where a new row begins
C:column no. corresponding to each non zero element in A
S:all the non zero elments without repeating them.

this is the code:
#include<iostream>
#include <vector>
using namespace std;
class Matrix
{
public:
Matrix(int n,int r,int c);
~Matrix();
double getElement(int r,int c);
int nzinC(int c,int n);
int nzinR(int r);
int count();
void setA(double i);
void setB(int i);
void setC(int i);
void setS();
double getElA(int i);
double getElS(int i);
int getElB(int i);
int getElC(int i);
int getcount();
private:
vector<double> A;
vector<int> B;
vector<int> C;
vector<double> S;
};
Matrix::Matrix(int n,int r,int c)
{
A.reserve(n);
B.reserve(r+1);
C.reserve(n);
S.reserve(getcount());
}

Matrix::~Matrix()
{
}

*/ methods in the class....u may ignore this part

void Matrix::setA(double i)
{
A.push_back(i);
}
void Matrix::setB(int i)
{
B.push_back(i);
}
void Matrix::setC(int i)
{
C.push_back(i);
}

double Matrix::getElement(int r,int c)
{
int flag=0;
for(int i=0;i<A.at(B.at((r-1)-1));i++)
{
if(C.at(i)==c)
{
flag=i;
break;
}
else flag=-1;
}
if(flag=-1) return 0;
else return A.at(flag);
}

int Matrix::nzinC(int c,int n)
{
int flag=0;
for(int i=0;i<n;i++)
{
if(C.at(i)==c)
flag=flag+1;
}
return flag;
}
int Matrix::nzinR(int r)
{
return (B.at(r+1)-B.at(r));
}

double Matrix::getElA(int i)
{
return A.at(i);
}
int Matrix::getElB(int i)
{
return B.at(i);
}
int Matrix::getElC(int i)
{
return C.at(i);
}
int Matrix::getcount()
{
int cnt=0;int flag=-1;
for (int i=0;i< A.size();i++)
{
for(int j=0;j<cnt;j++)
{
if (A.at(i)==A.at(j))
{ flag=0;
break;
}
}
if (flag==-1)
cnt++;
}
return cnt;
}
void Matrix::setS()
{
int cnt=0;int flag=-1;
for (int i=0;i<A.size();i++)
{
for(int j=0;j<cnt;j++)
{
if (A.at(i)==A.at(j))
{ flag=0;
break;
}
}
if (flag==-1)
{
S.push_back(A.at(i));
cnt++;
}
}
}
double Matrix::getElS(int i)
{
return S.at(i);
}
*/
int main()
{
int row,col,i,j,nz;
double a;
cout<<"Please enter the number of row";
cin>>row;
cout<<"Please enter the number of columns";
cin>>col;
cout<<"Please enter the number of non zero entries";
cin>>nz;
vector<double>::iterator Iter1;// creates an iterator necessary to traverse vector
vector<int>::iterator Iter2;// creates an iterator necessary to traverse vector
cout<<"Please enter the non zero entries in left to right top to bottom fashion";
Matrix M1(nz,row,col);
for (i=0;i<nz;i++ )
{
cout<<"Entry no."<<i ;
cin>>a;
M1.setA(a);
}
cout<<"Please enter position(index starting from 0,left to right top to bottom)where the new column begins";
for (i =0;i<row+1;i++)
{
cout <<"Entry no. "<<i ;
cin>>j;
M1.setB(j);
}
cout<<"Please enter respective column index of the non zero entries left to right top to bottom manner ";
for (i=0;i<nz;i++ )
{
cout<<"Entry no."<<i ;
cin>>j;
M1.setC(j);
}
j=M1.nzinC(1,nz);
cout<<"No. of non zero entries in 1st column="<<j;
}

i believe there is a problem with the way i have defined the class or the constructor...
plz help me figure out the error....and if the way in which i have defined the vectors inside the class is inappropriate..kindly let me know...thnx in advance...
 

Last edited by ankita.iitk; 07-08-2008 at 10:04 AM.. Reason: make it more understandable to ppl..
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help Help Pentax S5i Help needed (Please) The Martian Digital Photography 14 06-20-2008 07:56 AM
Help Help Help Please Pentax S5i info needed The Martian Digital Photography 2 05-20-2008 07:00 AM
Help needed on this 857W config. Repost to be clearer what the problemsare and the help needed sparticle Cisco 3 08-30-2007 07:47 PM
Ideas needed & help needed! Ryan Macy Ruby 2 07-19-2006 08:04 PM
Microsoft small business server 2003 - help needed to understand what is needed to use it Dima Computer Support 5 10-20-2004 08:27 PM



Advertisments