need for simple program of arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tamara omar
    New Member
    • Sep 2006
    • 23

    need for simple program of arrays

    declare two dimensional array of type double using dynamic memory allocation then write code to delete this two dimensional array???
  • m013690
    New Member
    • Sep 2006
    • 23

    #2
    Originally posted by tamara omar
    declare two dimensional array of type double using dynamic memory allocation then write code to delete this two dimensional array???
    double *dPtr[5] = new double[5];

    delete[ ] dPtr;

    Comment

    • D_C
      Contributor
      • Jun 2006
      • 293

      #3
      Code:
        int WIDTH;
        int HEIGHT;
        
        cout << "Enter array width: ";
        cin >> WIDTH;
        cout << "Enter array height: ";
        cin >> HEIGHT;
      
        char* array2D = (char*)malloc(WIDTH*HEIGHT*sizeof(char));
        free(array2D);

      Comment

      • tamara omar
        New Member
        • Sep 2006
        • 23

        #4
        plz fix my program!!!

        i asked for a program which declares a two dimensional array of type double using dynamic memory allocation and then delete this array but im a begginer in c++ and i dont know how to completly write it ,i tried and thats it
        #include<iostre am>

        using namespace std;

        int main()
        {


        double *dPtr[5]= new double[5];


        delete[] dPtr;
        cout<<&dPtr[]<<" "<<endl;
        return 0;
        }

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Please don't double post

          Unfortunately you have chosen to use the non-working code from this thread instead of the working code. There is no such thing as an allocated 2D array, it's in your head.

          However you can simulate this imaginary concept like this

          Code:
          #include <iostream>
          
          using namespace std;
          
          int main()
          {
              double *dPtr[5];
              double *data = new double[25];
          
              dPtr[0] = data;
              dPtr[1] = dPtr[0]+5;
              dPtr[2] = dPtr[1]+5;
              dPtr[3] = dPtr[2]+5;
              dPtr[4] = dPtr[3]+5;
          
              delete data;
          
          //    cout<<&dPtr[]<<" "<<endl;
          
              return 0;
          }
          I have commented out the cout line because it is not at all clear what you where trying to achieve with it (especially after the data has been deleted).

          Comment

          Working...