-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCase_Study2.c
68 lines (57 loc) · 1.92 KB
/
Case_Study2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/////// Case Study 2 ///////
/////// Sentinel-Controlled Repetition ///////
/////// Pseudocode ///////
// 1 Initialize total to zero
// 2 Initialize counter to zero
// 3
// 4 Input the first grade
// 5 While the user has not as yet entered the sentinel
// 6 Add this grade into the running total
// 7 Add one to the grade counter
// 8 Input the next grade (possibly the sentinel)
// 9
// 10 If the counter is not equal to zero
// 11 Set the average to the total divided by the counter
// 12 Print the average
// 13 else
// 14 Print “No grades were entered”
#include <stdio.h>
// Function main begins program execution
int main(void)
{
unsigned int counter; // Number of grades entered
int grade; // Grade value
int total; // Sum of grades
float average; // Number with a decimal point for average
// Initialization phase
total = 0; // Initialize total
counter = 0; // Initialize counter
// Processing phase
// Get the first grade from the user
printf("Enter the first grade (or sentinel to end): ");
scanf("%d", &grade);
// Loop while sentinel value not yet read from the user
while (grade != -1)
{
total = total + grade; // Add grade to total
counter = counter + 1; // Increment counter
// Get the next grade from the user
printf("Enter the next grade (or sentinel to end): ");
scanf("%d", &grade);
} // end while
// Termination phase
// If the user entered at least one grade
if (counter != 0)
{
// Calculate the average of all grades entered
average = (float)total / counter; // Avoid truncation
// Display average with two digits of precision
printf("Class average is %.2f\n", average);
} // end if
else
{
// If no grades were entered, output a message
puts("No grades were entered");
} // end else
return 0;
} // end function main