Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

CMPUT 101 Lab # 5 October 22, :00 – 17:00.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006.
Chapter 4 Summation.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Chapter 02 (Part III) Introduction to C++ Programming.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Overview Go over parts of quiz? Another iteration structure for loop.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
for( 起始條件 ; 判斷式 ; 條件運算 ){ // 迴圈內容 } while( 判斷式 ){ // 迴圈內容 } do{ // 迴圈內容 } while( 判斷式 ) ;
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Sentinel Loops. while Syntax while(expression) statement.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
while Repetition Structure
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
לולאות קרן כליף.
Conditional Construct
Counting Loops.
Chapter 2.2 Control Structures (Iteration)
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS1201: Programming Language 2
Statements and flow control
Fundamental Programming
Repetition Statements (Loops) - 2
Presentation transcript:

Chapter five exercises

a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false

2. y = 1 and count =

6.Sum = Sum = 157

10. sum = 0; count = 0; while (count < 10) { cin >> num; sum = sum + num; count++; } 13. a. 18 b. 14 c. false

17. a. * b. infinite loop c. infinite loop d. **** e. ****** f. ***

19.WHAT IS DONE, OFTEN IS NOT DONE WELL.

24. a. The loop is executed 5 times; Output: b.The loop is executed 4 times; Output: c.The loop is executed 1 time; Output: 7 20 d.The loop is executed 3 times; Output: e.The loop is executed 3 times; Output: f.The loop is executed 0 times; Output: 5 30

#include using namespace std; int main() { int num;//input number int digit;//one digit of the number int sum = 0;// some of the digits int pwr = 0;// cout << "Enter an integer: "; cout << "The digits of " << num << " are: "; if (num < 0)num = -num; //Find the highest power of 10 that divides the number while ( num > pow(10,pwr+1)) pwr++;

while (num > 0) { digit = num / static_cast (pow(10, pwr)); num = num % static_cast (pow(10, pwr)); cout << digit << " "; sum = sum + digit; pwr--; } if (pwr != -1) //Either num is 0 or there are trailing zeros in num while (pwr != -1) { cout << 0 << " "; pwr--; } cout << endl; cout << "The sum of the digits = " << sum << endl; return 0; }

#include using namespace std; int main() { int num; cout << "Enter an integer: "; cin >> num; cout << num << " with digits reversed is "; if (num < 0) { num = -num; cout << "-"; } while (num > 0) { cout << num % 10; num = num / 10; } cout<<endl; return 0; }

#include using namespace std; const int N = 20; int main () { int counter; //loop control variable int number; //variable to store the new number int sumOdds = 0; //variable to store the sum of odd integers int sumEvens = 0; //variable to store the sum of even integers cout << "Please enter " << N << " integers" << endl; for (counter = 1; counter <= N; counter++) { cin >> number;//read number if (number % 2 == 0) sumEvens = sumEvens + number;//update even sum else sumOdds = sumOdds + number;//update odd sum } cout << endl; cout << "Even sum: " << sumEvens << endl; cout << "Odd sum: " << sumOdds << endl; return 0; }