Decisions in programs Allow programs to follow different courses of action or execute different tasks depending on data values. Flow-diagrams of decisions Psuedocode If x is not zero if amount is positive Multiply product by x process credit else Process debit 1. If angle measurement is in degrees Convert angle measurement to radians 2. Calculate height of projectile when it hits target. 1. if yard width is less than house width or yard height is less than house width Display an error message and exit program 2. Calculate cutting time. 1. if coins value Is greater than 10 dollars Display a message "good saver" else Display a message " save more" C provides two language constructs: If statement and switch statement If (condition) If (condition) { { true statements true statements } } else { false statements } condition has the form : expression rel-op expression expression is a variable, literal (constant), or general expression rel-op is a symbol or symbol pair: <, <=, >, >= ("relational operators) ==, != (not equal) ("equality" operators) A condition evaluates to true (non-zero in C) or false (zero in C). (x < y), (y + z >= 0), (y != 0) For x = 5, y = 12, z = -15, what are the values of the 3 conditions above? Other operators that can appear in conditions: Logical operators - && (and), || (or), and ! (not) Evaluate: (x < y || y != 0) ((y + z >= 0) && (y != 0)) ( y != 0 && z <= y) ( !(x < y) && (y + z >= 0)) ( !((x < y) && (y + z >= 0))) nested if statements can model decisions with several alternatives. if student receives an exam score of 90 or higher Set grade to A else if student receives an exam score of 80 or higher Set grade to B else if student receives an exam score of 70 or higher Set grade to C else if student receives an exam score of 60 or higher Set grade to D else Set grade to F if (score >= A_SCORE) grade = 'A'; else if (score >= B_SCORE) grade = 'B'; else if (score >= C_SCORE) grade = 'C'; else if (score >= D_SCORE) grade = 'D'; else grade = 'F'; Notice that (score >= B_SCORE) could also be written as (score >= B_SCORE && score < A_SCORE) This condition contains an unnecessary comparison: score < A_SCORE Write this as function. _____ assign_grade(__________) { _____ grade; /* local variable */ /* put if statement here */ return _______; } Problem: Write a program that reads a student's exam score and determines the student's grade. Use function assign_grade and a display function that displays the score, grade, and exam scale. Also, use an instruct function to display the results. Data requirements: Inputs _________ Outputs _________ Algorithm: 1. Display user instructions 2. 3. 4. Function instruct Input arguments: output: Function assign_grade Input arguments: output: Function display Input arguments: output: How to use this function: #define A_SCORE 90 #define B_SCORE 80 #define C_SCORE 70 #define D_SCORE 60 ______ assign_grade(__________); void instruct(); void display(______, _______); void main() { int score; char letter_grade; /* display instructions */ _______(_____); printf("Enter student's exam score:"); scanf("%d", &score); /* find letter grade */ _______ = assign_grade(_______); /* display result */ display(______, _______); } void instruct() { printf("This is a simple grading program.\n"); printf("You enter the student's score and \n"); printf(" the program displays the grade."\n\n"); } void display(int s, char l_g) { printf("Student got a score of %d on the exam\n", _____); printf("This is a grade of %c\n\n", _______); printf("Scale is A: %d and above\n", _____); printf(" B: %d up to %d\n", _____, ______); printf(" C: %d up to %d\n", _____, ______); printf(" D: %d up to %d\n", _____, ______); printf(" F: less than %d\n", _____); } void assign_grade(int score) { char grade; /* put if statement here. */ if (______ >= A_SCORE) ______ = 'A'; ... return ______; } This is a simple grading program. You enter the student's score and the program displays the grade. Enter student's exam score:75 Student got a score of 75 on the exam This is a grade of C Scale is A: 90 and above B: 80 up to 90 C: 70 up to 80 D: 60 up to 70 F: less than 60 Putting if statements in functions. // Calculates hourly rate of pay for an employee double calc_gross( double hours, double rate) { double max_hours = 100.0; double max_reg_hours = 40.0; double overtime_rate = 1.5; double gross; if (hours <= 0.0) { gross = ERROR; /* constant that indicates error */ printf("error - negative value for hours\n"); } else if (hours <= max_reg_hours) gross = hours * rate; else if (hours <= max_hours) gross = max_reg_hours * rate + (hours - max_reg_hours) * overtime_rate * rate; else { gross = ERROR; printf("error - hours is too large\n"); } return gross; } To call this function in a main function: #define ERROR -1 double calc_gross(double, double); int main(void) { double hours, pay_rate; /* inputs */ double gross_pay, net_pay; /* outputs */ ... /* assume hours and pay_rate have been read -- calculate gross_pay */ gross_pay = calc_gross(hours, pay_rate); /* calculate net pay if gross_pay was calculated */ if (gross_pay == ERROR) return ERROR; else { net_pay = calc_net(gross_pay); printf("Gross pay is $%f and net pay is $%f\n", gross_pay, net_pay); } return 0; } /* end of main */