Page 78 - Programming Microcontrollers in C
P. 78

Summary        63

                          EXERCISES
                          1. Write a function to calculate the Fibonacci number for 10, 20, 30,
                            and 40.

                          2. Devise a means for determining the number of times the fib func­
                            tion is called in the above program. What is this number for
                            fib(20)?

                          3. A separate problem from the number of times the function is called
                            is the number of times the function is called without exiting through
                            the bottom of the function. This term is called the depth of the
                            function. Determine the maximum depth of the fib() function
                            in calculating fib(20).

                          4. Repeat problem 1, but rewrite the Fibonacci number function so
                            that it does not employ recursion. How does the time to execute
                            this version of the fib(30) compared to that above?


            Summary

                              The basics of writing programs in C have been discussed in this
                          chapter. Several important concepts have been skipped over in this
                          presentation and will be covered in Chapter 2.
                              If you have not done so, it is recommended that you enter and
                          compile each example shown. These programs will all compile and
                          run under the MIX PowerC Compiler, the Cosmic compiler for the
                          M68HC11, the M68HC16, and the M68300 series of chips. They
                          also compile on the DIAB MCORE compiler. With the exception of
                          the MIX PowerC compiler, all of the compilers listed are cross com­
                          pilers that run on a PC platform, but compile code for another
                          computer.
                              The ANSI version of the language is the current standard, and
                          none of the classical C constructs have been introduced in this text. It
                          is not to the programmer’s advantage to use the classical version of
                          the language, even though programs that conform to classical C will
                          compile on an ANSI compliant compiler. Any version of a C++ com­
                          piler structured to compile C code will also compile ANSI C code.
                          The DIAB compiler listed above is a C/C++ compiler.
   73   74   75   76   77   78   79   80   81   82   83