1 c++ loops sentinel controlled count controlled eof controlled

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

Upload: henry-ford

Post on 14-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

1

C++ Loops

• Sentinel Controlled• Count Controlled• EOF Controlled

Page 2: 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;}

Page 3: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

3

Count Controlled Loops

cin >> theCount;loopCount = 0;while (loopCount < theCount) { // Input // Do Work

loopCount ++; }

Page 4: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

4

EOF Controlled Loops

cin >> aValue; // Prime Readwhile ( !cin.eof() ) { // Do Work

cin >> aValue; }

Prog2!

Page 5: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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

Page 6: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

6

Quadratic Equations

ax2 + bx + c = 0

Formula to compute one root

a

acbbbx

2

4*

Page 7: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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

a

acbbbx

2

4*

Page 8: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

8

C++ Function sqrt() // Header file#include <cmath> float sqrt(float x);// Function Prototype (Declaration)

Function Name: sqrtFunction Type: float (before sqrt) type of the return valueFunction Parameter: x Type of the parameter: float (before x)

Page 9: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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)

Page 10: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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

Page 11: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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

Page 12: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

12

Function Parametersfloat 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.

Page 13: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

13

Function Definition float sqrt(float x){ float root; // Other variables // Compute root return root;}

Function HeaderFunction BodyLocal variables: rootNo input inside the functionActual parameter provides the value

cin >> num;cout << “The square root of “ << num << “is ” << sqrt(num);

Page 14: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

14

Other C++ Math Functions

Header File <math.h> or <cmath>

double sin(double angle);

int ceil(double x);

int floor(double x);

double pow(double base, double exp);

double fabs(double x);

Page 15: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

15

Other C++ Functions

cin.get(char)cin.eof()

Function Prototypesvoid get(char& c);bool eof();

Page 16: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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;

Page 17: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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

Page 18: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

18

User Defined Functionsint 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

Page 19: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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

Page 20: 1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

20

Schedule

Quiz3-4

Due 5 PM today

QuizProg2

Now