O'Reilly logo

Beginning Mac OS® X Programming by Drew McCormack, Michael Trent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.8. Exercises

  1. Look up the man page for the functions in the following table:

    Function
    printf
    scanf
    pow
  2. The Calculator program can only perform integer math. Extend Calculator to do double-precision floating-point computations. These double-precision values will use the double data type, rather than the int data type. For example, you need to change the calculate function to accept and return doubles instead of ints. Feel free to skip ahead to Chapter 6 if you want to learn more about these data types. But this is mostly an opportunity to practice using Xcode to build and run a program.

    The "3 printf" and "3 scanf" man pages tell you how to read and write double-precision numbers to standard I/O. Make sure the decimal point is printed only when necessary:

    Enter an expression: 1 + 2
    1 + 2 = 3
    
    Enter an expression: 1 + 2.1
    1 + 2.1 = 3.1
  3. Common mathematical functions and operators appear in the following table. Extend your Calculator changes in Exercise 2 to incorporate these new operators.

    NameKeyFunction/OperatorExample
    Divide"/"/x = y / z;
    Integer Divide"\"/x = (int)y / (int)z;
    Modulo"%"%x = (int)y % (int)z;
    Power"^"pow()x = pow(y, z);

    You need to add the following line of code near the top of Calculate.c, along with the other include statements:

    #include <math.h>

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required