Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14."— Presentation transcript:

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

2 2 Announcements Read Chap. 5 for lab on Thursday

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

4 4 Outline Do-while loop Functions

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

6 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

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

8 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

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

10 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'));

11 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

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

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

14 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

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

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

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

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

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

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

21 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

22 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)

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

24 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

25 Basic Compilation Process

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

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

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


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

Similar presentations


Ads by Google