20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

112 Chapter 7 Work<strong>in</strong>g with Arrays<br />

As an example of the const attribute, the l<strong>in</strong>e<br />

const double pi = 3.141592654;<br />

declares the const variable pi.This tells the compiler that this variable will not be modified<br />

by the program. If you subsequently wrote a l<strong>in</strong>e like this <strong>in</strong> your program:<br />

pi = pi / 2;<br />

the gcc compiler would give you a warn<strong>in</strong>g message similar to this:<br />

foo.c:16: warn<strong>in</strong>g: assignment of read-only variable 'pi'<br />

Return<strong>in</strong>g to Program 7.7, the character array baseDigits is set up to conta<strong>in</strong> the 16<br />

possible digits that will be displayed for the converted number. It is declared as a const<br />

array because its contents will not be changed after it is <strong>in</strong>itialized. Note that this fact<br />

also aids <strong>in</strong> the program’s readability.<br />

The array convertedNumber is def<strong>in</strong>ed to conta<strong>in</strong> a maximum of 64 digits, which<br />

holds the results of convert<strong>in</strong>g the largest possible long <strong>in</strong>teger to the smallest possible<br />

base (base 2) on just about all mach<strong>in</strong>es.The variable numberToConvert is def<strong>in</strong>ed to be<br />

of type long <strong>in</strong>t so that relatively large numbers can be converted if desired. F<strong>in</strong>ally, the<br />

variables base (to conta<strong>in</strong> the desired conversion base) and <strong>in</strong>dex (to <strong>in</strong>dex <strong>in</strong>to the<br />

convertedNumber array) are both def<strong>in</strong>ed to be of type <strong>in</strong>t.<br />

After the user keys <strong>in</strong> the values of the number to be converted and the base—note<br />

that the scanf call to read <strong>in</strong> a long <strong>in</strong>teger value takes the format characters %ld—the<br />

program then enters a do loop to perform the conversion.The do was chosen so that at<br />

least one digit appears <strong>in</strong> the convertedNumber array even if the user keys <strong>in</strong> the number<br />

zero to be converted.<br />

Inside the loop, the numberToConvert modulo the base is computed to determ<strong>in</strong>e<br />

the next digit.This digit is stored <strong>in</strong>side the convertedNumber array, and the <strong>in</strong>dex <strong>in</strong><br />

the array is <strong>in</strong>cremented by 1. After divid<strong>in</strong>g the numberToConvert by the base, the conditions<br />

of the do loop are checked. If the value of numberToConvert is 0, the loop term<strong>in</strong>ates;<br />

otherwise, the loop is repeated to determ<strong>in</strong>e the next digit of the converted<br />

number.<br />

When the do loop is complete, the value of the variable <strong>in</strong>dex is the number of digits<br />

<strong>in</strong> the converted number. Because this variable is <strong>in</strong>cremented one time too many <strong>in</strong>side<br />

the do loop, its value is <strong>in</strong>itially decremented by 1 <strong>in</strong> the for loop.The purpose of this<br />

for loop is to display the converted number at the term<strong>in</strong>al.The for loop sequences<br />

through the convertedNumber array <strong>in</strong> reverse sequence to display the digits <strong>in</strong> the correct<br />

order.<br />

Each digit from the convertedNumber array is <strong>in</strong> turn assigned to the variable<br />

nextDigit.For the numbers 10 through 15 to be correctly displayed us<strong>in</strong>g the letters A<br />

through F, a lookup is then made <strong>in</strong>side the array baseDigits, us<strong>in</strong>g the value of<br />

nextDigit as the <strong>in</strong>dex. For the digits 0 through 9, the correspond<strong>in</strong>g location <strong>in</strong> the<br />

array baseDigits conta<strong>in</strong>s noth<strong>in</strong>g more than the characters '0' through '9' (which as<br />

you recall are dist<strong>in</strong>ct from the <strong>in</strong>tegers 0 through 9). Locations 10 through 15 of the<br />

array conta<strong>in</strong> the characters 'A' through 'F'.So, if the value of nextDigit is 10, for

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!