Question
In C++, the largest int value is 2147483647 (10 decimal digits). So, an integer larger than this cannot be stored and processed as an integer.
In C++, the largest int value is 2147483647 (10 decimal digits). 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 a number in an array. Write a program that inputs two positive integers of any length and outputs the sum of the numbers. For the purpose of this assignment, the numbers may contain any number of digits. Your program must contain those functions:
A function to read and store a number as individual digits into a dynamic array created in the function. This function must then return the address of this dynamic array. (Must be done in the specified way. Otherwise 0 credit)
A function to add two numbers stored in arrays and return the sum result as a pointer. This means the sum result must be stored in a dynamic array created in this function. Each digit of the result should be stored separately. (Must be done in the specified way. Otherwise 0 credit)
A function to print out a number stored as an array of individual digits.
You may add additional functions when fit.
The required functions may look like this:
// read in a number
int* readNum(int& numOfDigits/*OUT*/);
// add two numbers. Store result in third
int* sumNum(const int * pNum1/*IN*/, int numOfDigits1/*IN*/,
const int * pNum2/*IN*/, int numOfDigits2/*IN*/,
int& numOfDigitsResult/*OUT*/);
// print a number stored in int array
void print(const int * pNum/*IN*/, int numOfDigits/*IN*/);
Your .cpp file should also contain:
Algorithms (pseudo code) for the function to add two numbers. Add it as block comments at the beginning of your source code file. If you choose to draw flowchart, include it in your HW document instead.
Pre- and Post- condition comments for each function.
/*IN*/, /*OUT*/, /*INOUT*/ comments to function parameters
The screenshot below shows a sample run.
Test your program with at least three different testing cases and provide matching screenshots of your running program. Explain how each testing case is different.
Comment your program appropriately. Pay attention to the standard stuff like coding style, indention, heading, and curly braces
***************************************************************************
Good Afternoon,
Im having trouble with the assignment. Above is the instructions and below is what I have coded so far. This class is in C++.
/*************************************************************** * HW7_.cpp * * * This program takes two positive integers of any length and * outputs the sum of the numbers ****************************************************************/
#include
int* readNum(int& numOfDigits /*OUT*/);
int* sumNum(const int* pNum1 /*IN*/, int numOfDigits /*IN*/, const int* pNum2 /*IN*/, int numOfDigits2 /*IN*/, int& numOfDigitsResult /*OUT*/);
void print(const int* pNum /*IN*/, int numOfDigits /*IN*/);
int main() { int userAmtOfDigits1, userAmtOfDigits2, totalDigits; int *userNumberPtr1 = new int[userAmtOfDigits1]; int *userNumberPtr2 = new int[userAmtOfDigits2]; int* userNumberPtr3;
userNumberPtr1 = readNum(userAmtOfDigits1); userNumberPtr2 = readNum(userAmtOfDigits2);
userNumberPtr3 = sumNum(userNumberPtr1, userAmtOfDigits1, userNumberPtr2, userAmtOfDigits2, totalDigits);
print(userNumberPtr3,totalDigits);
return 0; } //end main
int* readNum(int& numOfDigits /*IN*/) { std::string userInput; int userNumber, amtOfDigits;
std::cout << "Please enter a positive integer: "; std::getline(std::cin, userInput);
numOfDigits = userInput.length(); int* returnNumPtr = new int[userInput.length()];
for (int i = 0; i < userInput.length(); i++) { returnNumPtr[i] = userInput[i];
} //end for
return returnNumPtr; } //end readNum
int* sumNum(const int* pNum1 /*IN*/, int numOfDigits /*IN*/, const int* pNum2 /*IN*/, int numOfDigits2 /*IN*/, int& numOfDigitsResult /*OUT*/) { numOfDigitsResult = numOfDigits + numOfDigits2; int* pNum3 = new int[numOfDigitsResult];
for (int i = 0; i < numOfDigitsResult; i++) { pNum3[i] = pNum1[i] + pNum2[i]; } //end for
return pNum3; } //end sumNum
void print(const int* pNum /*IN*/, int numOfDigits /*IN*/) { pNum = new int[numOfDigits];
std::cout << "The sum of the numbers is: " << pNum;
delete pNum; } //end pr
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started