Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Exercises

  1. Write a function that returns the average of two floating-point numbers. Write a small program to test your function and log the output. Next, put the function in a separate source file but “forget” to declare the function in the file that has your main routine. What happens? Now add the function declaration to the file with your main program and verify that the declaration fixes the problem.
  2. Write another averaging function, but this time try to pass the result back in one of the function’s arguments. Your function should be declared like this:

    void average( float a, float b, float average )

    Write a small test program and verify that your function doesn’t work. You can’t affect a variable in the calling context by setting the value of a function parameter.

    Now change the function and its call to pass a pointer to a variable in the calling context. Verify that the function can use the pointer to modify a variable in the calling context.

  3. Assume that you have a function, int flipCoin(), that randomly returns a 1 to represent heads or a 0 to represent tails. Explain how the following code fragment works:
    int flipResult;
    if ( flipResult = flipCoin() )
      printf( "Heads is represented by %d\n", flipResult );
    else
      printf( "Tails is represented by %d\n", flipResult );

    As you will see in Chapter 6, “Classes and Objects,” an if condition similar to the one in the preceding example is used in the course of initializing an Objective-C object.

  4. An identity matrix is a square array of numbers with ones on the diagonal (the elements where the row number equals the column number) and zero everywhere else. The 2×2 identity matrix looks like this:

    040fig01.jpg

    Write a program that calculates and stores the 4×4 identity matrix. When your program is finished calculating the matrix, it should output the result as a nicely formatted square array.

  5. Fibonacci numbers (http://en.wikipedia.org/wiki/Fibonacci_number) are a numerical sequence that appears in many places in nature and in mathematics. The first two Fibonacci numbers are defined to be 0 and 1. The nth Fibonacci number is the sum of the previous two Fibonacci numbers:

    Fn = Fn–1 + Fn–2

    Write a program that calculates and stores the first 20 Fibonacci numbers. After calculating the numbers, your program should output them, one on a line, along with their index. The output lines should be something like this:

    Fibonacci Number 2 is: 1

    Use a #define to control the number of Fibonacci numbers your program produces, so that it can be easily changed.

  6. Rewrite your program from the previous exercise to use a while loop instead of a for loop.
  7. What if you are asked to calculate the first 75 Fibonacci numbers? If you are using ints to store the numbers, there is a problem. You will find that the 47th Fibonacci number is too big to fit in an int. How can you fix this?
  8. Judging by the number of tip calculators available in the iOS App Store, a substantial fragment of the population has forgotten how to multiply. Help out those who can’t multiply but can’t afford an iPhone. Write a program that calculates a 15% tip on all check amounts between $10 and $50. (For brevity, go by $0.50 increments.) Show both the check amount and the tip.
  9. Now make the tip calculator look more professional. Add a column for 20% tips (Objective-C programmers eat in classy joints). Place the proper headers on each column and use a pair of nested loops so that you can output a blank line after every $10 increment.

    Using the conversion specification %.2f instead of %f will limit the check and tip output to two decimal places. Using %% in the format string will cause printf to output a single % character.

  10. Define a structure that holds a rectangle. Do this by defining a structure that holds the coordinates of a point and another structure that represents a size by holding a width and a height. Your rectangle structure should have a point that represents the lower-left corner of the rectangle and a size. (The Cocoa frameworks define structures like these, but make your own for now.)
  11. One of the basic tenets of efficient computer graphics is “Don’t draw if you don’t have to draw.” Graphics programs commonly keep a bounding rectangle for each graphic object. When it is time to draw the graphic on the screen, the program compares the graphic’s bounding rectangle with a rectangle representing the window. If there is no overlap between the rectangles, the program can skip trying to draw the graphic. Overall, this is usually a win; comparing rectangles is much cheaper than drawing graphics.

    Write a function that takes two rectangle structure arguments. (Use the structures that you defined in the previous exercise.) Your function should return 1 if there is a non-zero overlap between the two rectangles, and 0 otherwise. Write a test program that creates some rectangles and verify that your function works.

  • + Share This
  • 🔖 Save To Your Account