Programming Exercise 4
Programming Exercise 4
The output of your program should be: 2 4 6 8 10 What would be printed from each of the following program segments? Compare and contrast your answers to parts a, b and c. a x=12; . while(x>7) { printf(%d\n, x); x-=1; } b for(x=12; x>7; x-=2 ) . printf(%d\n, x); c x=12; . do { printf(%d\n, x); x-=3; } while(x>7); What is the output of the following statement? a for (x = 2; x <= 10; + b for (x = 20; x >= 0; x=x-4) . +x) . printf (%d , x); printf (%d\t, x+2); Determine the total number of items displayed. Also determine the first and last numbers printed. int num=0; while (num<=20) { num++; printf(%d , num); } Determine the value in total after each of the following loops is executed: a total=0; . for(i=1; i<=10; i+=1) total += 1; b total=1; . for(count=1; count<=10; count+=1) total = total*2; c total=0; . for(i=10; i<=15; i++) total += i;
3.
4.
5.
6.
Write a program based on the following grading criteria: Mark Letter Grade 80 100 A 60 79 B 50 59 C 40 49 D below 40 F Read integer marks ranging from 0 to 100, one at a time. Stop when the user enters 999. Calculate and display the number of students who have obtained A, B, C, D and F. Sample output:
Enter mark: 78 The corresponding grade is B ... ... Enter mark: 98 The corresponding grade is A Enter mark: 999 There are: 5 students who obtained A 5 students who obtained B ... ...
7.
Write a program that read in the student matric numbers and the marks for 4 quizzes of a group of students. The program is finished when the user enters a 0 for the student matric number. For each student, the program requests the user to enter the mark of 4 quizzes and it finds the average marks of each student. Sample output:
Enter student matric number: 65000 Enter marks: 20 Enter marks: 15 Enter marks: 10 Enter marks: 10 Average of student 65000 is 13.75 Enter student matric number: 65654 Enter marks: 10 Enter marks: 15 Enter marks: 20 Enter marks: 20 Average of student 65654 is 16.25
8.
For this year Mega Sale Carnival, SOGO has announced the following discounts on purchase of items based on department:
Purchase amount Department/Discount Children Ladies Men Departme Departme Department nt nt 2% 2.5% 5% 5% 7.5% 10.0% 7.5% 10.0% 12.5% 10.0% 12.5% 15.0%
Write a program to compute the discount and net amount for a customer. Sample output:
Enter department (C Children, L Ladies, M Men): W Invalid! Enter department (C Children, L Ladies, M Men): O Enter user purchase amount: 120.00 This user will get 5% discount. Discount amount : RM 6.00 Net amount to pay : RM 114.00
CASE STUDY 1: COMPUTING THE DEPRECIATION A machine purchased for RMXX,XXX is decreased in value (depreciated) at a rate of RMX,XXX a year for X years. Write and run a C program that computes and displays a depreciation table. Then check the final end-ofyear value and determine whether it is equal to, less than or greater than half of the actual price. Write an appropriate message. Sample output (the table should have the form as shown below):
The price of the machine (RM) The rate of depreciation (RM) Years of depreciation : 3000 : 120 :7 ACCUMULATED
YEAR DEPRECIATION END-OF-YEAR VALUE DEPRECIATION ------ ------------------------------------------------------------------------------------1 120 2880 2 120 2760 3 120 2640 4 120 2520 5 120 2400
6 7
120 120
2280 2160
720 840
The final end-of-year value (RM 2160) is greater than half of the actual price (RM 1500). Do you want to continue? Y-yes, N-no: N Thank you for using this system.
Remark Exceptional Excellent Very Good Good Fairly Good Satisfactory Quite Satisfactory
Every student is awarded a grade according to his performance in every examination conducted, with regards to every subject, which he has enrolled for in a semester. The letter grades scored have associated grade points with them in descending order (A: 4.00, A-: 3.67 for example). The Grade Point Average (GPA) is calculated by taking an average of the grade point scores in every subject. The user must key in number of subjects and the score of each subject, during his past semester. Next the grade point associated with each score is determined. The program will sum up all the grade points scored in each subject. Divide the sum by the total number of subjects, which he enrolled for. What the program gets after division is the students GPA. Sample output:
How many subjects? 3 Input subject 1 score: 89
Grade is A: Exceptional Input subject 2 score: 77 Grade is A-: Excellent Input subject 3 score: 67 Grade is B: Good The sum of grade points is 10.67 Your GPA is 3.56