Midterm Programming Assignment


This midterm consists of three programming problems. Turn in your code and the printout of your test runs for each problem. Your code has to be sufficiently tested. Your testing will be a factor in determining your grade. You may discuss the problems with each other, but sharing code is strictly prohibited (and will be ruthlessly punished).


P1.
Write the function divide that implements integer division using only addition and subtraction. Assume the prototype int divide (int, int) . Your program should work the same way as C++ integer division for positive integers. For 1 point of extra credit, it should behave the same way as C++ division for all integer inputs. Test your function by calling it from main() in various integer arithmetic expressions.


P2.
Write a program that inputs a positive integer and prints all of its prime divisors. If a prime divides the number several times, print that prime as many times, i.e. the product of all primes printed must equal the input number. After factoring a number, your program must ask for a new number and so on, until the number entered is negative, in which case the program exits.


P3.
Write a program that inputs a positive integer number and prints that number in word form. Your program must work correctly for numbers under one million. For 1 point of extra credit, make it work for numbers less than 2 billion.

Hints: (1) Use the switch statement (2) Remember that numbers between 10 and 20 require special treatment.

Examples:

 
> factor
18
18: 2 3 3
25
25: 5 5
13
13: 13
159
159: 3 53
720
720: 2 2 2 2 3 3 5
2048
2048: 2 2 2 2 2 2 2 2 2 2 2
-10
Bye!
 
> number
125
one hundred twenty-five.

12
twelve.

1200 
one thousand.
two hundred.

12000
twelve thousand.

12125
twelve thousand.
one hundred twenty-five.

0
zero.

59
fifty-nine.



Sergey Bratus
11/1/1998