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

C++ Programming (7th Edition) Author Malik, D.S.; Malik, D. Chapter 12 Programmi

ID: 3581477 • Letter: C

Question

C++ Programming (7th Edition) Author Malik, D.S.; Malik, D.

Chapter 12 Programming Excerise 4

please !! I need the answer to this question!! Need Help ASAP!!!!

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. (Hint: Read numbers as strings and store the digits of the number in the reverse order.)

Explanation / Answer

#include <iostream>
using namespace std;
char getDigit(void)
{
char ch;
cin>>noskipws>>ch;
return ch;
}
string getNumber(void)
{
string number = "";
char ch = getDigit();
while(ch != ' ')
{
number += ch;
ch = getDigit();
}
return number;
}
string addNumbers(string a, string b)
{
string out = "";
int i = a.length()-1, j = b.length()-1, carry = 0;
while(i >= 0 && j >= 0)
{
int temp = (a[i] - '0') + (b[j] - '0') + carry;
out += (temp % 10 + '0');
carry = temp / 10;
i--; j--;
}
string temp = "";
for(int i = out.length()-1; i >= 0; i--)
temp += out[i];
return temp;
}
int main()
{
string num1, num2;
cout<<"Enter the first number: ";
num1 = getNumber();
cout<<"Enter the second number: ";
num2 = getNumber();
cout<<num1<<" + "<<num2<<" = "<<addNumbers(num1, num2)<<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