CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : 110 220 330 440 550 660 770 880 990 10 100 … Suppose we need.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Overview creating your own functions calling your own functions.
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 ; }
How Create a C++ Program. #include using namespace std; void main() { cout
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS 1400 Chapter 7 ARRAYS. Array variables Simple variables can hold single values int x;// x can hold one integer float y; // y can hold one float Array.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Chapter One Lesson Three DATA TYPES ©
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
C:\Temp\Templates 4 5 Use This Main Program 6.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 5 Function Basics
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CS 2308 Exam I Review.
Counting Loops.
Functions, Part 2 of 3 Topics Functions That Return a Value
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Variables have attributes
CS150 Introduction to Computer Science 1
Chapter 15 - C++ As A "Better C"
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
CMSC 202 Lesson 6 Functions II.
CS150 Introduction to Computer Science 1
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

CS 1400 March 21, 2007 Odds and Ends Review

Reading to the end of a file Example test.txt : … Suppose we need to read these two parallel arrays without knowing how many values are in the file?

Method 1 int a[50], b[50]; // may be larger than needed fin.open(“test.txt"); while (!fin.fail()) {fin >> a[size] >> b[size]; if (!fin.fail()) size++; } cout << “items read: “ << size;

Method 2 int a[50], b[50]; fin.open(“test.txt"); while (fin >> a[size] >> b[size)) {size++; } cout << “items read: “ << size; The file variable itself can be tested for success or failure

Review of static variables… A static variable is; –initialized only once (first time the function is called) –retains its value or stays alive when the function exits

Example: void DisplayNextFive (bool restart) { static int start_value = 0; if (restart) start_value = 0; for (int n=start_value; n<start_value+5; n++) cout << n << endl; start_value += 5; } int main() {DisplayNextFive (false); DisplayNextFive (false); DisplayNextFive (true); DisplayNextFive (false);

Default parameters A function may assign default values to the last parameters in a list using the “=“ operator. Default parameters need not be specified by the caller.

Example 1 void DisplayNextFive (bool restart = false) {static int start_value = 0; if (restart) start_value = 0; for (int n=start_value; n<start_value+5; n++) cout << n << endl; start_value += 5; } int main() {DisplayNextFive (); DisplayNextFive (); DisplayNextFive (true); DisplayNextFive (); DisplayNextFive (true); DisplayNextFive ();

Example 2 int SumTwoToFiveNumbers (int a, int b, int c=0, int d=0, int e=0) {return (a+b+c+d+e); } int main() {cout << SumTwoToFiveNumbers (2, 4, 3, 5, 7) << endl; cout << SumTwoToFiveNumbers (2, 4, 3, 5) << endl; cout << SumTwoToFiveNumbers (2, 4, 3) << endl; cout << SumTwoToFiveNumbers (2, 4) << endl;

Function Overloading C++ matches a function call to a function using the function signature: –function name –number and type of parameters No two functions can have the same signature (but they can have the same name!)

Example int Average (int a, int b)// #1: calculate integer average with rounding { return (int((a + b) / )); } float Average (float a, float b)// #2: calculate average of two float values { return ((a + b) / 2.0); } float Average (float a, float b, float c) // #3: calculate average of three float values { return ((a + b + c) / 3.0); } int main() { int n = 5, k = 8; float x = 5.0, y = 8.0, z = 10.0; cout << Average (n, k) << endl;// calls #1: outputs 7 cout << Average (x, y) << endl;// calls #2: outputs 6.5 cout << Average (x, y, z) << endl;// calls #3: outputs 11.5

What about this?? int Average (int a, int b)// #1: calculate integer average with rounding { return (int((a + b) / )); } float Average (float a, float b)// #2: calculate average of two float values { return ((a + b) / 2.0); } float Average (float a, float b, float c) // #3: calculate average of three float values { return ((a + b + c) / 3.0); } int main() { int n = 5, k = 8; float x = 5.0, y = 8.0, z = 10.0; cout << Average (n, y) << endl;// ambiguous!! cout << Average (5.0, 8.0) << endl;// also ambiguous!!

Floating-point constants are double One possible solution – use double variables double Average (double a, double b) { return ((a + b) / 2.0); } Another possible solution – cast constants to float cout << Average (float(5.0), float(8.0)) << endl;