CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Modular Programming With Functions
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Building Java Programs
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.
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions.
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
Chapter 5 Functions.
Mock test review Revision of Activity Diagrams for Loops,
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.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Scope and Casting. Scope Region of the program where a particular name can be referenced Formal parameters and local variables –can be accessed from within.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Storage Classes.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
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.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Variables and memory addresses
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Expressions Methods if else Statements Loops Potpourri.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure.
CSE202: Lecture 13The Ohio State University1 Function Scope.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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.
1 Generating Random Numbers Textbook ch.6, pg
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
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Recursion.
C Functions Pepper.
Iterative Constructs Review
CMPT 201 Functions.
Quiz 2.
CS1010 Programming Methodology
Recursion.
Chapter 5 - Functions Outline 5.1 Introduction
Recursion.
Reference Parameters.
Functions 2: Stupid Function Tricks
Iterative Constructs Review
CS2011 Introduction to Programming I Loop Statements (II)
Chapter 7: User-Defined Functions II
STORAGE CLASS.
STORAGE CLASS.
Presentation transcript:

CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02

Question 1 a)What is the output ? LineMainFunc1Console 4X = X = 0X = 1 12X = 0X = 11 7X = 00

Question 1 What is the output? LineMainFunc2Console 4X=1,Y= X=1,Y=2Num1= 1, Num2= 2 12X=1,Y=2Num1= 100, Num2= 2 13X=1,Y=2Num1= 100, Num2= X=1,Y=2Num1= 100, Num2= X=1,Y=2Num1=2,Num2=1 12X=1,Y=2Num1= 200, Num2= 1 13X=1,Y=2Num1= 200, Num2= X=1,Y=2Num1= 200, Num2= X=1,Y=21 2

Question 2 Random Function Rand() rand() returns a pseudo random Integer rand() number needs a SEED srand(int seed) reset SEED to be seed If srand() is not called, rand() uses default Create random INTEGER in [a, b]Create random number in [0, 1] How to generate random number in arbitrary interval ? How to generate other distributions ?

Question 2 Compare the two codes : The difference is where the srand(seed) is called For code 1, different random number is generated For code 2, the random number is always the same Because seed is reset in every iteration

Question 3

Storage Class and Scope Global variables: outside functions Global Variables are consider BAD !! Local variables: within functions

Storage classes - Static Static can also be defined within a function Static variable Is initialized at compilation time and retains its value between calls int main(void) { func(); } void func(){ static int count = 0; count++; printf("%d",count); } The function called with the same parameter may return different result

Some Questions from Past Year Paper Note that “%f” by default outputs to 6 decimal places!

Some Questions from Past Year Paper When evaluation, anything not 0 is TRUE, 0 is FALSE When output, TRUE is 1, FALSE is 0

Some Questions from Past Year Paper When use ++x, you increment before you fetch When use x++, you increment after fetch

Some Questions from Past Year Paper Be careful with the stopping condition

Some Questions from Past Year Paper Don’t get confused by the indentation!

Some Questions from Past Year Paper Macron is always direct substitution! Z=((++x) >= (y++)? (++x): (y++));

Some Questions from Past Year Paper (D) X = I = 0 cannot be used in variable declaration

Some Questions from Past Year Paper (A) Short-cut evaluations

Some Questions from Past Year Paper (B) Careful with (int) coercion

Some Questions from Past Year Paper (E) Direct substitution!