Let’s all Repeat Together

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Computer Science 1620 Loops.
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/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Computer Science 1620 Programming & Problem Solving.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Computer Science 1620 Formatting. Suppose you work for the HR dept. of a company you wish to write a program to show their earnings per month Details:
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Chapter 05 (Part II) Control Statements: Part II.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
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.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Introduction to Computer Programming
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Control Structures Sequential execution Transfer of control
while Repetition Structure
for Repetition Structures
Chapter 3 L7.
The Selection Structure
CS150 Introduction to Computer Science 1
Repetition Structures (Loops)
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
CS 1430: Programming in C++ No time to cover HiC.
Starting Out with C++: From Control Structures through Objects
CS150 Introduction to Computer Science 1
CISC181 Introduction to Computer Science Dr
Chapter 3 Input output.
CS150 Introduction to Computer Science 1
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Life is Full of Alternatives
Arithmetic Operations
do/while Selection Structure
Life is Full of Alternatives
Reading from and Writing to Files
Life is Full of Alternatives
Introduction to cout / cin
Functions Divide and Conquer
Reading from and Writing to Files
ADT LIST So far we have seen the following operations on a “list”:
Presentation transcript:

Let’s all Repeat Together 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time We Introduced the while repetition structure Today we will Cover counter and sentinel controlled loops Formatting output 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problems Write a program that reads in the salary of 5 employees and calculates the gross pay We know, before the program runs, how many times the loop will iterate Counter-controlled repetition Write a program that reads an undetermined number of student grades and calculates the average student grade We don’t know, before the program runs, how many times the loop will iterate Sentinel-controlled repetition 9/30/05 CS150 Introduction to Computer Science 1

Counter-Controlled Repetition We know, before we run the program, the number of repetitions that the loop will make Also called definite repetition 11.1: Write a program that interactively reads in the salary of 5 employees and calculates their gross pay. For each employee the program will read in the number of hours worked and the hourly wage. If an employee works over 40 hours then those hours are considered overtime and the employee will be paid one and a half of their hourly wage for each hour 9/30/05 CS150 Introduction to Computer Science 1

Sentinel-Controlled Repetition We have no idea how many times the loop will need to iterate Write a program that reads an undetermined number of student grades and calculates the average student grade How will we know when we’ve read all employee’s salaries? i.e. How will we know when to stop looping? 9/30/05 CS150 Introduction to Computer Science 1

Sentinel-Controlled Repetition Use a sentinel value User types employee salaries until all legitimate salaries have been entered User then types in sentinel value to indicate that there are no more legitimate salaries Also called indefinite repetition Sentinel value must be chosen so that it cannot b confused with legitimate inputs -1 is a good value to use in most cases 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Formatting C++ Output So far, the only formatting that we have done to our output has been adding spaces and blank lines We can also format floating point numbers so that they display a specific number of digits in the fractional part You need to include the preprocessor directive <iomanip> 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Formatting C++ Output cout << "Class average is " << setprecision( 2 ) << fixed << average << endl; setprecision( 2 ) indicates that there should only be 2 digits in the fractional part of the number The default is 6 fixed indicates that the number should appear in the fixed point format i.e. no scientific notation 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Formatting C++ Output Another useful formatting operator is setw This is also part of the <iomanip> library and is in the std namespace Format: cout << setw(12) << temp; This will display the value stored in temp in a space 12 characters wide By default the output will be right-justified 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Formatting C++ Output int binary = 1010; int decimal = 10; cout << setw(7) << “decimal”; cout << setw(10) << “binary”; cout << setw(7) << decimal; cout << setw(10) << binary; 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write a program that reads an undetermined number of student grades and calculates the average student grade The answer is on the following slides 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution #include “stdafx.h” #include <iostream> using namespace std; int main() { int gradeCounter; int grade; double average; double total; total = 0; gradeCounter = 0; 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution cout << "Enter grade, -1 to end: "; cin >> grade; while ( grade != -1 ) { total = total + grade; gradeCounter = gradeCounter + 1; } if ( gradeCounter != 0 ) average = total / gradeCounter; cout << "Class average is " << average << endl; else cout << "No grades were entered" << endl; 9/30/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered Counter and sentinel-controlled repetitions Formatting output Readings P. 83 - 94 counter and sentinel loops P. 93, p. 113 formatting output 9/30/05 CS150 Introduction to Computer Science 1