Programming in C

(Barry) #1
Exercises 117

than 1 or greater than 75 ), the program displays a message and returns a value of 1.
Executing the returnstatement at that point in the program causes the program to ter-
minate immediately, and no further statements are executed. As noted in Chapter 3,
“Compiling and Running Your First Program,” the nonzero value that is returned indi-
cates by convention that the program terminated with an error condition, and that fact
could be tested by another program if desired.
After the number has been entered by the user, you see the statement


unsigned long long int Fibonacci[numFibs];


The Fibonacci array is declared to contain numFibselements.This is called a variable
length array because the size of the array is specified by a variable and not by a constant
expression. Also, as previously noted, a variable can be declared anywhere in a program,
as long as the declaration occurs before the variable is first used. So although this decla-
ration appears out of place, it’s perfectly legitimate. It’s not usually considered good
programming style to do this, however, mainly because, by convention, the variable
declarations are often grouped together so someone reading the program can see the
variables and their types in one place.
Because Fibonacci numbers get large very quickly, the array is declared to contain the
largest positive integer value you can specify, namely an unsigned long long int.As
an exercise, you might want to determine the largest Fibonacci number that you can
store inside an unsigned long long int variable on your computer.
The rest of the program is self-explanatory:The requested number of Fibonacci num-
bers are calculated and then displayed to the user.The execution of the program is then
complete.
A technique known as dynamic memory allocationis also often used to allocate space for
arrays while a program is executing.This involves using functions such as mallocand
callocthat are in the standard C library.This topic is discussed in detail in Chapter 17,
“Miscellaneous and Advanced Features.”
You have seen how arrays are powerful constructs that are available in virtually all
programming languages. A program example showing the use of multidimensional arrays
is deferred to Chapter 8, which begins a detailed discussion of one of the most impor-
tant concepts in the C language—the program function. Before proceeding to that chap-
ter, however, try to work the following exercises.


Exercises



  1. Type in and run the eight programs presented in this chapter. Compare the output
    produced by each program with the output presented after each program in the
    text.

  2. Modify Program 7.1 so that the elements of the array valuesare initially set to 0.
    Use a forloop to perform the initialization.

  3. Program 7.2 permits only 20 responses to be entered. Modify that program so that
    any number of responses can be entered. So that the user does not have to count

Free download pdf