In: Computer Science
C language
Example: "Hello Charley, your test scores were 70, 75, 80,
85, 90, 95. Your average is 82.5 with letter grade: B."
Hint: You can print "Hello Charley, your test scores were
" first, and then use for loop to print thetest scores, followed by
"Your average is 90.0 with letter grade: A."
//header files
#include<stdio.h>
#include<stdlib.h>
//function to calculate and return average
float getAvg(float *a,int s)
{
//loop variable
int j;
//variable to hold sum of elements of array
float sum_elements=0;
//loop to go from 0 to array size s
for(j=0;j<s;j++)
{
//sum up the elements of array a
sum_elements=sum_elements+a[j];
}
//return the average which is sum of the elements/number of
elements in array a
return sum_elements/s;
}
//function to get input from user
float getScoreFromUser()
{
//score variable
float score;
//taking score as an input from user and storing in score
variable
printf("Enter score: ");
scanf("%f",&score);
//returning the score
return score;
}
//main function
int main()
{
//character array to hold username
char username[30];
//variable to store choice from menu
int c;
//variable to store number of exams
int n;
//flag for test scores used to check if the test scores are entered
or not
int flag_scores=0;
//flag for name to check if the name is entered or not
int flag_name=0;
//array to store test scores
float scores_array[20] ;
//variable to store average of test scores
float average;
//character to store grade as per the average
char ch;
//infinite loop
while(1)
{
//printing the menu as required
printf("***************************\n");
printf("Enter user name.\n");
printf("Enter scores.\n");
printf("Display average score.\n");
printf("Display summary.\n");
printf("Quit\n");
printf("***************************\n");
//taking choice from the user as an integer input
scanf("%d",&c);
//if c is 5 then
if(c==5)
{
//exit from the program
exit(0);
}
//if c is 1 then
else if(c==1)
{
//taking name as an input from user
printf("Enter name:");
scanf("%s",username);
//setting its flag to 1
flag_name=1;
}
//if c is 2
else if(c==2)
{
//infinite loop
while(1)
{
//asking for number of exams
printf("How many exams do you have?");
//taking number of exams as an input from user in n
scanf("%d",&n);
//if n is less than equal to 20 and greater than 1( as n has to be
positive) then
if(n<=20 && n>=1)
{
//come out of this loop
break;
}
//if n >20 then loop will be continue until user enters n value
less then 20 and greater than 1
}
//loop in which i is going from 0 to n
for(int i=0;i<n;i++)
{
//calling getScoreFromUser function and storing its returned value
in array scores_array at its index i
scores_array[i] = getScoreFromUser();
}
//setting its flag to 1
flag_scores=1;
}
//if c is 3
else if(c==3)
{
//if flag_scores is not 1 that means user has not yet entered test
scores
if(flag_scores==0)
printf("Please use the menu to enter test scores first.\n");//print
the error message
//otherwise
else
{
//print the average of test scores by calling getAvg and passing
array and its size to it
printf("Average of all test scores is
%.2f\n",getAvg(scores_array,n));
}
}
//if c is 4
else if(c==4)
{
//if user has entered both scores and name then
if(flag_scores==1 && flag_name==1)
{
//store the average in average variable
average=getAvg(scores_array,n);
//if average is in between 100 and 90
if(average<=100 && average>=90 )
ch='A';//set ch to A
//else if average is in between 80 and 90
else if (average<90 && average>=80 )
ch='B';//set ch to B
//else if average is in between 70 and 80
else if (average<80 && average>=70 )
ch='C';//set ch to C
//else if average is in between 60 and 70
else if (average<70 && average>=60 )
ch='D';//set ch to E
//else if average is in between 60 and 40
else if (average<60 && average>=40 )
ch='E';//set ch to E
//else if average is less than 40
else if (average<40)
ch='F';//set ch to F
//printing the name
printf("Hello %s,your test scores were ",username);
//printing the test scores
for(int i=0;i<n;i++)
{
//if i is the last index
if(i==n-1)
printf("%.2f. ",scores_array[i]);//print the array element at i
index and a "." after it with 2 digits after decimal place
//otherwise
else
printf("%.2f, ",scores_array[i]);//print element at i index and a
"," after it with 2 digits after decimal place
}
//printing the average with 2 digits after decimal place
printf("Your average is %.2f with letter grade:
%c.\n",average,ch);
}
//if test scores are entered but name is not entered
else if (flag_scores==1 && flag_name==0)
{
//print the appropriate message
printf("Please enter the name first\n");
}
//if test scores are not entered but name is entered by user
else if(flag_scores==0 && flag_name==1)
{
//print the appropriate message
printf("Please enter the test scores first\n");
}
//otherwise
else
{
//print the appropriate message
printf("Please enter the name and test scores first\n");
}
}
}//loop ends
return 0;
}//main ends