C-programming autumn 2010


Excercise 2

THU 16.9 9:15-12:00 B119 summary lecture and exercises

  1. Show the output produced by the following statements (justify your answer)
      int i = 3;
       int j = 7;
    
       while(j++, i +=3, i < j)
          printf("i = %d, j = %d\n", i, j);
    
       for(; ;)
          break;
    
       for(i = 1, j = 3; ;i++, j--)
          if(i == j) {
             printf("end: i = %d, j = %d\n", i, j);
             break;
          } else printf("within : i = %d, j = %d\n", i, j);
    
       i = (j = 5) +4;
    
       for(; i > j;) {
          j++;
          printf("%d\n", j);
       }
    
    
  2. Write a complete program that can be used to test your knowledge of multiplication; it keeps reading two integer values and then prompts you for the value of the product, and finally verifies your answer. Limit input values to those less than 20. Reading stops if 20 values are read, or if an incorrect value is entered. In the later case, the program should display a message. For both cases, prior to termination, the program displays the number of correct answers and the number of incorrect answers.
  3. Write a program that counts the monthly payments of a loan of 250 000 euros, which has been taken for 10 years and the loan is annuity loan. This means that the monthly sum of payment is the same. You can assume that the interest rate is the same during the whole loan period. The program should count what the monthly payment would be using different annual interest rate between 3% - 11% using a 1 % interval.
    The monthly payment can be counted using the following expression:

    	 monthly payment = r ^ n * p/ (1200 * (r^n -1)) *A,  
    	           where r = 1 + p/1200
    	                 p = annual interest rate (percent), 
    			 n = the number of months when 
                                 the loan is paid back,
    	                 A = the original loan amount  
    			 ^ means to the power of.
    
    To count the power you can use the library function double pow(double x, double y), from the math library (header file is math.h).

  4. Write a program that counts the average of the student's grades weighted by the credits. The study information of each student is in a file. Each line contains one course grade information.

     
    	course name   credits of the course       grade            date
    	
            C-ohjelmointi           4                   3            31052004     
    	                                                        
    	
    The program asks first the name of the file and prints after that the average of the student's grades weighted by the credits.
  5. Print 432.376372 into a field, whose width is 15, and use the following precisions: 1,2,3,4 and 5, so that the output is left-justified. How is right-justified output obtained?
    Print 456.892356 using the following formats:
    %f
    %.2f
    %2.f
    %10.4f
    %10.5f
    %-10.5f
    %010.5f
    %-+10.5f
    %+-10.4f
    %10.0f
    



.

Study also:
  1. Text files
  2. Macros
  3. Conditional compilation