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

overload the modulus operator for a class that uses arrays to represent large po

ID: 3887032 • Letter: O

Question

overload the modulus operator for a class that uses arrays to represent large positive numbers. Example: 1234 would be in an array like this... int arr[]={1,2,3,4}. Overload the % operator so that when two of the class objects are divided the remainder would be given. Example: bigposint num, num2; (private variables are arrays: num has arr[]={1,2,3,4} and num2 has arr[]={3,4,5,6} thus num2% num should be 3456% 1234 but the implementation should be written using the arrays apart of the class objects. )

Explanation / Answer

#include<iostream>
using namespace std;
class computemod
{
   public :
       int num[10000];//variable
       int size;//number of digits in variable
       //constructor
   computemod(int a[],int n)
   {
       int i;
       for(i=0;i<n;i++)
       {
           num[i]=a[i];  
       }
       size=n;  
   }
   bool find(int n[],computemod const &num1)
   {
   int i=0;
   while(i<size)
   {
           if(n[i]<num1.num[i])return false;
           else if(n[i]>num1.num[i])return true;
           else i++;      
   }
     

   }
   void subtract(int n[],computemod const &num1)
   {
       int i=size-1;
      
       while(i>0)
       {
           if(n[i]>num1.num[i])
           {
               n[i]=n[i]-num1.num[i];  
           }
           else if(n[i]<num1.num[i])
           {
               n[i-1]--;
               n[i]=10+n[i]-num1.num[i];  
           }
           else
           {
               n[i]=0;
           }
           i--;
          
       }
       if(n[i]>num1.num[i])
           {
               n[i]=n[i]-num1.num[i];  
           }
           else if(n[i]==num1.num[i])
           {
               n[i]=0;  
           }
          
         
          
   }
   computemod operator % (computemod const &num1)
   {
       int n[size];
       int i=0;
       while(i<size)
       {
           n[i]=num[i];  
           i++;
       }
       //print();
      
       while(find(n,num1)==true)
       {
           //cout<<"HELLO ";
           subtract(n,num1);  
       }
       computemod b(n,size);
       return b;
   }
   void print()
   {
       int i;
       while(i<size)
       {
           cout<<num[i]<<" ";  
           i++;
       }  
       cout<<" ";
   }
      
};
int main()
{
   int num2[] ={1,2,3,4};
   int num1[] = {3,4,5,6};
   computemod a(num2,4),b(num1,4);
   cout<<"resutlt :";
   computemod c = b%a;
   c.print();
  
   return 0;
}

output;-

resutlt :0 9 8 8


Process exited normally.
Press any key to continue . . .