Announcements First Midterm

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
From Selection to Repetition
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Announcements Homework 3 is due this Wednesday, November 11 th Superman robot moves into the phone booth. Homework 4 will be assigned this week and will.
Control of flow We learned that default flow of instructions is sequential. Then, we learned how to control the flow using "if" and "switch." Now, we will.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Announcements First Midterm March 16 th on Monday next week at 19:40 (100 minutes) Exam classes: if (LastName
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Announcements First Midterm March 14 th on Monday next week at 19:40 (100 minutes) Exam classes: if (LastName
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Chapter Topics The Basics of a C++ Program Data Types
Announcements Midterm
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
if-else, switch, while, for, do-while
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Announcements Final Exam on August 17th Wednesday at 16:00.
Basic Elements of C++ Chapter 2.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
One-Dimensional Array Introduction Lesson xx
Alternate Version of STARTING OUT WITH C++ 4th Edition
Additional Control Structures
Course websites CS201 page link at my website: Lecture slides
Chapter 2.2 Control Structures (Iteration)
Computing Fundamentals
Announcements Final Exam on December 25th Monday at 16:00.
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Announcements Homework 1 will be assigned this week,
2.6 The if/else Selection Structure
Announcements First Midterm
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Objectives You should be able to describe: The while Statement
CS150 Introduction to Computer Science 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
CS31 Discussion 1D Winter19: week 3
CS31 Discussion 1H Fall18: week 3
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Announcements First Midterm November 7th on Tuesday next week at 19:40 (110 minutes) Exam classes: if (LastName <= "Camat")      cout << " FENS G077 " << endl; else if ("Canbakal" <= LastName <= "Gungor")      cout << " FMAN 1099 " << endl; else if ("Gurcan" <= LastName <= "Kose")      cout << " FASS G062 " << endl; else if ("Kulaoglu" <= LastName <= "Onder")      cout << " FASS G022 " << endl; else if ("Oskay" <= LastName <= "Ozyazgan")      cout << " FENS L035 " << endl; else if ("Pala" <= LastName <= "Sonmez")      cout << " FENS G032 " << endl; else if ("Sulun" <= LastName <= "Turkoglu")      cout << " FENS G035 " << endl; else if ("Ucak" <= LastName)      cout << " FENS L045 " << endl; This week recitations, we will solve sample midterm questions. Detailed email about the midterm is sent with previous years’ exams. Homework 4 is due on Wednesday, November 8th next week

for loop compared with while <initialization> while (<test>) { <statement1>; ... <statementN>; <update> } for (<initialization>; <test>; <update> ) { <statement1>; ...    <statementN>; } Let’s compare for loop with the while loop. for loop merges the 3 pieces (initialization, condition and update) of a while loop into one line. This makes loops easier to write and read in one line.

Example Rewrite the while loop of main of primes.cpp using for k = low; while (k <= high) { if (IsPrime(k)) { cout << k << endl; numPrimes += 1; } k += 1; } for (k = low; k <= high; k += 1) { if (IsPrime(k)) { cout << k << endl; numPrimes += 1; } }

Shorthand for increment/decrement Lots of code requires incrementing a variable by one Three methods, using = and +, using +=, and using ++ effectively they are same num = num + 1; num += 1; num++; // post increment It is also possible to write ++num; pre-increment These differ on when the increment is performed, but this difference doesn’t matter when used as an abbreviation for the statement n += 1; in a single statement Similarly there are post-decrement (and pre-decrement) num = num - 1; num -= 1; num--;

The do-while loop Similar to while loop, but the test is after the execution of the loop body The while loop may never execute, do-while loop executes at least once <initialization> do { <statement1>;    ...   <statementN>; <update> } while (<condition>); Example: Prompt for a number between 0 and 100, loop until such a number is entered (user should enter at least one number) cout << "enter number in range [0..100] "; cin >> num; } while (num < 0 || num > 100 ); Don’t forget

Priming Priming: reading an initial value before the loop do not get confused with prime numbers; this is something else Problem: enter numbers, add them up, stop when -1 entered int sum = 0; int num; cin >> num; // prime the loop while (num != -1) { sum += num; cin >> num; } cout << "total = " << sum << end; Code duplication exists here: input (and perhaps prompt) code is repeated before the loop and in the loop

Pseudo infinite solution using break To avoid repeating code, include it in the body of the loop only, use a test to break out of the loop break statement exits (inner-most) loop I don’t prefer this kind of loops (I’d prefer code duplication) Because the loop condition is not clear, hence prevents readability Try not to use break in this course Save it for later when you develop really complicated loops int sum = 0; int num; while (true) // seemingly infinite loop { cin >> num; if (num == -1) { break; // get out of loop } sum += num; cout << "total = " << sum << end;

Fence Post Problem The problem that occurs when one or more operations of the loop body are executed one less then the others. Example: Display integers between 1 and 10 separated by comma 1,2,3,4,5,6,7,8,9,10 no comma after 10; no comma before 1. for (n=1; n <= 10; n++) { cout << n << ","; } Problem: comma after 10 for (n=1; n < 10; n++) } cout << n; No problem, but code duplicates Think of other solutions! (see page 175 of Tapestry)

Downward-counting loop Calculate n to the power of m: nm=nxnx…xn Example: 25=2x2x2x2x2=32 int power = 1; int n, m; cin >> n >> m; for (int i = m; i <= 1; i--) { power = power * n; }

Nested loops Sometimes one loop occurs in another Generating 2-dimensional tabular data multiplication table Sorting vectors (which will be studied much later) Display some geometric figures using character * (or any other character) display rectangles, triangles Although other loops can be nested as well, most of the time, for loops are used in nested manner

Nested loops - Example Write a function to display a rectangle of stars (height and width are parameters) e.g. if height is 4 and width is 7, the output should look like ******* for (i=1; i<= height; i++) { for (j=1; j<=width; j++) // inner loop prints 1 line of stars cout << "*"; } cout << endl; // end of line is put to the end of each line See drawfigures.cpp for the complete function and its use in main

Nested loops - Example Write a function to display a perpendicular isosceles triangle of stars (perpendicular side length is parameter) e.g. if side length is 6 , the output should look like * ** *** **** ***** ****** for (i=1; i <= side; i++) { for (j=1; j<=i; j++) // inner loop prints 1 line of stars cout << "*"; } cout << endl; // end of line is put to the end of each line See drawfigures.cpp for the complete function and its use in main

Same loop: downward-counting for (i=1; i <= side; i++) { for (j=1; j<=i; j++) cout << "*"; } cout << endl; for (i=1; i <= side; i++) { for (j=i; j>=1; j--) cout << "*"; } cout << endl;

Drawfigures – Other Considerations What about having a function to display a line of stars (number of stars is a parameter) useful for both rectangle and triangle void PrintLine (int numstars) // pre: numstars > 0 // post: displays numstars stars in one line { int i; for (i=1; i<= numstars; i++) { cout << "*"; } cout << endl; // end of line is put to the end of the line in rectangle function, inner loop is replaced by a function call for (i=1; i<=height ; i++) PrintLine(width); use of PrintLine in triangle function is similar

Example – Multiplication Table On ith line print, i*1, i*2, i*3, ... , i*i Total number of lines is an input. Display lines starting with 1. See multiply.cpp #include <iostream> #include <iomanip> // for setw using namespace std; int main() { int i,k,numlines; const int WIDTH = 4; cin >> numlines; for (i=1; i <= numlines; i++) for (k=1; k <= i; k++) cout << setw(WIDTH) << i*k; } cout << endl; return 0;

Constants Sometimes very useful provides self documentation re-use the same value across the program avoid accidental value changes like variables, but their value is assigned at declaration and can never change afterwards declared by using const before the type name (any type is OK) const double PI = 3.14159; const string thisclass = "CS201" const int WIDTH = 4; later you can use their value cout << (PI * 4 * 4); but cannot change their value PI = 3.14; causes a syntax error

Formatting Output We use stream manipulator setw to specify the total number of spaces that the next output will use setw(field length) written in cout and affects only the next output value not the whole cout line output is displayed using field length spaces in right justified manner (any empty space is on the left) defined in header file <iomanip>, so you have to have #include <iomanip> Example cout << setw(9) << "cs201"; output shown is four blanks and cs201

Example using robot class (see rectangularscan.cpp) Write a program in which the robot starts at 0,0 and searches a rectangular space that covers n*n cells n is input (in the example below, n is 8) during this journey the robot should pick or put things on the cells so that all visited cells occupy one thing