1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Functions Computational assistants.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
True or false A variable of type char can hold the value 301. ( F )
Chapter 10.
C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
Computer Science 1620 Loops.
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
Function basics – pp Computational assistants – improves clarity and enable software reuse. JPC and JWD © 2002 McGraw-Hill, Inc.
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 7 Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
1 Chapter 7 Functions. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Chapter 8 Functions. Chapter 8 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Functions. 2 Chapter 7 Topics  Writing a Program Using Functional Decomposition  Writing a Void Function for a Task  Using Function Arguments and.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
C++: Functions, Program Compilation, Libraries Modified from CS101 slides, which are by JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 05 (Part II) Control Statements: Part II.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
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.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Bill Tucker Austin Community College COSC 1315
Chapter 8 Functions.
ifstreams and ofstreams
CS-103 COMPUTER PROGRAMMING
Programming Fundamentals
COMS 261 Computer Science I
Intro to Programming Week # 4 Control Structure Lecture # 8
Today’s Lecture I/O Streams Tools for File I/O
Chapter 3: Input/Output
Chapter 3 Input output.
Control Structures Part 1
Programs written in C and C++ can run on many different computers
COMS 261 Computer Science I
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
CHAPTER 4 File Processing.
COMS 261 Computer Science I
C++ Programming Basics
ifstreams and ofstreams
COMS 261 Computer Science I
Chapter 8 Functions.
Presentation transcript:

1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14

2 Announcements Read Chap. 5 for lab on Thursday

3 Review Exam 1 –Any questions Iteration constructs File I/O

4 Outline Do-while loop Functions

Iteration Options while loop while (expression is true) { action } // end while(…) for loop for (int i = 0; i < n; i++) { action }

Iteration Do’s Key Points –Make sure there is a statement that will eventually terminate the iteration criterion The loop must stop! –Make sure that initialization of loop counters or iterators is properly performed –Have a clear purpose for the loop Document the purpose of the loop Document how the body of the loop advances the purpose of the loop

The Do-While Statement The while and for loops make the test before the action is performed –Sometimes we know the action part of the loop will be executed at least once –Perform the test after the action Syntax do { Action } while (Expression)

The Do-While Statement Semantics –Execute Action –If Expression is true then execute Action again Action is always executed at least once –Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces

The Do-While Statement Expression Action false Syntax do { –Action } while (Expression) Evaluate the action at least once

Waiting for a Proper Reply char reply; do { cout << "Decision (y, n): "; if (cin >> reply ) reply = tolower(reply ); else reply = 'n'; } while ((reply != 'y') && (reply != 'n'));

Functions Previous examples –Programmer-defined functions main() –Library-defined functions cin.get() string member functions size() Don’t reinvent the wheel! There are lots of libraries out there

Terminology A function is invoked by a function call / function invocation y = f(a);

Terminology A function call specifies –The function name The name indicates what function is to be called y = f(a); –The actual parameters to be used in the invocation The value(s) are the information the called function requires from the invoking function y = f(a); y = sqrt(5.0);

Terminology A function call produces a return value –The return value is the value of the function call y = f(a); y = sqrt(5.0); –Recall, the main function returns an integer

Invocation Process Flow of control is temporarily transferred to the invoked function –Correspondence established between actual parameters of the invocation with the formal parameters of the definition cout << " Enter number: " ; double a; cin >> a; y = f(a); cout << y; –Value of a is copied to x double f(double x) { double result = x*x + 2*x + 5; return result; }

Invocation Process –Local objects are maintained in the invocation’s activation record –main() has a record cout << " Enter number: " ; double a; cin >> a; y = f(a); cout << y; –Activation record is large enough to store values associated with each object defined by the function double f(double x) { double result = x*x + 2*x + 5; return result; }

Invocation Process –Other information may also be maintained in the invocation’s activation record cout << " Enter number: " ; double a; cin >> a; y = f(a); cout << y; –Possibly a pointer to the current statement being executed and a pointer to the invoking statement double f(double x) { double result = x*x + 2*x + 5; return result; }

Invocation Process –Next statement executed is the first one in the invoked function cout << " Enter number: " ; double a; cin >> a; y = f(a); cout << y; double f(double x) { double result = x*x + 2*x + 5; return result; }

Invocation Process –After function completes its action, flow of control is returned to the invoking function –The return value is used as value of the function invocation cout << " Enter number: " ; double a; cin >> a; y = f(a); cout << y; –Y is assigned the value returned from the function double f(double x) { double result = x*x + 2*x + 5; return result; }

Invocation Process Sequential instruction execution Main Memory Function call invocation interrupts sequential instruction execution Sequential instruction execution within the function Function returns to caller

Execution Process Function body of invoked function is executed Flow of control then returns to the invocation statement The return value of the invoked function is used as the value of the invocation expression

Function Prototypes Before a function can appear in an invocation its interface must be specified –Prototype or complete definition int max(int a, int b)

Function Prototypes Interfaces, also called prototypes are normally kept in header files –Header files can be associated with libraries

Libraries Library –Collection of functions, classes, and objects grouped by commonality of purpose –Include statement provides access to the names and descriptions of the library components (function prototypes) –Linker connects program to actual library definitions

Basic Compilation Process

26 Some Standard Libraries fstream –File stream processing assert –C-based library for assertion processing iomanip –Formatted input/output (I/O) requests ctype –C-based library for character manipulations

27 Some Standard Libraries math –C-based library for trigonometric and logarithmic functions Note –C++ has many other libraries –There are many libraries available in netland

Library Header Files Describes library components Typically contains –Function prototypes Interface description –Class definitions Sometimes contains –Object definitions Example: cout and cin in iostream

Library Header Files Typically do not contain function definitions –Definitions are in source files –Access to compiled versions of source files provided by a linker

30 #include using namespace std; int main() { cout << "Enter Quadratic coefficients: "; double a, b, c; cin >> a >> b >> c; if ( (a != 0) && (b*b - 4*a*c > 0) ) { double radical = sqrt(b*b - 4*a*c); double root1 = (-b + radical) / (2*a); double root2 = (-b - radical) / (2*a); cout << "Roots: " << root1 << " " << root2; } else { cout << "Does not have two real roots"; } return 0; } Invocation Library header files

31 #include #include // file stream library using namespace std; int main() { ifstream fin("mydata.txt"); int ValuesProcessed = 0; float ValueSum = 0; float Value; while (fin >> Value) { ValueSum += Value; ++ValuesProcessed; } if (ValuesProcessed > 0) { ofstream fout("average.txt"); float Average = ValueSum / ValuesProcessed; fout << "Average: " << Average << endl; return 0; } else { cerr << "No list to average" << endl; return 1; } File Input

32 ifstream sin("in1.txt"); // extract from in1.txt ofstream sout("out1.txt"); // insert to out1.txt string s; while (sin >> s) { sout << s << endl; } sin.close(); // done with in1.txt sout.close(); // done with out1.txt sin.open("in2.txt"); // now extract from in2.txt sout.open("out.txt", // now append to out2.txt (ios_base::out | ios_base::app)); while (sin >> s) { sout << s << endl; } sin.close(); // done with in2.txt sout.close(); // done with out2.txt File Input and Output