CMPT 102 - Assignment 2

Out: Monday, Oct. 3, 2005
Due: Monday, Oct. 17, 2005 at 5:00pm
Weight: 10%
Total: 100 marks


Question 1 [20]

Write a program that analyzes a line of characters.


Question 2 [20]

Write a program that prints a solid triangle using asterisks.


Question 3 [20]

A proper divisor of a number is a divisor of the number which is not equal to itself. A pair of amicable numbers are two distinct numbers such that the sum of the proper divisors of any one of them is equal to the other. For example, (220, 284) is a pair of amicable numbers since

  • the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284;
  • and the proper divisors of 284 are 1, 2, 4, 71, and 142, of which the sum is 220.
Write a program to generate the first four pairs of amicable numbers.

  • The output of your program should consist of four lines. Each line should be a pair of amicable numbers with the smaller number listed first. For example, the first line of your program should be
    (220, 284)
  • Save your program in a file called amicable.c.
  • Run your program and save the results in a file called q3.out.

Question 4 [40]

Suppose we have a grades file which records the students' grades for all the assignments and exams of a course. Write a program to generate a file which contains the final grade for each student, the total number of students, and the average of the final grades of all students.

  • The grades file consists of two parts separated by a blank line.
    • The first part consists of five lines, and each of them contains two numbers: the first number is the total mark of an assignment or exam, and the second number is the weight of that assignment or exam in the final grade.
    • The second part consists of one line for each student, and that line contains his/her student number and grades for all the assignments and exams.
    • The following is an example of the grades file:
      50  10
      100 20
      100 20
      50  15
      120 35
      
      921510066  48  63  91  37  65  
      921050547  38  60  82  48  75  
      921970610  43  55  76  28  50  
      931580448  45  96  92  45  118  
      
    • You can assume the data in the grades file is correct, that is, you don't need to do error checking of the grades file.
  • The final grade of a student should be the sum of the following values for all the assignments and exams:
    grade/total_mark*weight
    (To ensure you get the correct result, you should use type float to store grades.) For example, for the above grades file, the final grade of the student 921510066 is:
    48/50*10 + 63/100*20 + 91/100*20 + 37/50*15 + 65/120*35 ≈ 70
  • The output file of your program should consist of two parts separated by a blank line.
    • The first part should consist of one line for each student, and that line contains his/her student number and final grade. To ensure a consistent format, you should use the following statement in your program:
      fprintf(fp, "%d %3d\n", studentno, (int)finalgrade);
    • Of course, you can choose your own variable names.
    • The second part should consist of two lines: the first line shows the total number of students, and the second line shows the average of the final grades of all students. To ensure a consistent format, you should use the following statements in your program:
      fprintf(fp, "Total: %d\n", total);
      fprintf(fp, "Average: %d\n", (int)average);
      
    • The following should be the output of your program for the above grades file:
      921510066   70  
      921050547   72  
      921970610   57  
      931580448   94  
      
      Total: 4
      Average: 73  
      
  • Save your program in a file called grades.c
  • Download the sample grades file and save it in a file called grades.dat. Test your program using this file. Call the output file of your program finalgrades.dat.

How to submit

Electronic submission

You should submit the files count.c, q1.out, triangle.c, q2.out, amicable.c, q3.out, grades.c, finalgrades.dat. You can do this by typing these commands:
tar cvf a2.tar count.c  q1.out triangle.c q2.out amicable.c q3.out grades.c finalgrades.dat
gzip a2.tar
Then submit the file a2.tar.gz

Hard-copy submission

You should submit the printout of the files you submit electronically. Also, don't forget to print out a copy of the sample cover page, fill it out, and sign it.