mercredi 15 juin 2016

C++ Delete and Compact even rows in 2D dynamic array

i need some help

This program must do:

•Delete all the even rows

•Compact the remaining rows tothe top of the 2D array

https://gyazo.com/64954d5a5ace39e7bb4264f2e2a9ecdf

CODE:

int main(int argc, char ** argv){
   int ** array2d;

   int size = 10;
   array2d = new int *[size];
   cout << "***** Before Compaction ******" << endl;
   for (int i = 0; i < size; i++) {
      array2d[i] = new int[size];
      cout << "ROW " <<i << ":";
      for (int j = 0; j < size; j++) {
         array2d[i][j] = i;
         cout << " "<< array2d[i][j];
      }
      cout << endl;
   }

   //NEED HELP WITH THIS PART DOWN HERE


   for (int i = 0; i < size; i++) {
      if (i % 2 == 0) { // delete the even rows
         delete[] array2d[i];
         array2d[i] = NULL;
      }
      // for each row in the first half, redirect the pointer
      // to the next odd row
      if(i<size/2){
         array2d[i] = array2d[2 * i + 1];
         array2d[2 * i + 1] = NULL;

      }

      cout << endl;
   }
   return 0;
}

Aucun commentaire:

Enregistrer un commentaire