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

Discussion in 'General Computer Support' started by ankita.iitk, Jul 7, 2008.

  1. ankita.iitk

    ankita.iitk

    Joined:
    Jul 7, 2008
    Messages:
    1
    Help Needed with this C++ code...its URGENT!!

    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: Jul 8, 2008
    ankita.iitk, Jul 7, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FINNY
    Replies:
    1
    Views:
    435
    Pennywise@DerryMaine.Gov
    Nov 18, 2006
  2. Security Advisory

    !!URGENT!! Tor Vulnerability Discovered !!URGENT!!

    Security Advisory, Aug 6, 2007, in forum: Computer Security
    Replies:
    1
    Views:
    963
    http://tinyurl.com/23k3dt@$NIFF-deeply.ahh
    Aug 11, 2007
  3. ch_mshahid

    Please Help Me Sove This C++ Its urgent

    ch_mshahid, Jul 7, 2008, in forum: Software
    Replies:
    0
    Views:
    673
    ch_mshahid
    Jul 7, 2008
  4. ch_mshahid

    Please Help Its urgent C++ Code required

    ch_mshahid, Jul 7, 2008, in forum: General Computer Support
    Replies:
    1
    Views:
    762
    jcas1411
    Jul 31, 2008
  5. pooja
    Replies:
    0
    Views:
    1,183
    pooja
    Mar 3, 2009
Loading...

Share This Page