Averaging Score
A
class of 20 students took a quiz; the grades ranging from 0 to 100 for the quiz
are made available to you,
You
are asked to write a program to calculate the average on the quiz
Hints
Hints
The
Class average is equal to the sum of the grades divided by the number of
students. The algorithm for solving this problem on a computer must input each
grade, keep track of the total of all grades input, perform the averaging
calculation and print the result.
First
of all we will need to use the pseudo code to list the actions to execute and
specify the order in which these actions should execute.
Pseudo
code is an artificial and informal language that helps you develop algorithms. It’s
useful for developing algorithms that will be converted to structured C
programs. They are not executed on computers; rather, they merely help you “think
out” a program before attempting to write it in a programming language such as
C. pseudo code consists purely consists purely of characters
Pseudo
code statement
Set
total to zero
Set
grade counter to one
While
grade counter is less than or equal to twenty
Input the next grade
Add the grade into the total
Add one to the grade counter
Set
the class average to the total divided by ten
Print
the class average
SOLUTION
/*Averaging Score in a class */
#include<stdio.h>
/*function
main begins program execution */
int
main(void)
{
int counter; /*no of grade to be entered */
int grade; /*grade value */
int sum; /*sum of grades input by user */
int average; /*average of grades */
/*Initialization phase */
sum =0; /*Initializing total */
counter=1; /*Initialize loop counter */
/*Processing phase */
while(counter<=20){ /*the program loops
20 times */
printf("Enter grade:");
/*Prompting for input */
scanf("%d",&grade); /*reading
grade from user */
sum =sum +grade; /*Adding grade to
total */
counter = counter +1; /*Incrementing
counter */
}
/*Terminating phase */
average= sum /20; /*integer division */
printf("Class Average is:%d",
average); /*displaying result */
return 0;
}
Explanation of the program
The first line (Averaging Score in a class) in the solution is called a comment. insert comments to document programs to improve readability. the comment simply describes the purpose of the program.
The second line (#include<stdio.h>) is a directive to the C prepossesor. lines beginning with # are prepossessed by the prepossessor before being complied.
(Stdio.h means Standard input/Output Header) and its always included in every C programs.
(int main (void)) is a part of every C-programs the parentheses after main indicates that main is a program building block called a function
({ }) Begins the body of every function and closes the body of every function
(int sum, int average, int counter, int total)- are definitions, the name sum, average, total, counter, are names of variables. A variable is a location in memory where a value can be stored for use by a program.
A variable name in C is any valid identifier. every variable has a name, a type and a value.
(printf)-This instructs the computer to perform an action, the entire line, including printf, it's argument within the parentheses and the semicolon (;) is called a statement (;) - Statement terminator (%d)- format control string, indicates the type of data that would be outputed. and its also indicates the data should be an integer.
(scanf)- we use "scanf" to obtain value from the user, it's reads from the Standard input, which is usually the keyboard
This statements: while(counter<=20){ indicates that the while statement should continue iterating (also called looping) as long as the value of gradeCounter is less than or equal to 20. while this condition remains true, the while statement executes the statements between the braces that delimit its body.
(average= sum /20;)This calculates the average of variables and assigns the result to variable average using the assignment operator (=)
(return 0) indicates that program ended successfully.
Comments
Post a Comment
Please feel free to comments if you have any ideas, questions and subjection concerning our post and programs