Sie sind auf Seite 1von 4

LAB 4 DUE JUNE 16 T H

PHASE 1 FROM THE HOMEWORK INTRODUCTION TO ARRAYS


NOTES: You may not use any functions we have not covered in class!! This program will take
some time to do! Do not wait til Thurs afternoon to start it. Most students would not be able to
finish.
Write a program that calls 1 function. The function will be passed a char array of size 20.
Function InitNumber: This is a void function that receives a char array of size 20. The function
will prompt the user to input the number of digits in the number. This number should be between
1 and 20 inclusive. The user should then input the number with the LEAST significant digit first.
The number will then be stored in the array with the least significant digit in the array element 0.
Empty array elements should be filled with 0.
Thus an interaction with the user may look like:
Please enter the number of digits in the number: 9
Please enter the digits in the number with the LEAST significant first: 4 0 3 3 2 2 1 1 6
NOTE: There is a slight change from the homework problem this no longer outputs the number
(however, you may not want to pull that part until you are about finished with your program).

PHASE 2 ADD AND OUTPUT


Integer variables have a maximum size. Sometimes in engineering you need numbers larger than
what the ints hold. This program creates two numbers as arrays of chars, then adds each digit of
the numbers together (i.e each array element to its corresponding element in the other array) and
if the total of the two digits is greater than 10 it carries the 1 to the addition of the next two digits.
If the two highest order digits have a carry to the next number (you are then out of array elements
and you have no place for that carry out to go) then an overflow message should be generated.
To achieve this algorithm, your main program should now create two char arrays of size 20 (20
should be a const ). Input numbers into both arrays with your InitNumber function from above.
The main program should then call an AddArrays function to add each element of those two
arrays together and store the value in a third total array which is also passed into the AddArrays
function. After the arrays are added the OutputNumber function should be used to output the
equation:
number1 + number2 = totalnumber

where number1, number2 and totalnumber are each printed from OutputNumber function (i.e.
you will have to call the OutputNumber function 3 times one for each of the arrays). The
output calls should be from the main program.
Therefore, you need to write two additional functions:
AddArrays is a function that should have three char arrays as parameters. The first two arrays
are those you have input. The third array will be empty on the call and should return the sum of
the first two arrays
So if array 1 had the chars
1 2 4
and array 2 had
2 3 6
the total array should contain (it MUST be chars)
3 5 0 1
Your program should also take into account for overflow from carry overs. For example if Array1
was
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
And array 2 was:
5
2
4
4

The output array would overflow because there are not 21 elements available to handle the high
order carry.
HINT: HOW TO CONVERT CHAR TO INT
If you remember the ASCII table the bit pattern for the ASCII char 0 is 00110000, the bit pattern
for the char 1 is 00110001. To convert 0 to the int 0 if you subtract 0 from 0 that performs
the subtraction 00110000 00110000 which leaves the bits as 00000000 which is the integer
value of 0. If I subtract 0 from 1 I have 00110001 00110000 which leaves me with 00000001
which is the int value of 1. Therefore, your logic to convert a char to an int is Array[i] 0.
If you subtract 0 to convert a char to an int what would you do to convert an int back to a char?
OutputNumber is a function that accepts an array as input and outputs the array in reverse order
(i.e. the number ) without the leading 0s for the number. Thus if the array passed in had
5
2
4
4
0
The output would be
4425

Sample Interaction User input is in blue


Please enter your first number
How many digits are in your number? 6
Please enter your number starting with the LEAST significant digit: 123456
Please enter your second number
How many digits are in your number? 7
Please enter your number starting with the LEAST significant digit: 1234567
654321 + 7654321 = 8308642
Another sample interaction User input is in blue
Please enter your first number
How many digits are in your number? 20
Please enter your number starting with the LEAST significant digit:
99999999999999999999
Please enter your second number
How many digits are in your number? 1
Please enter your number starting with the LEAST significant digit: 1
99999999999999999999 + 1 = ERROR:

Addition Overflow

PHASE 3 REPEAT THE PROCESS


This last phase is the repetition. Allow the user to process more additions. After the first
interaction ask the user if they would like to do another addition.
A sample interaction may be as (user input is in blue).
Please enter your first number
How many digits are in your number? 6
Please enter your number starting with the LEAST significant digit: 123456
Please enter your second number
How many digits are in your number? 7
Please enter your number starting with the LEAST significant digit: 1234567
654321 + 7654321 = 8308642
Would you like to add two more numbers (y/n)? y
Please enter your first number
How many digits are in your number? 20
Please enter your number starting with the LEAST significant digit:
99999999999999999999
Please enter your second number
How many digits are in your number? 1
Please enter your number starting with the LEAST significant digit: 1

99999999999999999999 + 1 = ERROR:
Would you like to add two more numbers (y/n)? n

Addition Overflow

Das könnte Ihnen auch gefallen