1 CS 1430: Programming in C++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.

Slides:



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

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
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 Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
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.
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.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
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.
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.
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.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
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
Variables A piece of memory set aside to store data
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
CS 1430: Programming in C++ No time to cover HiC.
Value returning Functions
CS 1430: Programming in C++ No time to cover HiC.
Chapter 6: User-Defined Functions I
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 1430: Programming in C++.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

1 CS 1430: Programming in C++

2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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

4 Count Controlled Loops cin >> theCount; loopCount = 0; while (loopCount < theCount) { // Input inside the loop cin >> aValue; // Do Work loopCount ++; }

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

6 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

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

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

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

10 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

11 Function Calls // Function Prototype float sqrt(float x); float sqrt(float); // 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

12 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 theValue = sqrt (coefB * coefB - 4 * coefA * coefC); // Stored in variable Value sqrt(delta); // Good C++ statement? // NO! MUST use the return value!

13 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.

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

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

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

17 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

18 Schedule Quiz3-4 Due 10 PM today Prog2 Test1 Quiz1 Now