Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\"size\" variable and replacing that \\int *\" with vector< int >. int setSize(i

ID: 3812063 • Letter: #

Question

"size" variable and replacing that int *" with vector< int >.

int setSize(int dsize);[header file]

int IntList::setSize(int dsize) [cpp file]

{   if( dsize<0 ) {     dsize=0;   }

else

{   

  if( data!=0 )

{       // means there is already memory allocated   

    delete [] data;

     data=0;   

    size=0;     }  

   if( dsize!=0 )

{       data=new int[dsize];    

   if( data==0 )

{         size=0;         cerr << "Warning: unable to allocate enough space for list" << endl;         exit(1); // this surely is reasonable.       }

else

{         size=dsize;       }   

  }

}

return size; }

Explanation / Answer

So the code goes:

// must include this somewhere:
#include <vector>

int setSize(int dsize) {
  data.resize( dsize ); // assuming data is defined as a vector<int> somewhere in your code, preferably in the constructor
}

// In your code you should declare the variable data as :
vector<int> data

This should preferably be done in the class definition like:
class test {
  vector<int> data;
}

In place of:
class test {
  int *data;
}

And you should be done now!