Extra Additives Lab

Computers store decimal numbers in binary form to conserve memory. Even though this allows a greater range of values, the largest integer value in many systems is 32,767 and long integers, the largest value is less than 2.2 billion.

Your task is to write a program that inputs pairs of numbers between 0 and 100 billion, inclusive. You should then add these numbers together and print the results.

Input: The input will only consist of digit characters ('0' . . '9'). No commas or other punctuation will be in the data. All values will be non-negative. Since the integers may exceed any binary integer value allowed in the system, we shall read in the values as an apstring and convert each integer character to its corresponding integer digit and store it in an integer array.

Output: Your program should print out the results of the addition. Do not print any leading zeros before any answer. Put a loop in your program to run it four times.

Input: Output:
7453 The sum is: 131688
0 The sum is: 325235
100000000000 The sum is: 100000023523
76434124235 The sum is: 76760470359

Use the declarations at right const NUM_DIG = 13; // 100 billion + 1 digit
to store your digits. apvector largeInt (NUM_DIG,0); // initialize to zero

To convert digit characters to int asciiToInt (char ch)
digit integers you may use {
this function. return (ch - '0');

Study Questions.

  1. How could the largeInt type be modified to include negative large numbers?

  2. How does your program store such largeInts such as 1,234,567,899? Illustrate with boxes and indices.

  3. Explain why you decided on your way of storing largeInt 's.

  4. Why must a largeInt be initialized before being filled?

  5. Why should a largeInt always be passed as a reference parameter?

  6. What can be done to prevent an undesirable change to a largeInt parameter?

  7. List at least three errors that might be generated when your largeInt Addition program is run.

  8. List at least three strategies for handling errors.

  9. What would have to be done to add a normal integer like 42 to a largeInt?

  10. What is the big-O time function for adding two largeInts both of length n?

Continue to:  Unit 2  / Prev  / Next