1 Lab Session-IV CSIT121 Spring 2002 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises Lab Exercise for Demo.

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

Revision.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Writing and Testing Programs Drivers and Stubs Supplement to text.
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.
Overview creating your own functions calling your own functions.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
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-3 CSIT121 Fall 2003 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
The If/Else Statement, Boolean Flags, and Menus Page 180
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.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Introduction to Problem SolvingS1.2.1 Bina © 1998 Liran & Ofir Introduction to Problem Solving Programming in C.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
How to start Visual Studio 2008 or 2010 (command-line program)
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
CPS120: Introduction to Computer Science Functions.
Previously Repetition Structures While, Do-While, For.
First steps Jordi Cortadella Department of Computer Science.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Objective: Students will be able to: Declare and use variables Input integers.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
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.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Fundamental Programming Fundamental Programming Introduction to Functions.
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
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Basic concepts of C++ Presented by Prof. Satyajit De
Bill Tucker Austin Community College COSC 1315
C++ Arrays.
User-defined Functions
Compound Assignment Operators in C++
Value returning Functions
User-defined Functions
CS150 Introduction to Computer Science 1
Counting Loops.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Introduction to Functions
CS150 Introduction to Computer Science 1
Introduction to Algorithms and Programming COMP151
Presentation transcript:

1 Lab Session-IV CSIT121 Spring 2002 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises 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 should we do in order to use the built-in mathematical functions? What is a function prototype?

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

4 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; }

5 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;

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

7 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;

8 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; }

9 Top Down Design Problem You are required to develop a software for solving a problem. The problem is described as follows: A List of four numbers (A,B,C,D) is given. Compute its mean, largest number and smallest number

10 Top Down Design Underline the nouns and verbs for data modeling and algorithm development Answer: A List of four integers (A,B,C,D) is given. Compute its mean, largest number and smallest number

11 Data Design Now we can determine input data and its type Answer: int A, B, C, D Also the output data and its type can be determined float mean int largest, smallest

12 Algorithm Design We develop an algorithm for this problem ALGORITHM 1. Read A, B, C, D 2. Determine the mean 3. Determine the largest number 4. Determine the smallest number 5. Display the results

13 Algorithm Refinement Next step is to refine the algorithm ALGORITHM 1. Read A, B, C, D 2. Determine the mean 2.1 Mean is (A+B+C+D)/4 3. Determine the largest number 3.1 Compare A&B and C&D and extract larger value 3.2 Compare extracted values and report the larger one 4. Determine the smallest number 4.1 Compare A&B and C&D and extract smaller value 4.2 Compare extracted values and report thesmallerr one 5. Display the results

14 Coding for Main Function Now Coding for Main() can be done #include using namespace std; int largest_number(int, int, int, int); int smallest_number(int, int, int, int); void main() { int A,B,C,D; float mean; int largest, smallest; cout <<"Input all numbers";

15 Coding for Main Function cin >>A>>B>>C>>D; mean = float(A+B+C+D) / 4.0; largest = largest_number(A,B,C,D); smallest = smallest_number (A,B,C,D); cout<<"Here are the values” <<setw(8)<<mean<<setw(8) <<largest <<setw(8)<<smallest<<endl; }

16 Coding for Other functions Coding for largest_number() int largest_number(int k, int j, int l, int m) { int temp1, temp2; if (k>=j) temp1=k; else temp1=j; if (l>=m) temp2=l; else temp2=m; if (temp1>=temp2) return temp1; else return temp2; }

17 Coding for Other Functions Coding for smallest_number int smallest_number(int k, int j, int l, int m) { int temp1, temp2; if (k <= j) temp1=k; else temp1=j; if (l<=m) temp2=l; else temp2=m; if (temp1<=temp2) return temp1; else return temp2; }

18 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 if--else clause as shown below for determining odd numbers: if ((number%2)!= 0) cout<<“odd”;