14 Problem Solving.pptx - Topic 14 Problem Solving 1 Solving Problems 2 When we write programs to solve problems we may use a combination of control

14 Problem Solving.pptx - Topic 14 Problem Solving 1...

This preview shows page 1 - 10 out of 32 pages.

Topic 14 Problem Solving 1
Image of page 1
Solving Problems When we write programs to solve problems, we may use a combination of control structures. loops to allow the program to repeat actions. selections to allow the program to make decisions. Sometimes these control structures are nested. 2
Image of page 2
Solving Problems When designing the solution to the problem, ask yourself the following questions: Do I need to repeat any actions? If yes, a loop is required. Do I need to make any decisions? If yes, selection is required. 3
Image of page 3
Case Study 1 Problem: A teacher wants to process her class test results. She wants to determine the number of passes and failures and the average of the test scores. For a student to pass, the test score must be 50 and above. 4
Image of page 4
Case Study 1 Understand the problem: Program input: a set of test scores Program output: number of passes number of failures average of the test scores To obtain the average, we need to compute the total of the test scores 5
Image of page 5
Case Study 1Design the solution:Do I need to repeat any actions? What type of loop should I use?Assume the number of test scores is known. 6
Image of page 6
Design the solution Case Study 1 count <= num_stud count = 1 count = count + 1 true Read num_stud false Read test_score 7
Image of page 7
Case Study 1 – Program Development 1 #include <iostream> using namespace std; int main() { int num_stud; // input – number of students cout << " Enter number of students to process: " ; cin >> num_stud; int test_score; // input – test score for(int count = 1; count <= num_stud; count++) { cout << " Enter test score " << count; cin >> test_score; } return 0; } 8
Image of page 8
Image of page 9
Image of page 10

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture