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?

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
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.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 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 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
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.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
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.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
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.
C++ Programming Lecture 10 Functions – Part II
 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 Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
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.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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
Functions and an Introduction to Recursion
Predefined Functions Revisited
Functions, Part 2 of 2 Topics Functions That Return a Value
Iterative Constructs Review
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
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
Functions October 23, 2017.
Iterative Constructs Review
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Chapter 6: User-Defined Functions I
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
Presentation transcript:

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? 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? l what’s is iterate-and-keep-track idiom? l what’s a nested iterative construct? what is inner/outer loop? How many loops can be nested in C++? 1

Predefined Functions

l C++ comes with libraries of code that can be reused in your programs. The code comes in the form of pre-defined functions n 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 l argument - the value function starts out with; function may have more than one argument; an argument is an expression (thus, can be just a simple constant or variable); l return value - value computed by the function result = sqrt(9.0); l function call (function invocation) - expression consisting of function name followed by arguments in parentheses function accepts arguments of certain type and returns value of certain type; sqrt accepts and returns double l to use a function need to specify include directive: #include l argument may be arbitrary expression result = sqrt(myVar + 9.0); nested function call function call is used as an argument for another function result = sqrt(abs(myVar)); 3

Type Changing Functions l is there a problem with this code? int a=9, b=2; double c=a/b ; l C++ provides functions for explicit conversions between types: function double converts to type double: int a=9, b=2; double c=double(a)/b ; l casting - explicit type conversion is called type l warning: wrong application of casting: int a=9, b=2; double c=double(a/b) ; l note that type conversion may be implicit: example: int a=55; double c = a + 30; integer expression is implicitly converted to double before assignment 4

Random Number Generation l (pseudo) random number generation pre-defined functions are used in programs to create events unpredictable by user (e.g. events in games) need to include n creates a series of numbers, numbers within series are (pseudo) random srand( seed ) – initializes random number generator, needs to be invoked once in the program, no return value seed – selects the (pseudo) random series rand() – returns a new integer random number in the series, can be used multiple times in program the number is from 0 to MAX_RAND – a named constant predefined in n 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; 5

Selecting Random Series with time() time(nullptr) – returns number of seconds since 01/01/1970, good for initializing unique series, needs nullptr is there for historical reasons l 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 time()