Problems 2
  1. Theory.
    1. What is the option that instructs the gcc compiler to print out all warnings?
    2. Name the comparison operators in C.
    3. What does this piece of code do: (a>b?a:b) ?
    4. In your terminal, how can you read the manual page for the creall function?
    5. What is your shell?
      Hints: try
      • echo $0
      • echo $SHELL
      • ps --pid "$$"
    6. If you use mathematical functions from complex.h, do you need to link your program with -lm option? Hint: man complex.
    7. What does the following shell command do?
      apropos complex | grep function
      
      Hint: you might need to sudo apt-get install manpages-posix manpages-posix-dev; the bar in the command is the pipeline; grep searches for given text.
    8. What will the following piece of code print?
      int i=1; printf("%i\n",i++); printf("%i\n",++i);
      
      Explain.
    9. How about int i=1; printf("%i %i\n",i++,++i); ? Hint: compile with -Wall.
    10. Rewrite the loop while(condition)body using the for loop.
    11. Rewrite the loop for(init;cond;inc)body using the while loop.
    12. Rewrite the loop do body while(condition); using the for loop.
  2. Practice.
    1. The header file limits.h and float.h define certain useful limits.
      1. The maximum representable integer is the largest integer i for which i+1>i holds true. Now, using the while loop determine your maximum integer and compare it with the value INT_MAX defined in limits.h. Hint: something like
        int i=1; while(i+1>i) {i++;}
        
        Now do the same with the for loop and do while loop. It may take about a dozen seconds to calculate depending on your hardware.
      2. The minimum representable integer is the most negative integer i for which i-1<i holds true. Now, using the while loop determine your minimum integer and compare with the value INT_MIN defined in limits.h. Do the same using the for loop and do while loop.
      3. The machine epsilon is the difference between 1.0 and the next representable floating point number. Using the while loop calculate the machine epsilon for types float, double, and long double, and compare with the values FLT_EPSILON, DBL_EPSILON, and LDBL_EPSILON defined in float.h. Hint:
        double x=1; while(1+x!=1){x/=2;} x*=2;
        
        Do the same using the do while loop and for loop. Hint:
        double e; for(e=1; 1+e!=1; e/=2){} e*=2;
        
        Remember to use %Lg format placeholder for long double numbers.
    2. Define int max=INT_MAX/2; (or, say, INT_MAX/3, if the execution time is longer than you are willing to wait)
      1. Calculate (using iteration statements) the sum
        float sum_up_float = 1.0f + 1.0f/2 + 1.0f/3 + ... + 1.0f/(max-1);
        
        and another sum
        float sum_down_float = 1.0f/(max-1) + 1.0f/(max-2) + 1.0f/(max-3) + ...  +1.0f;
        
        with float type and compare the two sums.
      2. Explain the difference.
      3. Does this sum converge as function of max?
      4. Now calculate the sums sum_up_double and sum_down_double using double type. Explain the result.
    3. Write a function of type void (that is, it does not return anything) with one argument of type int, which in the case when the argument is 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 prints out the name of the digit, correspondingly "zero", "one", "two", ... , "nine"; and prints "not a digit" in the case of other arguments. Use the switch construction. The signature of the function is void name_digit(int i).