Introduction to Computer Programming CS101: Lecture 8
Formulating Algorithms: Sentinel-Controlled Repetition (cont.) The second refinement of the preceding pseudocode statement is then Prompt the user to enter the first grade Input the first grade (possibly the sentinel) While the user has not yet entered the sentinel Add this grade into the running total Add one to the grade counter Prompt the user to enter the next grade Input the next grade (possibly the sentinel) ©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
// class average problem with sentinel-controlled repetition #include <iostream> using namespace std; int main() { int counter; double average ,total ,grade; total = 0; counter = 0; grade=0; average = 0; cout << " Enter grade or -1 to quit : " ; cin >> grade; // input grade or sentinel value
while (grade != -1) // while grade is not -1 { total = total + grade; ++counter; cout << "Enter grade or -1 to quit: "; cin >> grade; // input grade or sentinel value } average = total / counter; cout << "\nTotal of all " << counter << " grades is: " << total << endl; cout << "Class average is :" << average << endl; return 0; } // end main
Output: Enter grade or -1 to quit : 98 Enter grade or -1 to quit: 75 Total of all 3 grades is: 237.5 Class average is :79.1667
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
Formulating Algorithms: Sentinel-Controlled Repetition (cont.) The pseudocode statement Calculate and print the total of all student grades and the class average can be refined as follows: If the counter is not equal to zero Set the average to the total divided by the counter Print the total of the grades for all students in the class Print the class average Else Print “No grades were entered” Test for the possibility of division by zero Normally a fatal logic error that, if undetected, would cause the program to fail (often called “crashing”). ©1992-2014 by Pearson Education, Inc. All Rights Reserved.
#include <iostream> using namespace std; int main() { int counter; double average ,total ,grade; total = 0; counter = 0; grade=0; average = 0; cout << " Enter grade or -1 to quit : " ; cin >> grade; // input grade or sentinel value
while (grade != -1) // while grade is not -1 { total = total + grade; ++counter; cout << "Enter grade or -1 to quit: "; cin >> grade; // input grade or sentinel value } if (counter != 0) //check the denominator (counter)not equal zero average = total / counter; cout << "\nTotal of all " << counter << "grades is: " << total << endl; cout << "Class average is :" << average << endl; else cout << "No grades were entered !!" << endl; return 0;
break and continue Statements break Statement The break statement, when executed in a while, for, do…while, or switch statement, causes immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch statement. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.
1 2 3 4 Broke out of loop at count = 5 #include <iostream> using namespace std; int main() { int counter; for (counter = 1; counter <= 10; ++counter) // loop 10 times if (counter == 5) break; // break loop only if counter is 5 cout << counter << " "; } // end for cout << "\nBroke out of loop at count = " << counter << endl; return 0; } // end main
break and continue Statements (cont.) The continue statement, when executed in a while, for or do…while statement, skips the remaining statements in the body of that statement and proceeds with the next iteration of the loop. In while and do…while statements, the loop-continuation test evaluates immediately after the continue statement executes. In the for statement, the increment expression executes, then the loop-continuation test evaluates. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.
Used continue to skip printing 5 #include <iostream> using namespace std; int main() { int counter; for (counter = 1; counter <= 10; ++counter) if (counter == 5) continue; cout << counter << " "; } // end for cout << "\nUsed continue to skip printing 5" << endl; return 0; } // end main 1 2 3 4 6 7 8 9 10 Used continue to skip printing 5
Program that reverses the digits in an integer variable number #include <iostream> #include <iomanip> using namespace std; int main() { int number, rem; cin>>number; while (number > 0) rem = number % 10; cout << rem; number = number / 10; } return 0; } // end main
C++ code to calculate x raised to the y power C++ code to calculate x raised to the y power. The code should have a while repetition statement. int x, y; int i = 1, power = 1; cout << "Enter base as an integer: "; cin >> x;// input base cout << "Enter exponent as an integer: "; cin >> y; // input exponent while (i <=y) { power *= x; i++; } cout << power << endl;
©1992-2014 by Pearson Education, Inc. All Rights Reserved. Logical Operators C++ provides logical operators that are used to form more complex conditions by combining simple conditions. The logical operators are && (logical AND), || (logical OR) and ! (logical NOT, also called logical negation). Ex: if (gender == 1 && age >= 65) ++seniorFemales; ©1992-2014 by Pearson Education, Inc. All Rights Reserved.
Logical Operators (cont.) Logical AND (&&) Operator The && (logical AND) operator is used to ensure that two conditions are both true before we choose a certain path of execution. The simple condition to the left of the && operator evaluates first. If necessary, the simple condition to the right of the && operator evaluates next. The right side of a logical AND expression is evaluated only if the left side is true. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
Logical Operators (cont.) Logical OR (||) Operator The || (logical OR) operator determines if either or both of two conditions are true before we choose a certain path of execution. The && operator has a higher precedence than the || operator. Both operators associate from left to right. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
Write a C++ statement(s) to accomplish the following: - If semesterAverage >= 90 or finalExam >= 90, print “Student grade = A”. ANS: if ((semesterAverage >= 90) || (finalExam >= 90)) cout << "Student grade is A" << endl;
Logical Operators (cont.) Logical Negation (!) Operator C++ provides the ! (logical NOT, also called logical negation) operator to “reverse” a condition’s meaning. The unary logical negation operator has only a single condition as an operand. You can often avoid the ! operator by using an appropriate relational or equality operator. ©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson Education, Inc. All Rights Reserved.