Question
C++ pleasr
Extra Credit: Large Integer (25 points) In C++, the largest int value is 2147483647. So, an integer larger than this cannot b
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <sstream>
using namespace std;

string trimString(string str) {
std::stringstream trimmer;
trimmer << str;
trimmer >> str;
return str;
}

void readNum(int N[], int &length) {
string num;

do {
cout << "Enter the positive integer of at most 20 digits:";
cin >> num;

num = trimString(num);

if(num.size() > 20) {
cout << "Number should be of maximum 20 digits only." << endl;
}
} while(num.size() > 20);

length = num.size();

int count = 1;
for(int i = num.size() - 1; i >= 0; i--) {
N[20 - count++] = num.at(i) - '0';
}
}

void printNum(int N[], int arrayLength, int length) {
for(int i=arrayLength-length; i < arrayLength; i++) {
cout << N[i];
}
cout << endl;
}

void sumNum(int N1[], int l1, int N2[], int l2) {
int result[21] = {0};
int length = 0;

int max = (l1 > l2 ? l1 : l2);
int carry = 0;

for(int i=20-1; i>=(20 - max); i--) {
result[i + 1] = N1[i] + N2[i] + carry;

if(result[i + 1] >= 10) {
carry = 1;
result[i + 1] -= 10;
} else {
carry = 0;
}
length++;
}

cout << "The sum of the numbers is:" << endl;
if(carry != 0) {
result[21 - 1 - length] = 1;
length++;
}

if(result[0] != 0) {
cout << "the sum of the numbers overflows. It has 21 digits." << endl;
}
printNum(result, 21, length);
}

int main() {
int N1[20] = {0}, N2[20] = {0};
int l1, l2;
readNum(N1, l1);
readNum(N2, l2);

cout << endl;
cout << "First Number: ";
printNum(N1, 20, l1);
cout << "Second Number: ";
printNum(N2, 20, l2);

cout << endl;
sumNum(N1, l1, N2, l2);
}

saved share gcc version 4.6.3 .cpp Enter the positive integer of at most 20 digits: 98271387213652132153 Enter the positive i

Add a comment
Know the answer?
Add Answer to:
C++ pleasr Extra Credit: Large Integer (25 points) In C++, the largest int value is 2147483647....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. (Dynamic Array) In C++, the largest int value is 2147483647. So. an integer larger than this cannot be stored and pr...

    3. (Dynamic Array) 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. Your program must, at least, contain a function to read and store a number into an...

  • please use c++ programming and single dimensional arrays to solve this problem thank you Problem 02:...

    please use c++ programming and single dimensional arrays to solve this problem thank you Problem 02: Large Integer (20 points) In CH, 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...

  • This is programming project 1 chapter 9(Book ISBN:1292222824 (1-292-22282-4) Walter Savitch Problem Solving with C++: Global...

    This is programming project 1 chapter 9(Book ISBN:1292222824 (1-292-22282-4) Walter Savitch Problem Solving with C++: Global Edition, 10/E) Question Do Programming Project 7 in Chapter 7 using a dynamic array. In this version of the problem, use dynamic arrays to store the ditits in each large integer. Allow an arbitrary number of digits instead of capping the number of digits at 20. TER 7/ Arrays time. For example digit at a the integer 1234 could be stored in the array...

  • Java Project 1. The largest positive integer of type int is 2,147,483,647. Another integer type, long,...

    Java Project 1. The largest positive integer of type int is 2,147,483,647. Another integer type, long, represents integers up to 9,223,372,036,854,775,807. Imagine that you want to represent even larger integers. For example, cryptography uses integers having more than 100 digits. Design and implement a class Huge of very large nonnegative integers. The largest integer should contain at least 30 digits. Use a deque to represent the value of an integer. Provide operations for the class that * Set the value...

  • it should be written in c or c++ program Question 1 (25 points) An integer number...

    it should be written in c or c++ program Question 1 (25 points) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 -1 + 2 + 3. Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1...

  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help...

    ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help of an integer array of size 100 (elements of this array will be digits of the Fibonacci number). When the function is called to find , it will calculate all Fibonacci numbers from to using the basic formula . To add two Fibonacci numbers, the function will add elements of two arrays corresponding to and and store their sums in the array corresponding to...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • COP2221 - Intermediate C++ Programming Module #6 Assignment One 20 points This assignment refers to Learning...

    COP2221 - Intermediate C++ Programming Module #6 Assignment One 20 points This assignment refers to Learning Outcome #2: Create and utilize arrays to store lists of related data Programming Problem You have been hired to create a C++ program to simulate the lottery. Your program allows users to see how often their lottery number choices match a randomly generated set of numbers used to pick lottery winners. The company, "How Lucky Are You?, Inc." wants a modular, professional and user-friendly...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT