Previous: 2.7 Summary
Up: 2 Basic Concepts
Next: 2.9 Problems
Previous Page: 2.7 Summary
Next Page: 2.9 Problems

2.8 Exercises

Given the following variables and their initializations:

int a, x, y, z;
     float b, u, v, w;

x = 10; y= 20; z = 30; u = 4.0; v = 10.0;

What are the values of the expressions in each of the following problems:

  1. (a)  a = x - y - z;
    (b)  a = x + y * z;
    (c)  a = z / y + y;
    (d)  a = x / y / z;
    (e)  a = x % y % z
  2. (a)  a = (int) (u / v);
    (b)  a = (int) (v / u);
    (c)  b = v - u;
    (d)  b = v / u / w;
  3. What are the results of the following mod operations:
    (a)   5  %  3
    (b)  -5  %  3
    (c)   5  % -3
    (d)  -5  % -3
  4. (a)  (x <= y && x >= z)
    (b)  (x <= y || x >= z)
    (c)  (x <= y && !(x >= z))
    (d)  (x = y && z > y)
    (e)  (x == y && z > y)
  5. Under what conditions are the following expressions True?
    (a)  (x = y && y = z)
    (b)  (x == y && y == z)
    (c)  (x == y || y == z)
    (d)  (x >= y && x <= z)
    (e)  (x > y && x < z)
  6. Make required corrections in the following code.

    (a)
         main()
         {    int n;
    

    scanf("%d", n); }

    (b) main() { float n;

    printf("%d", n); }

    (c)

    main() { int n1, n2;

    if (n1 = n2) printf("Equal\n"); else printf("Not equal\n"); }

  7. Find and correct errors in the following program that is supposed to read ten numbers and print them.
    main()
         {    int n, count;
    

    scanf("%d", &n); while (count < 10) { printf("%d\n", n); scanf("%d", &n); } }

  8. We wish to print integers from 1 through 10. Check if the following loop will do so correctly.
    i = 1;
         while (i < 10) {
              printf("%d\n", i);
              i =  i + 1;
         }
  9. Suppose a library fine for late books is: 10 cents for the first day, 15 cents per day thereafter. Assume that the number of late days is assigned to a variable late_days. Check if the following will compute the fine correctly.
    if (late_days == 1)
              fine = 0.10;
         else
              fine = late_days * 0.15;

tep@wiliki.eng.hawaii.edu
Tue Aug 16 14:01:55 HST 1994