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

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
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.
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]); }
Computer Science 1620 Programming & Problem Solving.
1 Lab Session-3 CSIT121 Fall 2003 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
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.
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.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
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.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
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.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
First steps Jordi Cortadella Department of Computer Science.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
1 Original Source : and Problem and Problem Solving.ppt.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
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.
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.
1 Lab Session-IV CSIT121 Spring 2002 Some Questions Scope of Variables Top Down Design Problem The Solution Lab Exercises Lab Exercise for Demo.
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.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CS 240 Computer Programming 1
Problem Solving and Program Design. Problem Solving Process Define and analyze the problem. Develop a solution. Write down the solution steps in detail.
Recursion Chapter 12.
Bill Tucker Austin Community College COSC 1315
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Value returning Functions
CS150 Introduction to Computer Science 1
Counting Loops.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Introduction to Functions
Presentation transcript:

1 Lab Session-IV CSIT121 Fall 2000 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 How can we 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 Exercises Develop a program that calls the math library function pow(x,y) for calculating x to the power y. Then develop your own version of pow(x,y). This version should check and report error if x=0. Also it should return value 1 if y=0 and value x if y=1 without going into calculations.