1 / 77

C++ GPA Calculator - Calculate GPA based on course marks (CS112 Lecture 7 & 8)

Learn how to calculate GPA based on course marks using C++. Handle non-numerical inputs and inputs outside the range of 0-100.

bpantoja
Download Presentation

C++ GPA Calculator - Calculate GPA based on course marks (CS112 Lecture 7 & 8)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS112 – 2017 / 2018 2nd TermProgramming ILecture 7 & 8: C++ Basics VI

  2. 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

  3. Are u still trying to install CodeBlocsk ?Wa’etak Teen ya Mnayel !

  4. 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?

  5. While Syntax

  6. While Semantics

  7. 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;

  8. 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;

  9. 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;

  10. 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

  11. 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

  12. 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 --

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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 --

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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 --

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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 --

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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;

  35. The For Statement • Syntax for(ForInit ; ForExpression; PostExpression) Action • Example for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; }

  36. Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;

  37. Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;

  38. Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0

  39. Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0

  40. Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0

  41. Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

More Related