CSCI
515 C/C++
Programming Fall 2002
Dr.
Creider
Lab 4 Assignment
Subtracting large integers
stored as groups of digits in an array
Write
a program to input two large numbers as character strings, store each number as
groups of digits in a separate single dimension array, subtract two large
integers and display the results.
In
the main program declare a character array of that has 256 elements in which to
store the input string. Declare three
long integer arrays that have 40 elements each. Write a loop to input a series of two large integer numbers (up
to 255 digits each one string at a time) as character strings until the user
enters the string “quit” in place of a string of digits for the first
number. Call a function to convert each
string separately to groups of digits.
Call a function to subtract the groups of digits. After the subtraction has been completed,
print the two numbers that were subtracted and the result that was produced.
Before
you call the function to subtract the groups of digits, test to determine which
array has the largest number and pass that array as the first group of
digits. The algorithm to determine this
is given as follows.
ALGORITHM
sign_of_num
= 1
// result is positive
if(first_groupA
== first_groupB) //
arrays may or may not contain the same number of groups of digits
if(Array A >= Array B) // **NOTE** Call
a function to determine which number is larger
subtract(A, first_groupA, B,
first_groupB, C, first_groupC, size) // size is the number of elements in each
array (40)
else
sign_of_num = -1 // result if negative
subtract(B, first_groupB, A,
first_groupA, C, first_groupC, size)
else
if(first_groupA < first_groupB)
// A has more groups than B
subtract(A, first_groupA, B,
first_groupB, C, first_groupC, size)
else
sign_of_num = -1 // result
if negative
subtract(B, first_groupB, A,
first_groupA, C, first_groupC, size) //
B has more groups than A
Write
a function to take each string of digits and convert it to groups of integer
digits (7 digits per group) stored in a long integer array. Use the function you wrote for Clab 2. Pass to the function each large integer number
separately as a character string, the integer array, and assign the subscript
of the array element that contains the first group of digits to the last
argument.
Write
a function to subtract two large integer numbers and store the result in a
third array. The algorithm to subtract
these groups is given in a diagram on a separate page.
Write
a function to print the number stored in one integer array. Print the groups of digits so that there are
no blanks between the digits in the long integer values. Use the function you previously wrote to
output groups of digits in an array.
You will have to modify this function so that you can print a minus sign
before the number if the number is negative.
Name
the file you create, LAB4. Assignment
due September 17, 2002. Do Not Use Any
Global Variables. Save both the source
(cpp file) and object code (exe) on your disk which is turned in at the end of
the lab.
Note: You should be able to write all functions
with this program when the least significant digits in the number are stored in
the last element of the array, or when the least significant digits are stored
in element zero of the array.
Examples of Subtraction
Subtracting large integers
stored as groups of digits in an array
Array A first_groupA
|
|
|
|
|
9851268 |
7895425 |
5874587 |
9875248 |
8521467 |
Array B first_groupB
|
|
|
|
|
|
3548987 |
9875245 |
3254875 |
5642874 |
subtracting Array B from Array A produces the following result
Array C first_groupC
|
|
|
|
|
9851268 |
4346437 |
5999342 |
6620373 |
2878593 |
Array A first_groupA
|
|
|
|
1 |
0000000 |
7895425 |
5874587 |
9875248 |
8521467 |
Array B first_groupB
|
|
|
|
|
|
8548987 |
9875245 |
3254875 |
5642874 |
subtracting Array B from Array A produces the following result
Array C first_groupC
|
|
|
|
|
9999999 |
9346437 |
5999342 |
6620373 |
2878593 |
Array A first_groupA
|
|
|
|
|
|
3548987 |
9875245 |
3254875 |
5642874 |
Array B first_groupB
|
|
|
|
|
|
7895425 |
5874587 |
9875248 |
8521467 |
subtracting Array A from Array B produces the following result
Array C first_groupC
|
|
|
|
|
|
-4346437 |
5999342 |
6620373 |
2878593 |
the answer is negative
Array A first_groupA
|
|
|
|
|
1 |
0000000 |
0000000 |
9875248 |
8521467 |
Array B first_groupB
|
|
|
|
|
1 |
0000000 |
0000000 |
9875248 |
8521466 |
subtracting Array B from Array A produces the following result
Array C first_groupC
|
|
|
|
|
|
|
|
|
1 |
NOTE: Examine the difference in the values in these arrays that produces a negative result