Programming Exercise 11 in Chapter 8 explains how to add large integers using ar
ID: 3572510 • Letter: P
Question
Programming Exercise 11 in Chapter 8 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 largelntegers 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. REFERENCE: (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.Explanation / Answer
#include<cstdlib>
#include<iostream>
#include<string>
using namespace std;
class largeInteger {
public:
largeInteger(int* array, int newSize){
datamember = array;
size = newSize;
}
void print(int largeInteger);
largeInteger add(largeInteger rhs);
int* dataMember; //The new data member that we added
int size;
};
largeInteger input() {
int* input;
cout << "Please enter a large number:";
string userInput;
cin>>userInput;
input = new int[userInput.length()]
for( int i=userInput.length() - 1; i >= 0; i-- )
{
int intValue = userInput[i] - '0'; //This converts the char inside the string into an int
input[i] = intValue;
}
largeInteger big = largeInteger (input, userInput.length());
return input;
}
void largeInteger :: print() {
for ( int i = size; i >= 0; i--) {
cout << dataMember[i];
}
}
largeInteger largeInteger :: add( largeInteger rhs )
{
int * sum;
if ( size >= rhs.size)
{
sum = new int[ size + 1 ];
int carry = 0;
//Add two numbers
for ( int i = 0; i < rhs.size; i++ )
{
int partialSum = dataMember[ i ] + rhs.dataMember[ i ] + carry;
if ( partialSum >= 10 )
{
carry = 1;
partalSum -= 10;
}
else
carry = 0;
sum[ rhs.size - i - 1] = partialSum;
}
for ( int i = size - 1; i >=rhs.size; i--)
{
sum[i] = dataMember[i];
}
return largeInteger ( sum, size+1);
}
else
{
sum = new int ( rhs.size +1);
int carry = 0;
for ( int i = 0; i < size; i++)
{
int partialSum = dataMember[i] + rhs.dataMember[i] + carry;
if ( partialSum >= 10)
{
carry = 1;
partialSum -= 10;
}
else
carry = 0;
sum[ size - i - 1] = partialSum;
}
for (int i = rhs.size - 1; i >=size; i--)
{
sum[i] = rhs.dataMember[i];
}
return largeInteger(sum, rhs.size + 1);
}
}
int main( int argc, char * argv[]) {
int * largeInteger;
largeInteger = new int ;
sustem( " PAUSE');
delete largeInteger;
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.