Hints for the 3rd midterm problem.
1. First write a function that prints out numbers with three
digits or less.
Once you have it, break your number into parts, each no longer
than three digits. Say, 23,570 breaks into 23 and 570,
23,070 breaks into 23 and 70 etc.
These parts can be processed independently. After you process
the first part, you only have to print "thousand." and
process the second part.
2. When processing a three digit number, you can print the number
of hundreds (if any) without looking at the last two digits.
3. When printing the number represented by the last two digits of
a three digit number, you have to check if that number is above
or below 20 (numbers below 20 have irregular names).
4. Test your program extensively.
For some tricks you might use, look at the following program that
counts the number of digits in a number, and then prints all of
the digits one by one. IMPORTANT: Do not try to use this code as
a model -- it solves a very different problem! It just shows you
how you can count the digits in a number, etc. For best results,
step through this code, with pencil and paper, for n = 2450 .
#include
#include
int main(){
int n;
cin >> n;
// find out how many digits n contains, and print that number
int count = 0; // digit counter
int m = n; // make a working copy of n
while( m != 0 ){ // let's divide m by 10 and see how many times
count++; // we can do it before we get a zero
m = m/10;
}
cout << "The number " << n << " has " << count << " digits.\n";
// now print these digits, one by one
// let us first prepare an auxiliary variable
int aux = 1;
for( int i=0; i < count-1 ; i++) aux *= 10;
cout << "aux = " << aux << endl;
string digits[10] = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine" };
int current_digit;
m = n; // again, make a working copy of our number
for( int i=0; i < count; i++){
current_digit = m/aux; // the leading digit of m
cout << digits[ current_digit ] << endl;
m = m % aux; // prepare for the next iteration (i.e. next digit)
aux = aux/10;
}
return 0;
}