Programming in C

(Barry) #1

154 Chapter 8 Working with Functions


const char baseDigits[16] =
{ '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int nextDigit;

printf ("Converted number = ");

for (--digit; digit >= 0; --digit ) {
nextDigit = convertedNumber[digit];
printf ("%c", baseDigits[nextDigit]);
}

printf ("\n");
}

int main (void)
{
void getNumberAndBase (void), convertNumber (void),
displayConvertedNumber (void);

getNumberAndBase ();
convertNumber ();
displayConvertedNumber ();

return 0;
}

Program 8.14 Output
Number to be converted? 100
Base? 8
Converted number = 144

Program 8.14 Output (Rerun)
Number to be converted? 1983
Base? 0
Bad base - must be between 2 and 16
Converted number = 1983

Notice how the wise choice of function names makes the operation of Program 8.14
clear. Spelled out directly in the mainroutine is the function of the program: to get a
number and a base, convert the number, and then display the converted number.The

Program 8.14 Continued
Free download pdf