Iterative Constructs Review

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 5 Functions.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 5: Control Structures II (Repetition)
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
CS0007: Introduction to Computer Programming Introduction to Arrays.
1 Lecture 3 Part 1 Functions with math and randomness.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
CMSC 1041 Functions II Functions that return a value.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions. Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in the form of predefined functions.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Functions.
Chapter 6: User-Defined Functions I
Why they aren't really random
Chapter 5: Control Structures II (Repetition)
Functions and an Introduction to Recursion
Chapter 3 Assignment and Interactive Input.
Predefined Functions Revisited
Functions, Part 2 of 2 Topics Functions That Return a Value
Iterative Constructs Review
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Chapter 5 Function Basics
CMPT 201 Functions.
Branching Constructs Review
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Random Number Generation
Chapter 6 - Functions Outline 5.1 Introduction
Functions and an Introduction to Recursion
Functions October 23, 2017.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
A First Book of ANSI C Fourth Edition
Chapter 6: User-Defined Functions I
CS150 Introduction to Computer Science 1
Chapter 5: Control Structures II (Repetition)
Predefined Functions Revisited
CS 144 Advanced C++ Programming January 31 Class Meeting
6 Functions.
Assignment Operators Topics Increment and Decrement Operators
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Iterative Constructs Review what is a block? what is special about declaring a variable inside a block? what is the difference between while and do-while? Can one replace the other? what does for do? Why is it needed? what is init-statement, expression, post-statement in for? Can for replace while/do-while? is reverse possible? what is loop variable? Where is it declared in for? What is its scope? how is break used inside an iterative construct? what is continue? how is it used? what is iterate-and-keep-track idiom? what is tracking variable? what’s a nested iterative construct? what is inner/outer loop? How many loops can be nested in C++?

Predefined Functions library code

Name, Return Value, Argument C++ comes with libraries of code that can be reused in your programs. The code comes in the form of pre-defined functions what is the program that adds pre-defined functions to your code to produce executable? function name – identifier distinguishing the function from others; example – square root function: sqrt return value - value computed by the function argument – the value function starts out with; function accepts arguments of certain type and returns the value of certain type sqrt accepts and returns double to use a function, need to specify particular include directive: to use sqrt #include <cmath>

Invocation, Argument Variants function call (function invocation) – expression consisting of function name followed by arguments in parentheses result = sqrt(9.0); semantics: argument is evaluated, function is executed, return value replaces function invocation invocation in expression – return value replaces invocation in expression evaluation result = sqrt(9.0) – myVar*5; standalone invocation – return value is ignored srand(55); argument variants function may have more than one argument result = pow(myVar, 55); an argument is an (arbitrary) expression result = sqrt(myVar*2 + 9.0); nested function call: use of one function call as argument to another result = sqrt(abs(myVar));

Type Changing Functions is there a problem with this code? int a=9, b=2; double c=a/b; casting – explicit type conversion static_cast<type> converts to type example: double c=static_cast<double>(a)/b; wrong application of casting: double c=static_cast<double>(a/b); coercion – implicit type conversion int a=55; double c = a + 30; integer expression coerced to double before assignment

Random Number Generation (pseudo) random number generation pre-defined functions are used to create events unpredictable by user (e.g. events in games) need to include <cstdlib> generates a series of numbers, numbers within single series are (pseudo) random srand(seed) – initializes random number generator, needs to be invoked once in the program, no return value seed – integer, selects the (pseudo) random series rand() – returns a new integer random number in the series, can be used multiple times in program, no argument the number is from 0 to MAX_RAND – a named constant declared in <cstdlib> ranged random idiom: to get a random number in a specific range, take a remainder of that range. Example, random number between 0 and 9 can be obtained as: int myRandValue = rand() % 10;

Selecting Random Series with time() time(nullptr) – returns number of seconds since 01/01/1970, good for initializing unique series, needs <ctime> nullptr is there for historical reasons selecting series srand(1); - repeats series every time you run your program – good for debugging srand(time(nullptr)); - selects unpredictable series every time you run your program – good for final compilation srand() rand() invocation seed 1 2 3 4 5 6 1 41 18467 6334 26500 19169 15724 1 41 18467 6334 26500 19169 15724 2 45 29216 24198 17795 29484 19650 2 45 29216 24198 17795 29484 19650 time() 14987 9212 26926 31604 11201 32623