this is the code i have written so far....its in c++....i need ur help....i can't find errors...:(

class Matrix
{
private:
int rows;
int cols;
float *pData;
public:
int getrows(){return rows;}
int getcols(){return cols;}
void setrows(){rows=r;}
void setcols(){cols=c;}
private:
void doCreate();
void doAdd(Matrix m);
void doSub(Matrix m);
void doTranspose();
void doDelet();
void doDisplay();
void doInsert();
public:
void Create(){doCreate();}
void Add(Matrix m){doAdd(Matrix m);}
void Sub(Matrix m){doSub(Matrix m);}
void Transpose(){doTranspose();}
void Delet(){doDelet();}
void Display(){doDisplay();}
void Insert(){doInsert();}
};

void Matrix:: DoCreate() //////create function///////
{
pData = new float *[rows];
for(int i=0;i<rows;i++)
pData=new float[cols];
}

void main()
{
int x;
Matrix m;
m.setrows(3);
m.setcols(4);
m.Create();
m.Insert();
m.Display();
m.Transpose();
}

i dnno how to insert in without using an array...:(

Edited 6 Years Ago by naz arif: n/a

  • Please use the (CODE) button to wrap your code (You may either press it first then paste between the (snip) and (snip) tags, or paste first then highlight and press the CODE-button. This gives code coloring, line numbers, and maintains indentation: All good things.
  • It is generally considered good to use CapitalCamelCase or Capital_case for the names of classes, and keep the names of methods and variables lowerCamelCase or lower_case.
  • To deal with your pData you may take it back apart in a manner similar to the way you created it (which, by the way, is more work than you need to do, since you could have called new float[rows*cols] instead). (Doing it your way enforces 'row major' layout, which may not always be to your advantage.) For instance to deal with the item at index [n][m] you get the nth row and look at its mth item

Edited 3 Years Ago by mike_2000_17: Fixed formatting

yes....i want to insert value in the object but with out using array....i dont understand how to insert a value without using an array...

  • Please use the (CODE) button to wrap your code (You may either press it first then paste between the (snip) and (snip) tags, or paste first then highlight and press the CODE-button. This gives code coloring, line numbers, and maintains indentation: All good things.
  • It is generally considered good to use CapitalCamelCase or Capital_case for the names of classes, and keep the names of methods and variables lowerCamelCase or lower_case.
  • To deal with your pData you may take it back apart in a manner similar to the way you created it (which, by the way, is more work than you need to do, since you could have called new float[rows*cols] instead). (Doing it your way enforces 'row major' layout, which may not always be to your advantage.) For instance to deal with the item at index [n][m] you get the nth row and look at its mth item

thank u so much 4 ur help....i will do what u said....actually m new so i dnno how to use the code button....:)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I made some changes....bt in still has many errors.......:(...i dnno how to remove errors........:(

class Matrix
{
private:    
 int rows;
 int cols;
 float *pData;
public:
 int getrows(){return rows;}
 int getcols(){return cols;}
 void setrows(){rows=r;}  
 void setcols(){cols=c;}
private:
 void doCreate();
 void doAdd(Matrix m);
 void doSub(Matrix m);
 void doTranspose();
 void doDisplay();
 void doInsert();
public:
 void Create(){doCreate();}
 void Add(Matrix m){doAdd(Matrix m);}
 void Sub(Matrix m){doSub(Matrix m);}
 void Transpose(){doTranspose();}
 void Display(){doDisplay();}
 void Insert(){doInsert();}
};

void Matrix::DoCreate()               //////create function///////
{
 pData = new float *[rows];
 for(int i=0;i<rows;i++)
 pData[i]=new float[cols];
} 

void Matrix::doAdd(Matrix m)       ///////Addition function///////
{
 Matrix m1;
 Create(m1);     
    if((m1.rows==m.rows)&&(m1.cols==m.cols))
     {
        for(int i=0;i<rows;i++)
        {
          for(int j=0;j<cols;j++)
             {
              m1.pData[i][j]=m1.pData[i][j]+m.pData[i][j];
             }
        }
        Display(m1);
     }
        else
        cout<<"Addition not possible" << endl;
}

void Matrix::doSub(Matrix m)      //////Subtraction function/////
{
Matrix m1;
 Create(m1);     
    if((m1.rows==m.rows)&&(m1.cols==m.cols))
     {
        for(int i=0;i<rows;i++)
        {
          for(int j=0;j<cols;j++)
             {
              m1.pData[i][j]=m1.pData[i][j]-m.pData[i][j];
             }
        }
        Display(m1);
     }
        else
        cout<<"Subtration not possible" << endl;
}

void Matrix::doTranspose()       //////Transpose function///////
{
  for(int i=0;i<cols;i++)
     {
      for(int j=0;j<rows;j++)
       {
         cout<<pData[i][j] << endl;
       }
     }  
}

void Matrix::doDisplay()       ///////Display function////////
{
  for(int i=0;i<rows;i++)
    {
    for(int j=0;j<cols;j++)
     {
      cout<<pData[i][j] << endl;
     }
    }
}

void Matrix::doInsert()       /////////Insert function///////
{
 Create(m);   
 cout << "Enter values for matrix:" << endl;
   for(int i=0; i<m.rows; i++)
    {
    for(int j=0;j<m.col; j++) 
      {
       cin>>m.pData[i][j];
      }
    }
}


void main()
{
 {
char select;
Matrix m,m1;
m.setrows(3);
m.setcols(4);
m1.setrows(3);
m1.setcols(4);
  {
    clrscr(); 
        option:
    cout<<"Insert...............[a]\n";
    cout<<"Add..................[b]\n";
    cout<<"subtract.............[c]\n";
    cout<<"Display..............[d]\n";
    cout<<"Transpose............[e]\n";
    cout<<"Exit.................[f]\n";
    cout<<"\n\nEnter Option = ";
    cin>>select;

    switch(select)
    {
    case 'a':                /////insert/////
        
      m.Insert();
      break;
      
    case 'b':              /////addition/////                                      
     
     m.Add(m1);
     break;
    
     case 'c':            /////subtration/////                                      
     
     m.Sub(m1);
     break;

    case 'd':             /////display/////                                   
   
      m.Display();
      break;
   
    case 'e':           /////transpose/////                                 
   
     m.Transpose();
     break;
     
    case 'f':          /////exit/////                                 
      
     exit (0);
     break;
    }
      getch ();

}
  • First thing to jump out at me: void main() is not legal C or C++. The main function must return int. It is in the standard.
  • Line 111 starts a needless block
  • Line 28 (and several others similar) the comment //////create function/////// is wasted effort. If you name your functions well, there is no need to explain the name (I would use doSubtract rather than doSub, for instance)
  • If you have compilation errors, you can usually figure out what the compiler is trying to tell you:
    • Look at only the first error: You know the compiler is confused after that
    • Look for the place in your code that the error mentions (sometimes it is not the first thing the compiler tells you, because sometimes your mistake caused a built in or library function to have trouble)
    • Actually think about what the compiler is telling you. Most problems are syntax errors: Unclosed blocks or strings or the like; or mistakes mixing types (passing a string where an int or float is expected is very common)
    • If you still need help, post your code exactly as it was when it failed to compile and the exact output from the compiler (up through the end of the first problem) ... and be sure you mention your operating system and the name and version of the compiler.
  • First thing to jump out at me: void main() is not legal C or C++. The main function must return int. It is in the standard.
  • Line 111 starts a needless block
  • Line 28 (and several others similar) the comment //////create function/////// is wasted effort. If you name your functions well, there is no need to explain the name (I would use doSubtract rather than doSub, for instance)
  • If you have compilation errors, you can usually figure out what the compiler is trying to tell you:
    • Look at only the first error: You know the compiler is confused after that
    • Look for the place in your code that the error mentions (sometimes it is not the first thing the compiler tells you, because sometimes your mistake caused a built in or library function to have trouble)
    • Actually think about what the compiler is telling you. Most problems are syntax errors: Unclosed blocks or strings or the like; or mistakes mixing types (passing a string where an int or float is expected is very common)
    • If you still need help, post your code exactly as it was when it failed to compile and the exact output from the compiler (up through the end of the first problem) ... and be sure you mention your operating system and the name and version of the compiler.

wowwww.....thanx alot........u solved my problem......thank u so much......actually m new to programming so i had alot of problems.....bt u made it easy......thank u:)

This question has already been answered. Start a new discussion instead.