Modular Program for Calculating Grades
This program demonstrates a modular approach to calculating letter grades based on user input scores. It allows users to input scores and calculates the corresponding letter grades using the GetGrade function. The program terminates when the user enters -999, and it handles invalid inputs. The modular design enhances code reusability and readability in the program.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
FUNCTIONS EXAMPLES
1. EXAMPLE (1) Write a complete modular program that accepts a score in the main program, then calls the function GetGrade that calculates the corresponding letter grade. The program should stop if the user enters -999. The function should consider invalid scores. #include <stdio.h> char GetGrade (double score); int main (void) { double score; char grade; do { printf ( Enter student s score> ); scanf ( %f , &score); if (score == -999.0) break; grade = GetGrade (score); printf ( Grade = %c , grade); } while (score != -999.0); } // end main Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 2 2
1. EXAMPLE (1) CONTD char GetGrade (double score) { char grd; if (score >= 0.0) && (score < 60.0) else if (score >= 60.0) && (score < 70.0) else if (score >= 70.0) && (score < 80.0) else if (score >= 80.0) && (score < 90.0) else if (score >= 90.0) && (score < 100.0) grd= A ; else grd= X ; return (grd); } // end GetGrade grd= F ; grd= D ; grd= C ; grd= B ; Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 3 3
1. EXAMPLE (1) CONTD #include <stdio.h> char GetGrade (double score); int main (void) { double score; char grade; do { printf ( Enter student s score> ); scanf ( %f , &score); if (score == -999.0) break; grade = GetGrade (score); if (grade != X ) printf ( Grade = %c , grade); else printf ( Invalid Input ); } while (score != -999.0); } // end main Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 4 4
1. EXAMPLE (1) THE WHOLE PROGRAM #include <stdio.h> char GetGrade (double score); int main (void) { double score; char grade; do { printf ( Enter student s score> ); scanf ( %f , &score); if (score == -999.0) grade = GetGrade (score); if (grade != X ) else } while (score != -999.0); } // end main break; printf ( Grade = %c , grade); printf ( Invalid Input ); char GetGrade (double score) { char grd; if (score >= 0.0) && (score < 60.0) else if (score >= 60.0) && (score < 70.0) else if (score >= 70.0) && (score < 80.0) else if (score >= 80.0) && (score < 90.0) else if (score >= 90.0) && (score < 100.0) grd= A ; else grd= X ; return (grd); } // end GetGrade grd= D ; grd= F ; grd= C ; grd= B ; Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 5 5
2. EXAMPLE (2) Write a complete modular program that accepts a score in the main program, then calls the function GetGrade that calculates the corresponding letter grade. The program should stop if the user enters -999. The user is not allowed to enter values less than 0 or greater than 100. #include <stdio.h> char GetGrade (double score); int main (void) { double score; char grade; do { score = -1.0; while (score < 0.0) || (score > 100.0) { printf ( Enter student s score> ); scanf ( %f , &score); } // end while (score < 0.0 . if (score == -999.0) break; grade = GetGrade (score); } while (score != -999.0); } // end main Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 6 6
2. EXAMPLE (2) CONTD char GetGrade (double score) { char grd; if (score >= 0.0) && (score < 60.0) else if (score >= 60.0) && (score < 70.0) else if (score >= 70.0) && (score < 80.0) else if (score >= 80.0) && (score < 90.0) else if (score >= 90.0) && (score < 100.0) grd= A ; else grd= X ; return (grd); } grd= F ; grd= D ; grd= C ; grd= B ; Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 7 7
2. EXAMPLE (2) THE WHOLE PROGRAM #include <stdio.h> char GetGrade (double score); int main (void) { double score; char grade; do { score = -1.0; while (score < 0.0) || (score > 100.0) { printf ( Enter student s score> ); scanf ( %f , &score); } // end while (score < 0.0 . if (score == -999.0) break; grade = GetGrade (score); } while (score != -999.0); } // end main char GetGrade (double score) { char grd; if (score < 60.0) else if (score >= 60.0) && (score < 70.0) else if (score >= 70.0) && (score < 80.0) else if (score >= 80.0) && (score < 90.0) else return (grd); } // end GetGrade grd= D ; grd= F ; grd= C ; grd= B ; grd= A ; Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 8 8
3. EXAMPLE (3) Write a complete modular program that accepts a score in the main program, then calls the function GetGrade that calculates the corresponding letter grade. The program should stop if the user enters -999. The user is not allowed to enter values less than 0 or greater than 100. The program should then count the number of students in each grade. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 9 9
3. EXAMPLE (3) THE WHOLE PROGRAM #include <stdio.h> char GetGrade (double score); int main (void) { int Acount=0, Bcount=0, Ccount=0, Dcount=0, Fcount=0; //declare & initialize counters double score; char grade; do { score = -1.0; while (score < 0.0) || (score > 100.0) { printf ( Enter student s score> ); scanf ( %f , &score); } // end while (score < 0.0 . if (score == -999.0) break; grade = GetGrade (score); switch (grade) { case A : Acount++; break; case B : Bcount++; break; case C : Ccount++; break; case D : Dcount++; break; case F : Fcount++; break; } //end switch } while (score != -999.0); printf ( Acount= %d, Bcount= %d, Ccount= %d, Dcount= %d, Fcount= %d , Acount, Bcount, Ccount, Dcount, Fcount); } // end main char GetGrade (double score) { ---- (refer to previous slides) return (grd); } // end GetGrade Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 10 10
4. EXAMPLE (4) Write a complete modular program that accepts an integer number n and calculates its factorial in the function factorial. The user is not allowed to enter negative numbers. #include <stdio.h> int factorial (int n); int main (void) { int result, number = -1; while (number < 0) { printf ( Enter an integer positive number> ); scanf ( %d , number); } // end while (number < 0) result = factorial (number); printf ( Factorial of %d = %d , number, result); } // end main Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 11 11
4. EXAMPLE (4) CONTD int factorial (int number) { int product = 1, i; switch (number) { case 0: case 1: product = 1; break; default: for (i = number; i > 1; i--) product *= i; // product = product * i; } return (product); } //end factorial Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 12 12
4. EXAMPLE (4) THE WHOLE PROGRAM #include <stdio.h> int factorial (int n); int main (void) { int result, number = -1; while (number < 0) { printf ( Enter an integer positive number> ); scanf ( %d , number); } // end while (number < 0) result = factorial (number); printf ( Factorial of %d = %d , number, result); } // end main int factorial (int number) { int product = 1, i; switch (number) { case 0: case 1: product = 1; default: for (i = number; i > 1; i--) product *= i; // product = product * i; } // end switch return (product); } //end factorial break; Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 13 13
5. EXAMPLE (5) Rewrite the payroll program in lecture 9 (see below) by moving the loop processing into a function subprogram called payroll. Return the total payroll amount payroll as the function result. total_pay = 0; //accumulator variable initialized count_emp = 0; while (count_emp < 7) { printf ( hours> ); scanf ( %d , &hours); printf ( rate> ); scanf ( %f , &rate); pay = hours * rate; printf ( pay is SR%6.2f\n , pay); count_emp++; //count_emp = count_emp + 1; total_pay = total_pay + pay; // total_pay += pay; } // end of while loop printf ( \n all employees processed\n ); printf ( total payroll is sr%8.2f\n , total_pay); Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 14 14
5. EXAMPLE (5) CONTD The function returns the total payment which is of type double function s type is double. The function receives the number of employees number of type int formal parameter is an integer variable. int count_emp; double hours, rate, pay, total_pay; total_pay = 0; //accumulator variable initialized printf ( enter number of employees> ); scanf ( %d , count_emp); for (i= 1; i<= count_emp; i++) { printf ( hours> ); scanf ( %d , &hours); printf ( rate> ); scanf ( %f , &rate); pay = hours * rate; printf ( pay is SR%6.2f\n , pay); total_pay += pay; } // end of for loop printf ( \n all employees processed\n ); printf ( total payroll is SR %8.2f\n , total_pay); Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 15 15
5. EXAMPLE (5) THE WHOLE PROGRAM #include <stdio.h> double payroll (int number); int main (void) { int count_emp; double total_pay; printf ( enter number of employees> ); scanf ( %d , count_emp); total_pay = payroll (count_emp); printf ( \n all employees processed\n ); printf ( total payroll is SR %8.2f\n , total_pay); } //end main double payroll (int number) { int i; double hours, rate, pay, total = 0.0; for (i= 1; i<= number; i++) { printf ( Enter hours> ); printf ( Enter rate> ); pay = hours * rate; total += pay; } //end for return (total); } // end payroll scanf ( %f , hours); scanf ( %f , rate); Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 16 16
6. GENERALIZATION OF PAYROLL FUNCTION double Sum_of_Product (int number) { int i; double item1, item2, product, total = 0.0; for (i= 1; i<= number; i++) { printf ( Enter item1> ); printf ( Enter item2> ); product = item1 * item2; total += product; } //end for return (total); } // end payroll scanf ( %f , item1); scanf ( %f , item2); By using generic names, the same function could be used in multiple purposes such as the calculation of: Total points a student earned (credit hours * points) Total sales of a store (item * price) Total taxes (income * tax) and so on Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 17 17
7. SELF-CHECK EXERCISE (1) Write a complete modular program that displays a menu, and performs the appropriate action accordingly in the corresponding function. The menu and the function names are listed below: Option 1 2 3 4 5 6 7 Action Exchange num1 and num2 Exchange num2 and num3 Exchange num1 and num3 Sort the three numbers Find their minimum Find their maximum Find their average Name Swap Swap Swap Sort Min Max Average Return nothing nothing nothing nothing int int double Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 18 18
8. SELF-CHECK EXERCISE (2) Write a modular program that calculates and prints the bill for Riyadh s power consumption. The rates vary depending on whether the user is residential, commercial, or industrial. A code of R corresponds to a Residential, C corresponds to a Commercial, and I to Industrial. Any other code should be treated as an error. The program should read the power consumption rate in KWH (Kilowatt per Hour); then it calculates the due amount according to the following: The rate is SAR 5 per KWH for Residential, SAR 10 per KWH for Commercial and SAR 20 per KWH for Industrial. The input data should be made in the main function. The calculation of the due amount is made in a function called DueAmount. The total of the amounts due should also be calculated. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 19 19
9. BUILT-IN FUNCTIONS C performs mathematical operations through mathematical libraries; namely, <stdlib.h> and <math.h>. These are listed below: Function Library Purpose Argument(s) Result abs(x) <stdlib.h> Returns the absolute value of its integer argument. Example: x = -5, abs(-x) = 5 <math.h> Returns the absolute value of its double argument. Example: x= -5.2, fabs(-x) = 5.2 <math.h> Returns the smallest integral value that is NOT less than x. Ex: x= 45.23, ceil(x)= 46.0 <math.h> Returns the largest integral value that is NOT greater than x. Ex.: x=45.23, floor(x)= 45.0 <math.h> Returns the non-negative square root of x for x >= 0.0. Ex.: x=25.0, sqrt(x)= 5.0 <math.h> Return xy. Ex.: x= 2.0, y=3.0; pow(x,y)=8.0 int int fabs(x) double double ceil(x) double double floor(x) double double sqrt(x) double double pow(x,y) double, double double Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 20 20
9. BUILT-IN FUNCTIONS (CONTD) Function Library Purpose Argument(s) Result exp(x) <math.h> Returns ex. double double log(x) <math.h> Returns the natural algorithm of x for x > 0.0 Returns the base-10 logarithm. Ex.: x= 100.0, log10(x) = 2.0 double double log10(x) <math.h> double double cos(x) <math.h> Returns the cosine of angle x. Ex.: x=0.0, cos(x) = 1.0 double (in radians) double sin(x) <math.h> Returns the sine of angle x. Ex.: x=1.5708, sin(x)=1.0 double (in radians) double tan(x) <math.h> Returns the tangent of angle x. Ex.: x=0.0, tan(x)=0.0 double (in radians) double Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 21 21
10. BUILT-IN FUNCTIONS EXAMPLE (1) Using the C built-in functions, to compute the roots of a quadratic equation in x of the form: ax2 + bx + c = 0. The two roots are defined as: root1 = ?+ ?2 4?? 2? root2 = ? ?2 4?? 2? disc = pow(b, 2.0) 4.0 * a * c; root1 = (-b + sqrt(disc)) / (2.0 * a); root2 = (-b - sqrt(disc)) / (2.0 * a); Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 22 22
11. BUILT-IN FUNCTIONS EXAMPLE (2) Write a complete program that computes the length of a side of a triangle using the following formula: ?2= ?2+ ?2 2?? ??? The lengths of the sides b and c are known. is given in degrees. To use the cos function of the C library, we must first convert the degrees into radians as follows: Radians = 180 ? #include <stdlib.h> #include <math.h> #define Int main (void) { double a, b, c, a2, degrees, radians; printf ( Enter the length of side b ); printf ( Enter the length of side c ); radians = degrees * PI / 180.0; a2 = pow(b, 2.0) + pow(c, 2.0) 2 * b * c * cos(radians); a = sqrt(a2); printf ( The length of the third side a= %f , a); return (0); } //end main PI 3.14 scanf( %f , &b); scanf( %f , &c); Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 23 23
12. SELF-CHECK EXERCISES (1) Write the following mathematical expressions using C functions: 1. Area = ??2 2. ? = ? + ?? 3. X = 2sin1 2? ? 4. ? = ???? (??) 5. ???????? = ?1 ?2 2? + ? cos1 2+ ?1 ?2 2 Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 24 24
13. SELF-CHECK EXERCISES (2) Evaluate the following expressions: 1. floor (15.8) 2. floor (15.8 + 0.5) 3. ceil (-7.2) * pow(4.0, 2.0) 4. sqrt (floor (fabs (-16.8))) 5. log10 (1000.0) Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 25 25