Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.

Slides:



Advertisements
Similar presentations
Chapter 6: User-Defined Functions I
Advertisements

Chapter 5: Control Structures II (Repetition)
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6: User-Defined Functions I
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 5: Control Structures II (Repetition)
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Guide To UNIX Using Linux Third Edition
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
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.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CPS120: Introduction to Computer Science Decision Making in Programs.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
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). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Scis.regis.edu ● CS-361: Control Structures Week 2 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Topic 4: Looping Statements
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Control Structures II (Repetition)
User-Defined Functions
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Chapter 5: Control Structures II (Repetition)
Presentation transcript:

Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1

Control Structures II (Repetition) 2

Why Is Repetition Needed? Repetition allows efficient use of variables Can input, add, and average multiple numbers using a limited number of variables For example, to add five numbers: ◦ Declare a variable for each number, input the numbers and add the variables together ◦ Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers 3

while Looping (Repetition) Structure Syntax of the while statement: expression acts as a decision maker and is usually a logical expression statement is called the body of the loop The parentheses are part of the syntax 4

while Looping (Repetition) Structure (cont’d.) 5

6

The body of the while executes when the expression evaluates to true The expression checks whether a variable called loop control variable (LCV) satisfies certain condition Previous example i was LCV LCV should be initialized and eventually make the expression evaluate to false (We do this by updating LCV) 7

Generally while loops are written in the following form: /*initialize loop control variable*/ while (expression){ /*expression tests LCV*/ //update LCV } 8

while Looping (Repetition) Structure (cont’d.) 9

Case 1: Counter-Controlled while Loops When you know exactly how many times the statements need to be executed ◦ Use a counter-controlled while loop 10

Example Ch5_CounterControl.cpp 11

Case 2: Sentinel-Controlled while Loops Sentinel variable is tested in the condition Loop ends when sentinel is encountered 12

Example Ch5_SentinelControl.cpp 13

Case 3: Flag-Controlled while Loops Flag-controlled while loop: uses a bool variable to control the loop 14

Example Ch5_FlagControlledLoop.cpp 15

Case 3: Flag-Controlled while Loops Flag-controlled while loop: uses a bool variable to control the loop 16

Example Ch5_FlagControlledLoop.cpp 17

Case 4: EOF-Controlled while Loops End-of-file (EOF)-controlled while loop: when it is difficult to select a sentinel value 18

eof Function The function eof can determine the end of file status eof is a member of data type istream Syntax for the function eof : Ch5_EOFControlledLoop.cpp 19

for Looping (Repetition) Structure for loop: called a counted or indexed for loop Syntax of the for statement: The initial statement, loop condition, and update statement are called for loop control statements 20

for Looping (Repetition) Structure (cont’d.) 21

for Looping (Repetition) Structure (cont’d.) 22

for Looping (Repetition) Structure (cont’d.) 23

for Looping (Repetition) Structure (cont’d.) The following is a semantic error: 24

The following is a legal (but infinite) for loop: for (;;) cout << "Hello" << endl; 25

for Looping (Repetition) Structure (cont’d.) 26

do…while Looping (Repetition) Structure Syntax of a do...while loop: The statement executes first, and then the expression is evaluated ◦ As long as expression is true, loop continues To avoid an infinite loop, body must contain a statement that makes the expression false 27

do…while Looping (Repetition) Structure (cont’d.) The statement can be simple or compound Loop always iterates at least once 28

do…while Looping (Repetition) Structure (cont’d.) 29

do…while Looping (Repetition) Structure (cont’d.) 30

While Loops Exercises Powerpoint on cs150 website Documents: good examples for exam questions workpage_whiles_ifs/ workpage_whiles_ifs/ ues_Sept_15_2015.doc ues_Sept_15_2015.doc 31

Examples arithmetic_quiz.cpp uesday_codeExamples/ uesday_codeExamples/ hursday_codeExamples/students/ hursday_codeExamples/students/ 32

User-defined functions 33

A C++ program is a collection of functions. main(){} Short programs: Programming instructions are packed in a function main. For larger programs it is not practical break the program into manageable pieces. 34

Functions (modules) They are like mini programs that can be combined to form larger programs They allow complicated programs to be divided into manageable pieces 35

Functions In C++, a function is similar to that of a function in algebra ◦ It has a name ◦ It does some computation It is a rule or correspondence between values f(x) = 2x +5 f(1) = 7 1 is argument, 7 is corresponding value of the function 36

Predefined Functions Functions can be user-defined or predefined Some of the predefined mathematical functions are: sqrt(x) pow(x, y) floor(x) calculates the largest whole number that is less than or equal to x 37

Predefined Functions (cont'd.) pow (2.0, 3) = 8.0 pow is double sqrt (4) = 2 floor(48.79) = 48.0 the largest whole number that is less than or equal to

Predefined Functions (cont'd.) Predefined functions are organized into separate libraries ◦ I/O functions are in iostream header ◦ cmath header contains math functions ◦ To use predefined functions, you must include the header file using an include statement 39

40

41

Example Example_6-1.cpp 42

User-Defined Functions You can use functions in a program again and again (previous examples, pow function) C++ does not provide every function that you will ever need You must learn how to write your own functions 43

User-Defined Functions Value-returning functions: have a return type ◦ Return a value of a specific data type using the return statement ◦ Remember main function and return 0? Void functions: do not have a return type ◦ Do not use a return statement to return a value 44

Value-Returning Functions To use these functions, you must: ◦ Include the appropriate header file in your program using the include statement ◦ Know the following items:  Name of the function  Number of parameters, if any  Data type of each parameter  Data type of the value returned: called the type of the function 45

Value-Returning Functions (cont’d.) Can use the value returned by a value- returning function by: ◦ Saving it for further calculation ◦ Ex: x = pow (3.0,5.0) ◦ Using it in some calculation ◦ Ex: area = PI * pow ( radius, 2.0) ◦ Printing it ◦ Ex: cout << abs (-5) ; 46

Value-Returning Functions (cont’d.) A value-returning function is used in an assignment an output statement 47

Value-Returning Functions (cont’d.) Heading (or function header): first line of the function ◦ Example: int abs(int number) Formal parameter: variable declared in the heading ◦ Example: number 48

Another Example: Heading of function: double pow (double base, double exponent ); Formal parameters are: base and exponent 49

double u = 2.5; double v = 3.0; double x,y; x = pow (u, v); y = pow (2.0, 3.2); Value of u is copied into base Value of v is copied into exponent u and v are actual parameters Same for 2.0 and 3.2 ! 50

Value-Returning Functions (cont’d.) Formal parameter: variable declared in the heading Actual parameter: variable or expression listed in a call to a function ◦ Example: x = pow(u, v) 51

Syntax: Value-Returning Function Syntax: functionType is also called the data type or return type (the value that the function returns) Statements (declaration or executable) 52

Syntax: Formal Parameter List 53

Function Call Syntax to call a value-returning function: 54

Syntax: Actual Parameter List Syntax of the actual parameter list: Formal parameter list can be empty: A call to a value-returning function with an empty formal parameter list is: 55

return Statement Function returns its value via the return statement ◦ It passes this value outside the function Syntax: In C++, return is a reserved word When a return statement executes ◦ Function immediately terminates When a return statement executes in the function main, the program terminates 56

Syntax: return Statement (cont’d.) 57

Function Prototype Function prototype: function heading without the body of the function. Comes before main. It’s not a definition. Gives the program the name of the function. Syntax: Data type of each parameter must be specified 58

Example Ch6_Largest.cpp 59

Value-Returning Functions: Some Peculiarities 60

Value-Returning Functions: Some Peculiarities (cont’d.) 61

Example Example_6-3.cpp Example_6-4_RollDice.cpp Example 6-6_CableCompanyBill.cpp 62

Flow of Compilation and Execution Execution always begins at the first statement in the function main Function prototypes appear before any function definition ◦ Compiler compiles these first Compiler can then correctly translate a function call 63

Void Functions User-defined void functions can be placed either before or after the function main If user-defined void functions are placed after the function main ◦ The function prototype must be placed before the function main Void function does not have a return type 64

Void Functions (cont’d.) Formal parameters are optional Void function definition syntax: 65

Void Functions (cont’d.) Formal parameter list syntax: Function call syntax: Actual parameter list syntax: 66

Void Functions (cont’d.) Value parameter: a formal parameter that receives a copy of the content of corresponding actual parameter Reference parameter: a formal parameter that receives the location (memory address) of the corresponding actual parameter 67

Value Parameters If a formal parameter is a value parameter: ◦ The value of the corresponding actual parameter is copied into it ◦ Formal parameter has its own copy of the data During program execution ◦ Formal parameter manipulates the data stored in its own memory space 68

Reference Variables as Parameters If a formal parameter is a reference parameter ◦ It receives the memory address of the corresponding actual parameter During program execution to manipulate data ◦ Changes to formal parameter will change the corresponding actual parameter 69

Reference Variables as Parameters (cont’d.) Reference parameters are useful in three situations: ◦ Returning more than one value ◦ Changing the actual parameter ◦ When passing the address would save memory space and time 70

Value and Reference Parameters and Memory Allocation When a function is called ◦ Memory for its formal parameters and its local variables is allocated in the function data area For a value parameter, the actual parameter’s value is copied into the formal parameter’s memory cell ◦ Changes to the formal parameter do not affect the actual parameter’s value 71

Value and Reference Parameters and Memory Allocation (cont’d.) For a reference parameter, the actual parameter’s address passes to the formal parameter Both formal and actual parameters refer to the same memory location During execution, changes made to the formal parameter’s value permanently change the actual parameter’s value 72

Example Example 6-15.cpp 73

Scope of an Identifier Scope of an identifier: where in the program the identifier is accessible Local identifier: identifiers declared within a function (or block) Global identifier: identifiers declared outside of every function definition C++ does not allow nested functions – Definition of one function cannot be included in the body of another function 74