title: Bigint Subtraction in C++
body: <p>Here I have a bigint class that uses an array called SafeArray that I created in a different class(we couldnt use vectors) the set and get function calls are from the SafeArray class, get takes an int parameter for array position and set takes 2 int parameters(one for position and one for value) all methods in this class work fine except for my subtract and compare methods. What I really need to work is my subtraction method. Any help? Thanks</p>
  
  <pre><code> int size = 20;   //just for testing, will increase later
  class bigint
  {
      SafeArray&lt;int&gt; *arr;
  public:
  bigint()
  {
      arr = new SafeArray&lt;int&gt;;
      for(int i =0;i &lt; size; i++)
          arr-&gt;set(i,0);
  }
  
  void print()
  {
      for(int i = 0;i &lt;arr-&gt;get_size() ;i++)
      {
  
          cout &lt;&lt; arr-&gt;get(i);
  
      }
     cout&lt;&lt;endl;
  }
  
  void assign(const bigint &amp;A)
  {
      for(int i=0;i&lt;arr-&gt;get_size();i++)
      {
          arr-&gt;set(i,A.arr-&gt;get(i));
      }
  
  }
  
      void assign(int num)
  {
      for(int i = arr-&gt;get_size()- 1; i &gt;= 0; i--)
      {
          arr-&gt;set(i,num%10);
          num /=10;
      }
  
  
  }
  
  void assign(string num)
  {
      long len = num.length();
      int j=arr-&gt;get_size()-1;
      for(long i=len-1;i&gt;=0;i--)
      {
          arr-&gt;set(j,num[i]-48);
          j--;
      }
  }
  
  void add(const bigint &amp;A)
      {
  
          int carry=0;
          for(int i=size-1;i&gt;=0;i--)
         {
             int result = arr-&gt;get(i)+A.arr-&gt;get(i)+carry;
             arr-&gt;set(i,result%10);
             carry=result/10;}
  
          }
  
  void subtract(const bigint &amp;A) {
      for(int i=0, borrow=0; i&lt;size; ++i)
  
      {    int result=((arr-&gt;get(i) - A.arr-&gt;get(i) + borrow));
                       if(borrow == result &lt; 0) {
              A.arr-&gt;set(i,result+=10);
          }   }   }
  
  void compare(const bigint &amp; A)
  {
     //int comp;
     //for(int i =0;i&lt;size;i++)
     if(arr-&gt;get(size-1)&gt;A.arr-&gt;get(size-1))
          cout&lt;&lt;0;
  
        else
           cout&lt;&lt;1;
  
   }
  };
  int main()
  {
   bigint A,B,C,D;
   A.assign(12345);
   A.print();
   cout&lt;&lt;endl;
   C.assign("123456789000");
   C.print();
   cout&lt;&lt;endl;
   B.add(C);
   B.print();
   //B.compare(A);
   //B.subtract(A);
   //B.print();
  
  
  
  return 0;
  </code></pre>
  
  <p>}</p>