Please Nothing too advanced as this is a class for beginners
Please Nothing too advanced as this is a class for beginners.
In summary, you should take away these points from this lesson: Functions should be declared before main() and declared after main() and defined after main() When main() passes its variables to a function, it provides \"copies\" of its variables. Even if the called function modifies the variables, it is only modifying its own private copy, Variables of main() will remain unaffected Function can take as many inputs as you wish separated by a comma Function can only return one variable using the return statement Assignment Build Functions Build a function called calculate_percent() with the following properties: Takes 2 floating-point numbers called \"score\" and \"max_score\" Returns a floating point number which is percentage of score and max_score.\" Do not use a global variable, and do not use printf/scanf inside this function Build a function called getavg() that returns the average of three numbers Takes 3 floating-point numbers Returns the average of three numbers. Do not use a global variable, and do not use printf/scanf inside this function Build a function called getGradeLetter(): Takes a floating-point number containing the percentage Returns a char which is the letter grade of the percentage Do not use a global variable, and do not use printf/scanf inside this function Build a function called print_line() Takes a character that will be used to print a line Takes the number of times of print the character For example, if we call print_line(\'*\', 10)., it should print: Build and present a menu to a user like the following and enclose it into a loop that ends when the Quit option is chosen. Enter user name. Enter exam scores. Display average exam scores. Display summary. Quit Call the print_line() function to print a line before printing the menu. For choice l.scanf a string, and store it to a username char array. For choice 2. use 3 for loop to enter 3 exam scores {assuming exam scores 3re out of 100}. For choice 3. do the following: If the user has already entered exam scores, use the getAvg() function to get the average of the exam scores. If the user has not yet entered exam scores, display an error message similar to: \"Please use the menu to enter exam scores firs?\" For choice 4. if the user has not yet entered their name and exam scores, display an error message. Otherwise display the average, the letter grade of the average, and the user\'s name. Example: \"Hello Preet, your exam scores were 80, 90, and 100. Your average is 90.0 with letter grade: A\" Use the getAvg() and getGradeLetter()where possible. When the Quit option is chosen, end the primary loop that contains the menu.Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
#include <stdio.h>
double calculate_percent(double score, double max_score){
double percent = score/max_score * 100; //calulate the percentage
return percent;
}
double getAvg(double score1, double score2, double score3){
double avg = (score1 + score2 + score3)/3; //calculate the average of 3 scores
return avg;
}
char getGradeLetter(double score){ //get the grade of percentage
char g = \'F\';
if(score >= 90)
g = \'A\';
else if(score >= 80)
g = \'B\';
else if(score >= 60)
g = \'C\';
else if(score >= 40)
g = \'D\';
else
g = \'F\';
return g;
}
void print_line(char c, int n){ //print a line of with a character c, n times in a line
printf(\"\ \");
for(int i=0; i<n; i++)
printf(\"%c\",c);
printf(\"\ \");
}
int main()
{
int c;
char name[10];
double scores[3];
double avg = 0;
while(1) {
print_line(\'*\',20);
printf(\"\ 1.Enter username\ \"); //display menu
printf(\"2.Enter examscores\ \");
printf(\"3.Display average examscores\ \");
printf(\"4.Display summary\ \");
printf(\"5.Quit\ \");
scanf(\"%d\",&c);
if(c==1){
scanf(\"%s\",&name); //read user name if choice = 1
} else if(c==2){
for(int i=0; i<3; i++) { //read 3 scores if choice = 2
scanf(\"%lf\",&scores[i]);
}
} else if(c==3){
if(scores[0]==0) { //print error if scores is not entered, for the choice = 3
printf(\"Please use menu to enter examscores first.\");
}else{
avg = getAvg(scores[0],scores[1],scores[2]); //get the average
printf(\"Average = %lf\",avg); //print average
}
} else if(c==4){
char g = getGradeLetter(avg); //get the grade for the percentage
printf(\"Hello %s, your exam scores were %lf, %lf and %lf. Your average is %lf, and grade letter is %c\",name,scores[0],scores[1],scores[2],avg,g);
} else if(c==5) {
break;
}
}
return 0;
}
-------------------------------------------
OUTPUT:
********************
1.Enter username
2.Enter examscores
3.Display average examscores
4.Display summary
5.Quit
1
Preet
********************
1.Enter username
2.Enter examscores
3.Display average examscores
4.Display summary
5.Quit
2
80
90
100
********************
1.Enter username
2.Enter examscores
3.Display average examscores
4.Display summary
5.Quit
3
Average = 90.000000
********************
1.Enter username
2.Enter examscores
3.Display average examscores
4.Display summary
5.Quit
4
Hello Preet, your exam scores were 80.000000, 90.000000 and 100.000000. Your average is 90.000000, and grade letter is A
********************
1.Enter username
2.Enter examscores
3.Display average examscores
4.Display summary
5.Quit
5


