Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm having the issues I was having before. Line 22 and 23 have uninitialized local variable errors from userAmtOfDigits1 and userAmtOfDigits2. I also have a

I'm having the issues I was having before. Line 22 and 23 have uninitialized local variable errors from userAmtOfDigits1 and userAmtOfDigits2. I also have a couple of warnings in line 40 for unreferenced local variable for amtOfDigits and userNumber. Below are the instructions and below that is my current code. This is in visual studio in c++.

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

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

***************************************************************************

#include

#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] - '0';

} //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*/)

{

int* pNum3;

if (numOfDigits >= numOfDigits2)//if no. of digits of num1 is greater or equal to num 2

{

numOfDigitsResult = numOfDigits + 1;

pNum3 = new int[numOfDigitsResult];

int carry = 0;

int i = numOfDigitsResult - 1;

int j = numOfDigits - 1;

int k = numOfDigits2 - 1;

while (k >= 0)

{

pNum3[i] = (pNum1[j] + pNum2[k] + carry) % 10;

carry = (pNum1[j] + pNum2[k] + carry) / 10;

i--; j--; k--;

}

while (j >= 0)

{

pNum3[i] = (pNum1[j] + carry) % 10;

carry = (pNum1[j] + carry) / 10;

i--; j--;

}

pNum3[i] = (carry) % 10;

}

else //if no. of digits of num2 is greater to num 1

{

numOfDigitsResult = numOfDigits2 + 1;

pNum3 = new int[numOfDigitsResult];

int carry = 0;

int i = numOfDigitsResult - 1;

int j = numOfDigits - 1;

int k = numOfDigits2 - 1;

while (j >= 0)

{

pNum3[i] = (pNum1[j] + pNum2[k] + carry) % 10;

carry = (pNum1[j] + pNum2[k] + carry) / 10;

i--; j--; k--;

}

while (k >= 0)

{

pNum3[i] = (pNum2[k] + carry) % 10;

carry = (pNum2[k] + carry) / 10;

i--; k--;

}

pNum3[i] = (carry) % 10;

}

if (pNum3[0] == 0)

{

int* pNum4 = new int[--numOfDigitsResult];

for (int i = 0; i

pNum4[i] = pNum3[i + 1];

return pNum4;

}

return pNum3;

} //end sumNum

void print(const int* pNum /*IN*/, int numOfDigits /*IN*/)

{

std::cout << "The sum of the numbers is: ";

for (int i = 0; i< numOfDigits; i++)

{

std::cout << pNum[i];

}

std::cout << " ";

} //end print

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions

Question

Is the message courteous and positive in spirit?

Answered: 1 week ago

Question

3 years blance sheet with income statement of a company

Answered: 1 week ago

Question

Were the decisions based on appropriate facts?

Answered: 1 week ago

Question

Were the right people involved in the decision-making process?

Answered: 1 week ago