# 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.

• Your program will ask the user to enter a line of characters. To do so, your program should print the following:
Enter a line of characters:
• Your program will count the number of letters (a-z, A-Z), the number of digits, and the number of other characters in the line. The output of your program should look like this:
letters: 15, digits: 10, others: 20
Your program should print the numbers using a minimum field width of 2.
• Save your program in a file called count.c.
• Test your program with three different lines of characters and save your testing results in a file called q1.out.

## Question 2 [20]

Write a program that prints a solid triangle using asterisks.

• Your program will ask the user to enter an integer between 1 and 20. To do so, your program should repeatedly print the following until the user enters an integer within the given range:
Enter an integer n such that 1<=n<=20:
Don't print a newline after the colon symbol so that the user will enter an integer on the same line. You can assume that the user always enters an integer.
• Your program should print a solid triangle using asterisks. For example, if n=7, then your program should print the following:
```      *
***
*****
*******
*********
***********
*************
```
In general, there are n lines, and for 1<=i<=n, the ith line is made of (n-i) spaces followed by (2i-1) asterisks.
• Save your program in a file called triangle.c.
• Test your program with three different cases and save your testing results in a file called q2.out. In at least one of the three cases, you should first enter several integers out of the given range.

## 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:
(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:
• 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
```

## 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.