1 Lab Session-3 CSIT121 Fall 2003 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Exercise 2.
Revision.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
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.
1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
1 Lab Session-IV CSIT121 Fall 2000 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises Lab Exercise for Demo.
1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter.
1 Lab Session-9 CSIT221 Fall 2002 Lab Exercise Based on operator overloading (Demo Required)
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab.
Chapter 4 Summation.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game.
1 Lab Session-VIII CSIT-121 Fall 2000 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
1 Lab Session-11 CSIT 121 Fall 2003 Using arrays in functions Programming Exercise.
The If/Else Statement, Boolean Flags, and Menus Page 180
What is the out put #include using namespace std; void main() { int i; for(i=1;i
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.
1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
1 Lab Session-3 CSIT221 Fall 2002 b Exception Handling b Lab Exercise (Demo Required)
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Objective: Students will be able to: Declare and use variables Input integers.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
1 Lab Session-IV CSIT121 Spring 2002 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises Lab Exercise for Demo.
User-Defined Functions (cont’d) - Reference Parameters.
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
CS 240 Computer Programming 1
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Introduction to Programming
Chapter 8 Exercises.
Bill Tucker Austin Community College COSC 1315
A Lecture for the c++ Course
Chapter 5 Classes.
Lab Session-9 CSIT-121 Spring 2005
Global & Local Identifiers
Static Data Member and Functions
User-defined Functions
While Loop Design ENGI 1020 Fall 2018.
User-defined Functions
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Code::Block vs Visual C++
Namespaces How Shall I Name Thee?.
Pointers & Functions.
TOPIC: FUNCTION OVERLOADING
Introduction to Algorithms and Programming COMP151
Presentation transcript:

1 Lab Session-3 CSIT121 Fall 2003 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo

2 Some Questions What will be the result? 15/12 and 15.0/12.0 What is the order of execution? A * B - E / C What is a function prototype? What is the output of the following program and why?

3 Sample Code #include using namespace std; void main() { int candies; int kids; int myshare, kidshare; candies=29; kids=5; kidshare = candies/kids; cout<<"Each kid gets "<<kidshare<<endl; cout<<"I keep "<<candies - kidshare*kids<<endl; cout<<"Now I act mean"<<endl; kidshare = candies++/kids; cout<<"Each kid gets "<<kidshare<<endl; cout<<"And I keep "<<candies - kidshare*kids<<endl; }

4 Scope of Global Variables #include using namespace std; void prettyprint(); int length; void main() { length=24;

5 Scope of Global Variables cout << "I can refer to length here" << length<<endl; prettyprint(); } void prettyprint () {length=26; cout << "I can also refer to length here"<<length<<endl; }

6 Scope of Variables Declared in Main Function #include using namespace std; void prettyprint(); void main() { int length=24; cout << "I can refer to length here" << length<<endl;

7 Scope of Variables Declared in Main Function prettyprint(); } void prettyprint () { cout << "I cannot refer to length here"<<endl; length=26; }

8 Scope of Variables Declared in Any Function Except Main #include using namespace std; void prettyprint(); void main() { cout << "I cannot refer to length here" <<endl; length=24;

9 Scope of Variables Declared in Any Function Except Main prettyprint(); } void prettyprint () { int length; cout << "I can only refer to length here"<<length<<endl; length=26; }

10 Lab Exercise for Demo Develop a program using top down design approach that accepts 5 values from the user and then calls a function to determine and print the odd numbers from this list. Use the if clause as shown below for determining odd numbers: if ((number%2)!= 0) cout<<“odd”; Make sure that all the variables are declared as global variables