please check out the following two code segments and suggest which one is more efficient and worthy to use...

The segments are actually constructors used to allocate a memory block for a matrix..


segment - 1 :

class matrix
{
  private :
    int rows, columns;
    int *element;

  public :
  // constructors
    matrix();
    matrix (int , int);

  // member functions
};

matrix :: matrix (int r, int c)
{
 rows = r;
 cols = c;
 element = new int [r * c];
}

segment - 2 :

class matrix
{
  private :
    int rows, columns;
    int **element;

  public :
  // constructors
    matrix();
    matrix (int , int);

  // member functions
};

matrix :: matrix (int r, int c)
{
 rows = r;
 cols = c;
 element = new int* [rows];
 int i = 0;
 while (i < rows)
 {
    *(element+i) = new int [cols];
    ++i;
 }
}

well....i think 1st one is more efficient... ;) ....and obviously more simple...
please give your views... :)

heyy sorry moderators...i actually forgot to put the code tags... :(
but thanks to ur efficient system... :)

The first one only looks more efficient, but IMO the second one is because you can reference matrix just as you would any normal 2d array matrix[i][j] = 0;

yaaa...i thought the same way...i am able to use the 2-d array style to access the elements in the 2nd style...hmm..okk..thankss.. :)
and what is this IMO??? :|

IMO -- In My Opinion

IMO -- In My Opinion

ohkkk :)
thanksss.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.