Sum two integers up to 20 digits

Sum two integers up to 20 digits

Write a program that will allow the user to sum two integers up to 20 digits big. The integers should be stored in a string with no more than size 20. The integers will be read in as a string, not an integer. Hints...

1) since each integer is stored as a string, it is already in a character array.

2) reverse each integer and store it in a temp string to do the addition. You are not required to do this, but this will make the processing a lot easier.

3) it would be best to declare an int for the carry. If you add two elements of an array and there is a carry, add the carry to the next summation.

4) If the sum of the 2 integers is more than 20 digits, print 'Overflow'

5) Prompt the user to do another.

6) Clear the screen between runs (using our new cls() and pause() presented in class, not system calls)

7) Use functions for most of the processing. Below are the prototypes for the functions you should use.

string inputnums(); //Used to input the numbers (the string returned is the input number)

Declare a string (to store the input) and an int to store the size

Get the string

Calculate size

Loop while size is greater than maxsize (20)

Input error overflow

Input new number

Get the new size

End of loop

Initialize a LCV to 0

Loop while LCV is less than the size

if the character at index LCV is a digit

increment LCV

else (if it never enters here, then one the characters in the number was a digit)

print error message that a character was detected in the number please re enter input new number

set LCV back to zero and start the loop over again to check the new number

end of loop

return the string to main() as a number that has passed input error checking;

End of input()

string reverse(string, int); String is the number, int is the size of the number declare a string that will contain the reversed number;

loop from size-1 to 0 (decrementing by 1)

concatenate the number string index to the reversed string

end loop

return reversed string to a string declared in main(). This is the string you will use to add

end of reverse()

void pad( string &, string &, int &, int &); //concatenates ‘0’ to the shortest reversed string, so that both numbers are the same length the strings is the reversed numbers, the ints are the lengths of the padded strings because we need to pad the shortest string with ‘0’s (so that both string have the same length

before we add them), we pass both strings in by reference, so that the original string gets changed (only the shortest will get changed, but we don’t know which one, so we have to pass both by reference, same thing with the lengths)

if (s1 is larger)

loop from 1 to larger size – smaller size concatenating shorter string with ‘0’ (by passing by reference will change original number so we don’t have to return it)

end =loop;

get new size for shorter string (by passing by reference the size of the shorter number has been changed, so we don’t have to return it) else do the same thing as above, except you are padding the other number string add(string, string, int); //Adds the two numbers. Don’t forget to reverse the answer void print(string, string, string, int); //Prints the answer in the following form (see below) char prompt(); //prompts the user to do another

Answer printed in the following format (make adjustments for the size of the numbers)
Powered by