title: Sparse Arrays And Matrices
body: <p>I need help with a program which allows the user random read access to any entry of the array. If the user attempts to read outside the useful region, your data structure should return a 0. Also It allows the user random write access to the useful region only. In case the user tries to write something in the part of the array that is supposed to be 0, print an error message.
  in addition the program should have constructor that take the array parameters as input only and initialize all of the array to 0. The second constructor should additionally take as input an array of ints copy its values to the useful part of the array you are making.</p>
  
  <p>This is the form of the array</p>
  
  <pre><code>[ 0 0 0 0 ... 0 0 1 2 3 4 5 0 0 .... 0 0 ] 
  </code></pre>
  
  <p>I am not sure how to create Sparse Array, also I don't want to use existing libraries.</p>
  
  <p>I have tried creating a dynamic array, which uses Overload operator, but my problem is that I don't even know what is Sparse Array and how to create it.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class MyArray
  {
      friend ostream&amp; operator&lt;&lt; (ostream &amp;os, MyArray &amp;array);
  
      public:
          MyArray (int size);
          MyArray (const MyArray &amp;rhs);
          ~MyArray ();
  
          MyArray&amp; operator= (const MyArray&amp; rhs);
          int&amp; operator[] (int index);
          int read_element (int index);
          void write_element (int index, int value);
  
      private:
          int *storage;
          int size;
  };
  
  #include "sparse_array_1d.h"
  
  MyArray::MyArray (int size)
  {
      storage = new int[size];
      this-&gt;size = size;
  }
  
  MyArray::MyArray (const MyArray &amp;rhs)
  {
      size = rhs.size;
      storage = new int[size];
      (*this) = rhs;
  }
  
  MyArray::~MyArray ()
  {
      delete [] storage;
  }
  
  int MyArray::read_element (int index)
  {
      return storage[index]; 
  }
  
  void MyArray::write_element (int index, int value)
  {
      storage[index] = value;
  }
  
  MyArray&amp; MyArray::operator= (const MyArray &amp;rhs)
  {
      int i,min_size;
  
      if (size &lt; rhs.size)
          min_size = size;
      else
          min_size = rhs.size;
  
      for (i=0; i&lt;min_size; i++)
          storage[i] = rhs.storage[i];
  
      return (*this);
  }
  
  int&amp; MyArray::operator[] (int index)
  {
      if (index &lt; size &amp;&amp; index &gt;=0)
          return storage[index];
  
      return storage[0]; 
  }
  
  ostream&amp; operator&lt;&lt; (ostream &amp;os, MyArray &amp;array)
  {
      int i;
  
      os &lt;&lt; "[ ";
  
      for (i=0; i&lt;array.size; i++)
          os &lt;&lt; array[i] &lt;&lt; " ";
  
      os &lt;&lt; "]" &lt;&lt; endl;
      return os;
  }
  
  #include &lt;iostream&gt;
  #include "sparse_array_1d.h"
  
  using namespace std;
  
  int main()
  {
      int i,size;
  
      cout &lt;&lt; "What array sizes would you like?" &lt;&lt; endl;
      cin &gt;&gt; size;
  
      MyArray p1 (size);
  
      //int q=1;
      for (i=0; i&lt;size; i++)
      {
          p1[i] = 0;
          //q++;
      }
  
      cout &lt;&lt; "p1: " &lt;&lt; endl;
      cout &lt;&lt; p1;
  
      int x;
  
      cout &lt;&lt; endl;
      cout &lt;&lt; "enter numbers : " &lt;&lt; endl;
  
      for (i=0 ; i&lt;size; i++)
      {   
          cin &gt;&gt; p1[i];
      }
  
      cout &lt;&lt; "This is the array" &lt;&lt; endl;
      cout &lt;&lt; p1;
      cout &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>