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

4. Programming Exercise 11 in Chapter 8 (I wrote it below) explains how to add l

ID: 3766858 • Letter: 4

Question

4. Programming Exercise 11 in Chapter 8 (I wrote it below) explains how to add large integers using
arrays. However, in that exercise, the program could add only integers of, at
most, 20 digits. This chapter explains how to work with dynamic integers.
Design a class named largeIntegers such that an object of this class can
store an integer of any number of digits. Add operations to add, subtract,
multiply, and compare integers stored in two objects. Also add constructors to
properly initialize objects and functions to set, retrieve, and print the values
of objects.

This is what #11 says:

(Adding Large Integers) In C++, the largest int value is 2147483647 .
So, an integer larger than this cannot be stored and processed as an integer.
Similarly, if the sum or product of two positive integers is greater than
2147483647 , the result will be incorrect. One way to store and manipulate
large integers is to store each individual digit of the number in an array.
Write a program that inputs two positive integers of, at most, 20 digits and
outputs the sum of the numbers. If the sum of the numbers has more than
20 digits, output the sum with an appropriate message. Your program must,
at least, contain a function to read and store a number into an array and
another function to output the sum of the numbers. (Hint: Read numbers as
strings and store the digits of the number in the reverse order.)

Explanation / Answer

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
#define DIGITS 20
class largeintegers
{
public:
largeintegers();
void Input();
void Output();
largeintegers operator+( largeintegers );
largeintegers operator-( largeintegers );
largeintegers operator*( largeintegers );
int operator==( largeintegers);
private:
int integer[ DIGITS ];
int len;
};
void largeintegers::Output()
{
int i;
for (i=len-1;i >= 0; i-- )
cout<<integer[i];
}
void largeintegers::Input()
{
string in;
int i,j,k;
cout << "Enter a number("<<DIGITS<<" digits max):";
cin >> in;   
for(i=0;in[i]!='';i++);
len=i;
k=0;
for(j=i-1;j>=0;j--)
integer[j]=in[k++]-48;
}
largeintegers::largeintegers( )
{
for ( int i = 0; i <DIGITS; i++ )
integer[ i ] = 0;
len=DIGITS-1;
}
int largeintegers::operator==( largeintegers op2 )
{int i;
if(len<op2.len)
return -1;
if(op2.len<len)
return 1;
for(i=len-1;i>=0;i--)
if(integer[i]<op2.integer[i])
return -1;
else if(op2.integer[i]<integer[i])
return 1;
return 0;
}
largeintegers largeintegers::operator+( largeintegers op2 )
{largeintegers temp;
int carry = 0;
int c,i;
if(len>op2.len)
c=len;
else
c=op2.len;
for ( i=0; i<c; i++ )
{temp.integer[ i ] =integer[ i ] + op2.integer[ i ] + carry;
if ( temp.integer[ i ] > 9 )
{temp.integer[ i ] %= 10;
carry = 1;
}
else
carry = 0;
}
if(carry==1)
{temp.len=c+1;
if(temp.len>=DIGITS)
cout<<"***OVERFLOW***** ";
else
temp.integer[i]=carry;
}
else
temp.len=c;
return temp;
}
largeintegers largeintegers::operator-( largeintegers op2 )
{largeintegers temp;
int c;
if(len>op2.len)
c=len;
else
c=op2.len;
int borrow = 0;
for( int i = c;i >= 0;i--)
if(borrow==0)
{if(integer[i]>=op2.integer[i])
temp.integer[i]=integer[i]-op2.integer[i];
else
{borrow=1;
temp.integer[i]=integer[i]+10-op2.integer[i];
}
}
else
{borrow=0;
if(integer[i]-1>=op2.integer[i])
temp.integer[i]=integer[i]-1-op2.integer[i];
else
{borrow=1;
temp.integer[i]=integer[i]-1+10-op2.integer[i];
}
}
temp.len=c;
return temp;
}   
largeintegers largeintegers::operator*( largeintegers op2 )
{ largeintegers temp;
int i,j,k,tmp,m=0;
for (int i=0; i<op2.len; i++)
{ k=i;
for (j=0; j< len; j++)
{tmp = integer[ j ] * op2.integer[i];
temp.integer[k] =temp.integer[k]+tmp;
temp.integer[k+1] =temp.integer[k+1]+ temp.integer[k]/10;
temp.integer[k] %=10;
k++;
if(k>m)
m=k;
}
}
temp.len=m;
if(temp.len>DIGITS)
cout<<"***OVERFLOW***** ";
return temp;
}
using namespace std;
int main()
{int c;
largeintegers n1,n2,result;
n1.Input();
n2.Input();
n1.Output();
cout <<" + ";
n2.Output();
result=n1+n2;
cout<< " = " ;
result.Output();
cout << " ";
n1.Output();
cout <<" - ";
n2.Output();
result=n1-n2;
cout<< " = " ;
result.Output();
cout << " ";
n1.Output();
cout <<" * ";
n2.Output();
result=n1*n2;
cout<< " = " ;
result.Output();
cout << " ";
c=n1==n2;
n1.Output();
switch (c)
{case -1: cout<<" is less than ";
break;
case 0: cout<<" is equal to ";
break;
case 1: cout<<" is greater than ";
break;
}
n2.Output();
cout << " ";
system("pause");
}

___________________________________________________

#include <cstdlib.h>
#include <iostream.h>
#include <cctype.h>

using namespace std;

const int MAXIMUM_DIGITS = 20;

int size; // number of digits in the integer
int digit[MAXIMUM_DIGITS]; // the digits stored in reverse order

void introduction();
void input_Large_Int(int a[], int& size_of_A);
void output_Large_Int(int a[], int size_of_A);
void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum);

int main()
{
int a[MAXIMUM_DIGITS], b[MAXIMUM_DIGITS], sum[MAXIMUM_DIGITS];
int size_of_A, size_of_B, size_Sum;
char ans;
for(int i=0;i<MAXIMUM_DIGITS-1;i++)
   {
       a[i] =0;
       b[i]=0;
   sum[i]=0;
   }
introduction();

do {
  
input_Large_Int(a, size_of_A);
input_Large_Int(b, size_of_B);

add(a,size_of_A,b,size_of_B,sum,size_Sum);
cout << "The sum of ";
output_Large_Int(a, size_of_A);
cout << " and ";
output_Large_Int(b, size_of_B);
cout << " is ";

output_Large_Int(sum,size_Sum);
       cout << endl << endl;
cout << "Would you like to add two more numbers? (y or n): ";
cin >> ans;
}
while (ans == 'y' || ans == 'Y');
  
system("PAUSE");
return 0;
}

void introduction()
{
cout << "This program will allow you to input two large integers and add "
<< "them together. Then you will be allowed to run the program again "
<< "and again depending on how many calculations you would like to make. "
<< endl << endl;
}

void input_Large_Int(int a[], int& size_of_A)
{
char digit[MAXIMUM_DIGITS];
char change;
int i = 0;

cout << "Please enter a positive integer with no more than " << MAXIMUM_DIGITS
<< " digits: ";

cin.get(change);
if (change == ' ')
cin.get(change);
while (isdigit(change) && i < MAXIMUM_DIGITS)
{
digit[i] = change;
i++;
cin.get(change);
}

size_of_A = i;
int j = 0;
while (i > 0)
{
i--;
a[j] = digit[i] - '0';
j++;
}

for (int i = j; i < MAXIMUM_DIGITS; i++)
a[i] = 0;

}


void output_Large_Int(int a[], int size_of_A)
{
  
for (int i = 0; i < size_of_A; i++)
cout << a[size_of_A - i - 1];
}

void add(int a[], int size_of_A, int b[], int size_of_B, int sum[], int& size_Sum)
{
int i;

for(i = 0; i < MAXIMUM_DIGITS - 2; i++)
{
sum[i] += (a[i] + b[i]) % 10;
sum[i + 1] += (a[i] + b[i]) / 10;
}
i++;
sum[i] += (a[i] + b[i]) %10;
if ((a[i] + b[i])/10 > 0)
{
cout << "There was an overflow causing more than 20 integers. "
<< endl;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote