title: Flexible Safe Array C++
body: <p>My safearray can hold any type of data and can be resized if needed. What I have got it set to do is if the user inputs an array size that is smaller that the index in the for loop(30 in this case), it will resize itself. However it makes it too big so I just have a lot of zeros. for example if I input size 15, it will make it size 45 which allows me to store all the data but I have extra spaces I do not need. My TA says this is fine for a good grade, but since It's not due for another couple of days I would like to have a resize function that resizes the array to be exactly the same size as the index regardless of what the user input size is. I'm not sure how best to do this. Any help? thanks.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  
  template&lt;typename Element&gt;
  class SafeArray
  {
     int size;
      Element*Array;
      Element def;
  
  public:
      SafeArray()                         //default constructor(with no parameter)
      {
          Array = new Element[size];
      size = 10;
      }
      SafeArray(int value = NULL)         //constructor with one int
      {
          Array = new Element[value];
          size = value;
      }
      ~SafeArray() { delete [] Array;};                     //destructor
  
        Element get(int pos)                    //get method
      { if (pos&lt;0)
      {cout&lt;&lt;"error";}
  
          if(pos&gt;=size)
          { set_default(def);}
          return Array[pos]; }
  
      void set(int pos, Element val)      //set method
      { if (pos&lt;0)
      {
          cout&lt;&lt;"error";
      }
          if(pos&gt;=size)
      { resize(3); }
          Array[pos] = val; }
  
      void resize(int size_mult)           //resize function
      {
              Element*temp=new Element[size*size_mult];
              for(int i = 0; i&lt;size;i++)
              {temp[i]=Array[i];}
              delete[]Array;
              Array = temp;
              size=size*size_mult;
          }
      void set_default(Element d)        //set_default(just a safety precaution, doesn't really effect the outcome)
      {
           def=d;
      }
      //Element get_default()
     // {
     //     return def;
     // }
      int get_size()                       //get size
      {
          return size;
      }
  };
  
  
  int main()
  {
  
      int N;
      cout&lt;&lt;"How big should the Array be?"&lt;&lt;endl;
      cin&gt;&gt;N;
      SafeArray&lt;int&gt; X(N);
      SafeArray&lt;double&gt;Y(N);
      X.set_default(-1);
      cout&lt;&lt;"Array is size "&lt;&lt;X.get_size()&lt;&lt;endl;
  
      for(int i=0; i&lt;30;i++)
  
      {
          int x=i*3+1;
          double y =1000.0/x;
          X.set(i,x);
          Y.set(i,y);
      }
  
      for (int i = 0; i &lt;= X.get_size(); i += 1)
          {if(i&lt;10)
              cout &lt;&lt;"0"&lt;&lt; i &lt;&lt; ": x = " &lt;&lt; X.get(i) &lt;&lt; ", 1000/x = " &lt;&lt; Y.get(i) &lt;&lt; "\n";
          else
          cout &lt;&lt; i &lt;&lt; ": x = " &lt;&lt; X.get(i) &lt;&lt; ", 1000/x = " &lt;&lt; Y.get(i) &lt;&lt; "\n";}
              cout&lt;&lt;"Array is size "&lt;&lt;X.get_size()&lt;&lt;endl;
  
      return 0;
  
  }
  </code></pre>