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

Class largeIntgers Write a class (largeIntgers) that supports the following func

ID: 3815167 • Letter: C

Question

Class largeIntgers Write a class (largeIntgers) that supports the following functionalities: For a given value for the length of a large integer, generates the digits of this large integer and stores them in a dynamic array. Suppose only positive values for this integer. Prints digits of this large integer on the console. Adds this "large integer" with another "large integer" that is already generated. Additional requirements and instructions: No use of global variables. Code is well commented. Submit all code files needed to compile and run this program, including a sample input file. Comply with syllabus policies.

Explanation / Answer

#include"largeIntegers.h"

largeIntegers::largeIntegers(int len)
{
   length = len;

   // initialize array with len //
   Arr = new int[len];

   /* Intializes random number generator */
   srand((unsigned)time(0));

   /* store ramdom number in array index by index */
   for (int i = 0; i < len; i++)
   {
       Arr[i] = rand() % 10;
   }
}

largeIntegers::~largeIntegers()
{
   delete[]Arr;
   Arr = NULL;
}

void largeIntegers::printnumber(int len)
{
   cout << "Printing Large Number ..." << endl;
   for (int i = 0; i < len; i++)
   {
       cout << this->Arr[i];
   }
   cout << endl;
}

largeIntegers largeIntegers::add(const largeIntegers&num)
{
   int size = 0;
   if (this->length < num.length)
   {
       // shift elements to right
       int shift = num.length - this->length;

       // TODO implementing shifting logic of elements to right
/*       for (int i = 0; i < shift; i++)
       {
           num.Arr[]
       }
*/
       size = num.length;
   }
   else
   {
       size = this->length;
   }
   largeIntegers temp(size);

   int sum = 0;
   int carry = 0;
   int arr_01Len = this->length;
   int arr_02Len = num.length;

   // 58988
   // 120

   for (int i = size-1; i >=0; i--)
   {

       if (arr_01Len >= 0 && arr_02Len >= 0)
       {
           sum = carry + this->Arr[i] + num.Arr[i];
           if (sum >= 10)
           {
               carry = 1;
               temp.Arr[i] = sum - 10; // 18 -10 = 8
           }
           else
           {
               temp.Arr[i] = sum; // 108
           }
           arr_01Len--;
           arr_02Len--;
       }

       if (arr_01Len > 0)
       {
           sum = carry + this->Arr[i];
           if (sum >= 10)
           {
               carry = 1;
               temp.Arr[i] = sum - 10; // 18 -10 = 8
           }
           else
           {
               temp.Arr[i] = sum; // 108
           }
       }
       if (arr_02Len > 0)
       {
           sum = carry + num.Arr[i];
           if (sum >= 10)
           {
               carry = 1;
               temp.Arr[i] = sum - 10; // 18 -10 = 8
           }
           else
           {
               temp.Arr[i] = sum; // 108
           }
       }
   }

   return temp;
}


int main()
{
   largeIntegers obj1(15);
   obj1.printnumber(15);
   largeIntegers obj2(20);
   obj2.printnumber(20);
   obj1.add(obj2).printnumber(20);
}