C-programming autumn 2009


Excercise 2

MON 21.9 12:15-15:00 BK106

  1. Show the output from the following statement and justify your answer assuming that the input was 3 and the read operation was successful.
    int i;
    
    switch(scanf("%d", &i)) {
    
    case EOF: printf("see %d\n", i);
              break;
    
    case 1: printf("\tyou");
    
    case 0: printf("\tlater");
              break;
    
    default: printf("printed %d\ntimes\n", i);
    }
    
  2. Write a program to find the area of rectangle. This program prompt the user to enter the values of the both sides, perform basic error checking, and output the area. If the inout is incorrect, the program should output a message and exit.
  3. Write a program that counts the monthly payments of a loan of 210 000 euros, which has been taken for 20 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) Use gcc -lm ...

  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 785.786544 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 766.753491 using the following formats:
    %f
    %.2f
    %2.f
    %10.4f
    %10.5f
    %-10.5f
    %010.5f
    %-+10.5f
    %+-10.4f
    %10.0f
    



.