Presentation is loading. Please wait.

Presentation is loading. Please wait.

Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.

Similar presentations


Presentation on theme: "Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement."— Presentation transcript:

1 Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement

2 Understand the problem A class of students took a quiz. The grades (integers ranging from 0 to 100) for this quiz are available. Determine the class average on the quiz.

3 First level design Determine the class average for the quiz. Complete representation of what the program needs to do. Not detailed enough to code yet.

4 Next level of refinement Initialize variables Input the quiz grades. Total them and count them. Calculate and print the class average Complete representation of the problem but still not detailed enough to code.

5 Refine “Initialize variables” Initialize total to zero Initialize counter to zero Initialize average to zero

6 Refine “ Input the quiz grades. Sum them and count them..” Think first. How many grades do we have? How will we know when we have the last one?

7 Refine “ Input the quiz grades. Sum them and count them..” Input the first grade While the user has not yet entered the special end of data value Add this grade into the total Add one to the grade counter Input the next grade (possibly the special end of data value)

8 Calculate and print the class average Think first. What kind of errors could occur? What could go wrong? Division by 0. User doesn’t enter any grades.

9 Calculate and print the class average If the counter is not equal to zero Set the average to the total divided by the counter Print the average else Print “ No grades were entered”

10 Grade averaging pseudo-code Initialize total to zero Initialize counter to zero Initialize average to zero. Input the first grade While the user has not yet entered the special end of data value Add this grade into the running total Add one to the grade counter Input the next grade (possibly the special end of data value) If the counter is not equal to zero Set the average to the total divided by the counter Print the average else Print “ No grades were entered”

11 #include using namespace std; main () { float average = 0; int counter = 0, total = 0, grade; cout << “Enter grade, enter -1 to end: “; cin >> grade; while (grade != -1) { total = total + grade; counter++; cout << “Enter grade, enter -1 to end: “; cin >> grade; } if (counter != 0) { average = (float) total/counter; cout << “Class average is “ << setprecision (2) << average << endl; else cout << “No grades were entered” << endl; return 0; }


Download ppt "Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement."

Similar presentations


Ads by Google