COSC 1P02 Introduction to Computer Science 8.1 Cosc 1P02 “For myself, I am an optimist--it does not seem to be much use being anything else.” Winston Churchill.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
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.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Introduction to Computer Science Iteration –the while loop –the for loop Unit 8.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Fundamentals of Python: From First Programs Through Data Structures
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Fundamentals of Python: First Programs
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
General Programming Introduction to Computing Science and Programming I.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Access 2003 Lab 3 Analyzing Data and Creating Reports.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 12: How Long Can This Go On?
Creating your first C++ program
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
COSC 1P02 Introduction to Computer Science 9.1 Cosc 1P02 "A lie gets halfway around the world before the truth has a chance to get its pants on.” Sir Winston.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Reports and Queries Chapter 3 – Access text Reports – Page Queries – Page
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Chapter 5: Control Structures II
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Control Structures RepetitionorIterationorLooping Part I.
Overview Excel is a spreadsheet, a grid made from columns and rows. It is a software program that can make number manipulation easy and somewhat painless.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Introduction to Computing Science and Programming I
Loops BIS1523 – Lecture 10.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 5: Control Structures II
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Repetition-Counter control Loop
Lecture 07 More Repetition Richard Gesick.
Selected Topics From Chapter 6 Iteration
Lecture 4B More Repetition Richard Gesick
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
While Loops.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 5: Control Structures II (Repetition)
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

COSC 1P02 Introduction to Computer Science 8.1 Cosc 1P02 “For myself, I am an optimist--it does not seem to be much use being anything else.” Winston Churchill

COSC 1P02 Introduction to Computer Science 8.2 Class Average  Marks for test in a course  Average is sum divided by number of marks  Running total  sum values as see them  at each point sum is sum of values so far  add next value  initial value (no values so far) is 0  Summation algorithm  Reading data  ASCIIPrompter  Writing results  ASCIDisplayer  Example

ASCIIPrompter Methods

ASCIIDisplayer Methods

COSC 1P02 Introduction to Computer Science 8.6 Reading Data from a File  Tedious and error prone to enter data as program running  Instead create a file of data (text editor/word processor)  Reading data from a file  ASCIIDataFile  fields (tab delimited)  Example  data includes student number  not really a number but text  String  report  header, body (detail lines), summary  separate methods for each part

ASCIIDataFile Methods

COSC 1P02 Introduction to Computer Science 8.8 End of File  Tedious and error prone for user to have to count number of pieces of data  A file contains a finite amount of data  each read consumes one value  eventually a read must happen when there is no more data  called End Of File (EOF)  method isEOF() returns true when last read failed because of EOF  Indefinite loop  need to loop an unknown number of times  loop until something happens  Example  expect if student number then will be mark  count number of students

COSC 1P02 Introduction to Computer Science 8.10 while Statement  An indefinite loop  Syntax  Execution  evaluates condition  if true, executes body  if false, quits (continues after body)  Read to end of file loop  condition always true so executes body  if statement  if condition is true, executes then part  then part is one statement so doesn’t need {}  break statement quits whatever loop it is executed within

COSC 1P02 Introduction to Computer Science 8.13 Fill a Box  Problem  packing to move  Condition  Termination  infinite loop  Generating random values  Math.random()  Output  Summation

COSC 1P02 Introduction to Computer Science 8.14 While...

COSC 1P02 Introduction to Computer Science 8.15 Filling a Box

COSC 1P02 Introduction to Computer Science 8.16 Fill private void fill ( ) { intsize;// capacity of box intamt;// amount in box intitem;// item item to pack in.setLabel("Enter box size"); size = in.readInt(); writeSize(size); amt = 0; while ( amt < size ) { item = (int) (10 * Math.random() + 1); amt = amt + item; writeItem(item,amt); }; writeResult(amt); };// fill Condition checks if the amount put into the box exceeds the capacity. While this condition is true the “while loop” keeps repeating. We put an item into the box of varying size. Calculated using random(). Keep a running total of what is in the box.

COSC 1P02 Introduction to Computer Science 8.17 High and Low Mark  Algorithm  is value higher (lower) the highest (lowest) so far  Initial state?  Double.MAX_VALUE  Independent if s  independent decisions  Finding maximum value pattern

COSC 1P02 Introduction to Computer Science 8.18

COSC 1P02 Introduction to Computer Science 8.19 Finding the Maximum (minimum) Value Programming Pattern

COSC 1P02 Introduction to Computer Science 8.20 private void display ( ) { intnumStd;// number of students doubletotMark;// total of marks doubleaveMark;// average mark doublehighMark;// highest mark doublelowMark;// lowest mark intaStdNum;// one student's student nmber doubleaMark;// one student's mark numStd = 0; totMark = 0; highMark = - Double.MAX_VALUE; lowMark = Double.MAX_VALUE; while ( true ) { aStdNum = in.readInt(); if (in.isEOF() ) break; aMark = in.readDouble(); numStd = numStd + 1; totMark = totMark + aMark; if ( aMark > highMark ) { highMark = aMark; }; if ( aMark < lowMark ) { lowMark = aMark; }; aveMark = totMark / numStd; writeDetail(numStd,aveMark,highMark,lowMark); };// display High Mark.. Initialization of the hi and lo mark. If the new mark is higher then the previous high mark then replace with new mark. The same is done for the low mark, inequality is changed

COSC 1P02 Introduction to Computer Science 8.21 Reports  Most data processing involves production of a report (typically printed)  Report  header  detail lines  summary (footer)  Report format  tabular in nature  each row represents one entity (e.g. student)  similar pieces of data (e.g. mark) in a column  header has a title and a heading for each column  summary may be columnar or free-form

COSC 1P02 Introduction to Computer Science 8.23 ReportPrinter  Support for printed reports in columnar format  title for report centered at top of page  data written to field  field has a name, a label and a width  name used to specify field when writing  label forms part of heading  width is width of column  report designed by setting title and adding fields  fields added L-R across report  handles moving to next line and pagination  Methods  setting title  adding fields  writing into fields  Example  PDFCreator

ReportPrinter Methods

COSC 1P02 Introduction to Computer Science 8.25 The End