Computing Sums and Averages 04/17/15. Totaling Up a List Write a program to input a list of four numbers and output the total.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Computer Science 1620 Loops.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
An Introduction to Programming with C++ Fifth Edition
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
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.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
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.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures Repetition or Iteration or Looping Part II.
Count and add list of numbers From user input and from file.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Control Structures RepetitionorIterationorLooping Part I.
A First Book of ANSI C Fourth Edition Chapter 5 Repetition.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
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.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
CIS 115 All Exercises Devry University (Devry) FOR MORE CLASSES VISIT CIS 115 All Exercises Devry University.
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.
ECE Application Programming
Chapter 5: Control Structures II (Repetition)
Controlling execution - iteration
Loop Structures.
A First Book of ANSI C Fourth Edition
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
An Introduction to Programming with C++ Fifth Edition
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Alternate Version of STARTING OUT WITH C++ 4th Edition
A First Book of ANSI C Fourth Edition
A First Book of ANSI C Fourth Edition
For Wednesday No new reading No quiz.
Chapter 2.1 Repetition.
Let’s all Repeat Together
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
Flowcharts and Pseudo Code
Chapter 5: Control Structures II (Repetition)
Objectives You should be able to describe: The while Statement
More Loops Topics Relational Operators Logical Operators for Loops.
Assignment Operators Topics Increment and Decrement Operators
EECE.2160 ECE Application Programming
Presentation transcript:

Computing Sums and Averages 04/17/15

Totaling Up a List Write a program to input a list of four numbers and output the total.

Algorithm 1. Set MAXCOUNT to 4 2.Intialize count to 1, total to while(count <= MAXCOUNT) A)Input num B)Add num to total C)Output total D)Increment count 4.Output “Final total is of” MAXCOUNT “numbers is” total

A First Book of ANSI C, Fourth Edition4 Ensures that any previous value present in the storage locations assigned to the variable total is overwritten and the total starts at a correct value Accumulating statement Computing Sums and Averages Using a while Loop (continued)‏

Algorithm -- How to Average? 1. Set MAXCOUNT to 4 2. total to while(count <= MAXCOUNT) A)Input num B)Add num to total C)Output total 4.Output “Average is” total/MAXCOUNT

A First Book of ANSI C, Fourth Edition6 Calculating an average Computing Sums and Averages Using a while Loop (continued)‏

Participation Write a program that finds the sum of tips earned on five mornings at Flying-M. Ask for the amount earned each day. Output the sum. How would you find the average?

Sentinels

A program can be made much more general by removing the restriction that exactly five numbers are to be entered  The user enters the number of values to be entered.  A sentinel can be used. Value to mark end of list. Cannot be a legitimate data value.

Tips Program with Sentinel Algorithm? tipsSentinels.c

Quiz When We Get Back Quiz Next Time –Lab Quiz on For – loop –Read section 5.4 before then. Read pp for Wed., April 7.

A First Book of ANSI C, Fourth Edition12 Sentinels

Example sentinel.c

Continue Here

A First Book of ANSI C, Fourth Edition15 Sentinels (continued)‏ One useful sentinel in C is the named constant EOF (End Of File)‏ –The actual value of EOF is compiler-dependent, but it is always assigned a code that is not used by any other character –EOF is defined in stdio.h

A First Book of ANSI C, Fourth Edition16 Sentinels (continued)‏

A Program on onyx scores.c

Three Statements Seen in Loops Won't cover: – break – Continue null

The Null Statement A semicolon with nothing preceding it is also a valid statement, called the null statement while(x > 20); Use the null statement where a statement is syntactically required, but no action is needed Be care about accidentally creating a while loop with a null statement.

Participation Write a program to input a line of characters. Output the line in all-caps. The toupper( ) function can be used to change a lowercase letter to an uppercase letter. Stop the input when a "." is input. To get a started program: cp ~jjarboe/allCaps.c.