Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CS 1430: Programming in C++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled."— Presentation transcript:

1 1 CS 1430: Programming in C++

2 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

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

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

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

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

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

17 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 18 Schedule Quiz3-4 Due 10 PM today Prog2 Test1 Quiz1 Now


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

Similar presentations


Ads by Google