Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled."— Presentation transcript:

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

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

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

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

5 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 6 Quadratic Equations ax 2 + bx + c = 0 Formula to compute one root

7 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 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 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 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 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 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 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 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 15 Other C++ Functions cin.get(char) cin.eof() Function Prototypes void get(char& c); bool eof();

16 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 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 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 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 20 Schedule Quiz3-4 Due 5 PM today QuizProg2 Now


Download ppt "1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled."

Similar presentations


Ads by Google