770 likes | 782 Views
Learn how to calculate GPA based on course marks using C++. Handle non-numerical inputs and inputs outside the range of 0-100.
E N D
CS112 – 2017 / 2018 2nd TermProgramming ILecture 7 & 8: C++ Basics VI
Lecture Objectives • Pb#11 – GPA Calculator • Pb #12 – Printing Stars • Pb #13 – Check Palindrome • C++ Iterative Constructs • for • while • do while • 2. Nested and Infinite Loops • 3.C-Strings
Are u still trying to install CodeBlocsk ?Wa’etak Teen ya Mnayel !
Pb#11: GPA Calculator • Given course mark, calculate GPA. • >= 90 A+ • >= 85 A • >= 80 B+ • >= 75 B • >= 70 C+ • >= 65 C • >= 60 D+ • >= 50 D • F • How to exclude non-numerical inputs? • How to handle inputs outside the range 0-100?
Computing an Average int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 numberProcessed 0 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 numberProcessed 0 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 0
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 numberProcessed 0 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 0
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 numberProcessed 0 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 0 value --
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 numberProcessed 0 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 0 value 1
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 numberProcessed 0 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 0 1 value 1
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 numberProcessed 0 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
Suppose input contains: 1 5 3 1 6 Execution Trace listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value --
Suppose input contains: 15 3 1 6 Execution Trace listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 5
Suppose input contains: 15 3 1 6 Execution Trace listSize 4 numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 6 value 5
Suppose input contains: 15 3 1 6 Execution Trace listSize 4 numberProcessed 1 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5
Suppose input contains: 15 3 1 6 Execution Trace listSize 4 numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 6 value 5
Suppose input contains: 15 3 1 6 Execution Trace listSize 4 numberProcessed 2 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value --
Suppose input contains: 153 1 6 Execution Trace listSize 4 numberProcessed 2 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 3
Suppose input contains: 153 1 6 Execution Trace listSize 4 numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 9 value 3
Suppose input contains: 153 1 6 Execution Trace listSize 4 numberProcessed 2 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3
Suppose input contains: 153 1 6 Execution Trace listSize 4 numberProcessed 3 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3
Suppose input contains: 153 1 6 Execution Trace listSize 4 numberProcessed 3 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value --
Suppose input contains: 1531 6 Execution Trace listSize 4 numberProcessed 3 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 1
Suppose input contains: 1531 6 Execution Trace listSize 4 numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 10 value 1
Suppose input contains: 1531 6 Execution Trace listSize 4 numberProcessed 3 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1
Suppose input contains: 1531 6 Execution Trace listSize 4 numberProcessed 3 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1
average 2.5 Suppose input contains: 1531 6 Execution Trace listSize 4 numberProcessed 3 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10
Suppose input contains: 1531 6 Execution Trace listSize 4 numberProcessed 3 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 average 2.5
Suppose input contains: 15316 Execution Trace Stays in stream until extracted int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
The For Statement • Syntax for(ForInit ; ForExpression; PostExpression) Action • Example for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; }
Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;
Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;
Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0
Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0
Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0
Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;
Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1
Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1
Execution Trace i 2 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1
Execution Trace i 2 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1
Execution Trace i 2 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2
Execution Trace i 2 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2
Execution Trace i 3 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2