13-Problem-Solving.pptx

# 13-Problem-Solving.pptx - CMPSC 201 Lecture 13...

This preview shows pages 1–12. Sign up to view the full content.

CMPSC 201 Lecture 13 Problem-solving (some slides adapted from Jenn Plasterr)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Objectives Be able to use identify different abnormal statements Be able to determine the type of loops to use Be able to avoid common loop problems Be able to debug looping problems when them arise Martin K.-C. Yeh 2
The Empty Statement A semicolon creates a C++ statement Placing a semicolon after x++ creates the statement: x++; Placing a semicolon after nothing creates an empty statement that compiles but does nothing: cout << "Hello" << endl; ; cout << "Good Bye"<< endl; Martin K.-C. Yeh 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Extra Semicolon Placing a semicolon after the parentheses of a for loop creates an empty statement as the body of the loop Example: for(int count=1; count <= 10; count++); cout << "Hello\n"; prints one "Hello", but not as part of the loop! The empty statement is the body of the loop cout << "Hello\n"; is not part of the loop body! Martin K.-C. Yeh 4
Designing Loops Designing a loop involves designing: The body of the loop The initializing statements The conditions for ending the loop Martin K.-C. Yeh 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Which Loop To Use? Choose the type of loop late in the design process First design the loop using pseudocode Translate the pseudocode into C++ The translation generally makes the choice of an appropriate loop clear Martin K.-C. Yeh 6
Which Loop To Use? Should the loop always execute at least once? Yes  do-while No  while or for Should the loop be count controlled or sentinel controlled? Count  any of the loops ( for is really good for this) Sentinel  while or do-while (usually). Numeric calculations? For loops really good for this, especially when using a variable changed by equal amounts each time the loop iterates Martin K.-C. Yeh 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Sums and Products A common task is reading a list of numbers and computing the sum Pseudocode for this task might be: sum = 0; repeat the following this many times get input and store it in next ; sum = sum + next; end of loop This pseudocode can be implemented with a for-loop as shown on the next slide Martin K.-C. Yeh 8
for-loop for a sum The pseudocode from the previous slide is implemented as: int sum = 0; for(int count=1; count<=thisMany; count++) { cin >> next; sum += next; } (sum must be initialized prior to the loop body!) Martin K.-C. Yeh 9

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Repeat "this many times" Pseudocode containing the line repeat the following "this many times" is often implemented with a for-loop Example: for(int count=1;count<=numOfRepetition; count++) { [Loop body]; } Martin K.-C. Yeh 10
for

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern