1 c++ loops sentinel controlled count controlled eof controlled

Post on 14-Jan-2016

216 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

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 Readwhile ( !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

ax2 + bx + c = 0

Formula to compute one root

a

acbbbx

2

4*

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*

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)

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

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

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

15

Other C++ Functions

cin.get(char)cin.eof()

Function Prototypesvoid 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 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

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

top related