1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.

Slides:



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

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Functions 1. Example: Power, Square Root and Absolute values of a number #include … float num; float power, squareRoot, absolute; cout
1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Chapter 6: User-Defined Functions I
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
Chapter 6: User-Defined Functions I
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Chapter 6: Functions Starting Out with C++ Early Objects
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
CPS120: Introduction to Computer Science Lecture 14 Functions.
QuadraticEquation class. Background A quadratic equation is a second-order polynomial equation in a single variable x, ax 2 + bx + c = 0 (with a≠0.)
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
1 CS 1430: Programming in C++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
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.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
Chapter 6: User-Defined Functions I
Functions, Part 2 of 2 Topics Functions That Return a Value
Variables A piece of memory set aside to store data
CMPT 201 Functions.
CSCI 161: Introduction to Programming Function
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Value returning Functions
CS 1430: Programming in C++ No time to cover HiC.
Chapter 6: User-Defined Functions I
Review for Final Exam.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

2 Sentinel Controlled Loops cin >> aValue; // Prime read! while (aValue != END_VALUE) { // Do Work cin >> aValue; }

3 Count Controlled Loops cin >> theCount; loopCount = 0; while (loopCount < theCount) { // Input // Do Work loopCount ++; }

4 EOF Controlled Loops cin >> aValue; // Prime Read while ( !cin.eof() ) { // Do Work cin >> aValue; } Prog2!

5 Loading an Input File in HiC Click the RUN menu Select option "Set Input File …" Click “Load Input” Browse to the file and open it Select the Input (Interactive) radio button Click OK Example: Notes/Hic/Input.cpp InputFile.txt

6 Quadratic Equations ax 2 + bx + c = 0 Formula to compute one root

7 C++ Code to Compute a Root C++ Function sqrt() cin >> coefA >> coefB >> coefC; delta = coefB * coefB - 4 * coefA * coefC; root1 = (-coefB + sqrt(delta)) / (2 * coefA);

8 C++ Function sqrt() // Header file #include float sqrt(float x); // Function Prototype (Declaration) Function Name: sqrt Function Type: float (before sqrt) type of the return value Function Parameter: x Type of the parameter: float (before x)

9 C++ Function Prototype // Function Prototype (Declaration) float sqrt(float x); // Could be double sqrt(double x); double sqrt(double); float sqrt(float); Function type Parameter type (not type)

10 Function Calls // Function Prototype float sqrt(float x); // Function Calls root1 = (-coefB + sqrt(delta)) / (2 * coefA); // Parameter is variable delta cout << “The square root of 10 is “ << sqrt(10); // Parameter is literal value 10 Value = sqrt (coefB * coefB - 4 * coefA * coefC); // Parameter is an expression

11 Return Value root1 = (-coefB + sqrt(delta)) / (2 * coefA); // Used in computation cout << “The square root of 10 is “ << sqrt(10); // Inserted into output stream // Displayed on screen Value = sqrt (coefB * coefB - 4 * coefA * coefC); // Stored in variable Value sqrt(10); // NO! MUST use the return value

12 Function Parameters float sqrt(float x); // Function Prototype // x: Parameter // Formal parameter // In function call: Actual parameter Can be literal value, variable, or expression root1 = (-coefB + sqrt(delta)) / (2 * coefA); // Actual parameter: // delta cout << “The square root of 10 is “ << sqrt(10); // Actual parameter: // 10 Value = sqrt (coefB * coefB - 4 * coefA * coefC); // Actual parameter: // coefB * coefB - 4 * coefA * coefC Actual parameters can be different.

13 Function Definition float sqrt(float x) { float root; // Other variables // Compute root return root; } Function Header Function Body Local variables: root No input inside the function Actual parameter provides the value cin >> num; cout << “The square root of “ << num << “is ” << sqrt(num);

14 Other C++ Math Functions Header File or double sin(double angle); int ceil(double x); int floor(double x); double pow(double base, double exp); double fabs(double x); …

15 Other C++ Functions cin.get(char) cin.eof() Function Prototypes void get(char& c); bool eof();

16 char aChar; int count = 0; // Call function cin.get() cin.get(aChar); // cin >> aChar; // Call function cin.eof() while (!cin.eof()) { count ++; cout << aChar; cin.get(aChar); // cin >> aChar; } cout << “Count: “ << count;

17 User Defined Functions int theFunction(char theType, float theValue); // Function Prototype // Function Name // theFunction // Function Type // int // Parameters // theType // char // theValue // float // Formal Parameters

18 User Defined Functions int theFunction(char theType, float theValue); // Function Prototype // Formal Parameters int intVal; float fValue; char charVal; cin >> charVal >> fValue; intVal = theFunction(charVal, fValue); // Function call // No types // Actual Parameters: // charVal // fValue

19 Summary Function Prototype float sqrt(float x); int theFunction(char type, float value); Function Definition float sqrt(float x) { // compute and return the square root } Function Call root1 = (-coefB + sqrt(delta)) / (2 * coefA); intVal = theFunction(charVal, fValue); Function Parameters (Arguments) Formal Parameters Actual Parameters

20 Schedule Quiz3-4 Due 5 PM today QuizProg2 Now