CS 1430: Programming in C++ No time to cover HiC.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CS101 Computer Programming I Chapter 4 Extra Examples.
Control Structures Repetition or Iteration or Looping Part II.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Overview Go over parts of quiz? Another iteration structure for loop.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
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.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
Control Structures Repetition or Iteration or Looping Part II.
Topic 4: Looping Statements
Chapter 3 Control Statements
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II (Repetition)
CS 1430: Programming in C++ No time to cover HiC.
C++ Programming: CS150 For.
Chapter 4 Loops DDC 2133 Programming II.
CS 1428 Exam I Review.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
Control Structures - Repetition
While Loops.
Iteration with While You can say that again.
TOPIC 4: REPETITION CONTROL STRUCTURE
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS 1430: Programming in C++ No time to cover HiC.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 6: Repetition Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CS 1428 Exam I Review.
CS 1430: Programming in C++.
CS 1430: Programming in C++ No time to cover HiC.
Presentation transcript:

CS 1430: Programming in C++ No time to cover HiC

Quiz2-2 1. For each statement in the following, determine if it’s valid or invalid. (Circle your answers.) float rate, hours, wage; cin >> rate >> hours; rate * hours = wage; wage = rate * hours; cout >> “The wage is ” >> wage; cout << “The wage is ” << wage; // Valid Invalid

Quiz2-2 2. What integer value does the following byte represent? 102 Assume the above byte represents ASCII character ‘f’. Gave the byte representing ASCII character ‘c’. 99 1 1

Quiz2-2 3. Evaluate the following mathematical expressions in C++: 15 / 8 19 % 33 6 + 12 / 3 * 2 6 + 4 * 2 6 + 8 Result 1 19 14

Quiz2-2 4. Write C++ statements according to the comments. // Declare float variables price and cost, // and integer variable quantity float price, cost; int quantity;   // Input price with an input prompt cout << “Enter the price: ”; cin >> price; // Input quantity with an input prompt cout << “Enter the quantity: ”; cin >> quantity;

Quiz2-2 4. Write C++ statements according to the comments.   // If price is positive and quantity is not negative, // Compute cost, which is the product of price // and quantity // Display cost at the beginning of next line with // message “The cost is ” // Otherwise, display message “Invalid input!”   if ( price > 0 && quantity >=0) { cost = price * quantity; cout << endl << “The cost is ” << cost; } else cout << endl << “Invalid input!”;

Quiz2-2 4. Write C++ statements according to the comments.   // Display cost at the beginning of next line with // message “The cost is ” cout << endl << “The cost is ” << cost; cout << ‘\n’ << “The cost is ” << cost; cout << “\n” << “The cost is ” << cost; cout << “\nThe cost is ” << cost;

Sentinel-Controlled Loop const int END_VALUE = -1; // not required const int LOW_LIMIT = 0; // not required const int HIGH_LIMIT = 60; int main() { int score, max; max = LOW_LIMIT; // Range is known cin >> score; // Prime read while (score != END_VALUE) if (score >= LOW_LIMIT && score <= HIGH_LIMIT) if (score > max) max = score; } else cout << “Invalid score!”; cin >> score; cout << “The max score is ” << max << ‘.’; return 0;

Sentinel-Controlled Loop const int END_VALUE = -1; // not required const int LOW_LIMIT = 0; // not required const int HIGH_LIMIT = 60; int main() { int score, max; max = 0; // Range is known cin >> score; // Prime read while (score != -1) if (score >= 0 && score <= 60) // 0.5 points off on Prog1! if (score > max) max = score; } else cout << “Invalid score!”; cin >> score; cout << “The max score is ” << max << ‘.’; return 0;

Loop Control Variable (LCV) while (score != -1) { … } LCV: score When to exit a loop Avoid infinite loops

Four Parts of Loops Loop Initialization (LCV and others) Testing LCV Loop Body Updating LCV

While Loop Initialization (LCV and others) while (LogicalExpression) (Testing LCV) false true Do Work Update LCV Statement after loop

Four Parts of Loops max = LOW_LIMIT; // Range is known cin >> score; // Prime read while (score != -1) { if (score >= LOW_LIMIT && score <= HIGH_LIMIT) if (score > max) max = score; } else cout << “Invalid score!”; cin >> score; cout << “The max score is ” << max << “.”;

Computing Count Range is not Known int score, max, count; count = 0; // initialization // cannot initialize max, since range unknown cin >> score; // Prime Read while (score != -1) { count ++; // count = count + 1; if (count == 1) // first score max = score; else if (score > max) } cin >> score; if (count == 0) cout << “No scores!”; cout << “The maximal score is ” << max;

Computing Count Range is not Known int score, max, count; cin >> score; // Prime Read while (score != -1) { count ++; // count = count + 1; if (count == 1) // first score max = score; else if (score > max) } cin >> score; if (count == 0) cout << “No scores!”; cout << “The maximal score is ” << max; // Does it run? // Run time error! // Uninitialized value: count!

Computing Count Range is not Known int score, max, count; count = 0; // Initialization! cin >> score; while (score != -1) { count ++; if (count == 1) max = score; else if (score > max) } if (count == 0) cout << “No scores!”; cout << “The maximal score is ” << max; // Works even there is no score

Trace in Visual Studio Project CS1430Trace Trace-RangeUnkown.cpp Trace-RangeKnown.cpp Line/Column number Break Point Step Over Reset Auto/Locals AutoHide

Trace Execution on Paper Range unknown int score, max, count; count = 0; cin >> score; while (score != -1) { count ++; if (count == 1) max = score; else if (score > max) } if (count == 0) cout << “No scores!”; cout << “The maximal score is ” << max; Input Scores: 45 55 39 -1 score max count ? ? ? 0 45 1 55 2 39 3 -1

Tracing on Paper Range Known Tracing Input: 48 54 66 53 59 -1 const int LOW_LIMIT = 0; const int HIGH_LIMIT = 60; int score, max = LOW_LIMIT; cin >> score; while (score != -1) { if (score < LOW_LIMIT || score > HIGH_LIMIT) cout << “Invalid score: ” << score; else if (score > max) max = score; } Tracing Input: 48 54 66 53 59 -1 score max ? 0 48 54 66 53 59 -1 How to do it? New values on next line Do not cross old values Blank when no new value WILL BE ON QUIZ And Test 1 And Final!

Checking Input Range max = 0; cin >> score; while (score != -1) { if (score >= 0 && score <= 60) if (score > max) max = score; else cout << “Invalid score!”; } // Is it correct? //(ignoring magic nubers) // Which if is else paired with? max = 0; cin >> score; while (score != -1) { if (score >= 0 && score <= 60) if (score > max) max = score; else cout << “Invalid score!”; } //else is paired with the last if!

Checking Input Range const int LOW_LIMIT = 0; const int HIGH_LIMIT = 60; max = LOW_LIMIT; cin >> score; // Prime read while (score != -1) { if (score >= LOW_LIMIT && score <= HIGH_LIMIT) if (score > max) max = score; } else cout << “Invalid score!”; cin >> score;

Getting a Valid Value Pseudo Code Input a value While the input value is out of range Display a message Input a new value cin >> score; while (score < 0 || score > 60) cout << “Invalid score!”; Correct?

Getting a Valid Value Pseudo Code Input a value While the input value is out of range Display a message cin >> score; while (score < 0 || score > 60) { cout << “Invalid score!”; } How about the end value -1?

Getting a Valid Value Pseudo Code Input a value While the input value is out of range and not -1 Display a message cin >> score; while ((score < 0 || score > 60) && score != -1) { cout << “Invalid score!”; } // Is the following correct? while (score < 0 || score > 60 && score != -1) // NO! It’s the same as the following: while (score < 0 || (score > 60 && score != -1))

Getting Valid Scores int score, max = LOW_LIMIT; cout << “Enter a score: ”; cin >> score; while (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT)) { cout << “Invalid score: ” << score; } while (score != END_VALUE) if (score > max) max = score;

Data Type bool bool badScore; // Boolean variable with two possible values: true or false badScore = false; // lower case cin >> score; badScore = (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT)); while (badScore == true) { cout << “Invalid score: “ << score; }

Data Type bool (II) bool badScore; badScore = false; cin >> score; badScore = (score != END_VALUE && (score < LOW_LIMIT || score > HIGH_LIMIT)) // Change (badScore == true) to badScore while (badScore) { cout << “Invalid score: “ << score; }

Event-Controlled Loops Sentinel-Controlled Loops Boolean-Variable-Controlled Loops Others

Quiz3-1 10 PM Today Lab2 Yesterday (5) Next Tuesday (3) Prog1 Schedule Quiz3-1 10 PM Today Lab2 Yesterday (5) Next Tuesday (3) Prog1

Program 1 Due Tuesday September 22 by 10 pm: 15 points (two bonus points towards style) Grace Friday September 25 by 10 pm: 15 points After Grace Time: 0 points and you still have to do it! NONE Differences: at least on test 1 (otherwise: F for CS1430) Do it yourself!

Programming Grand Rules You could lose up to 7 points on style! I can check your Style if you print it out or put it into the DropBox.