Problems 2
  1. Theory.
    1. What is the option that instructs the gcc compiler to print out all warnings?
    2. Name all 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 the function prototype of the cimagl function? Hint: RTFM.
    6. If you use functions from complex.h, do you need to link your program with -lm option? Hint: man complex.
    7. What will the following piece of code print?
      int i=1; printf("%i\n",i++); printf("%i\n",++i);
      
      Explain.
    8. How about int i=1; printf("%i %i\n",i++,++i); ? Hint: compile with -Wall.
    9. Rewrite the loop while(condition)body using the for loop.
    10. Rewrite the loop for(init;cond;inc)body using the while loop.
    11. 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 the sum
        float sum_up = 1.0f + 1.0f/2 + 1.0f/3 + ... + 1.0f/(max-1);
        
        and another sum
        float sum_down = 1.0f/(max-1) + 1.0f/(max-2) + 1.0f/(max-3) + ...  +1.0f;
        
        with float type and using the for loop 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 and sum_down using double type and explain the result.
      5. Compile your code with -O3 option. Has the execution time changed? Hint: you can measure the execution time of a program by running it using time utility,
        time ./a.out
        
    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).