CS 1430 Programming in C 1 C Loops

  • Slides: 17
Download presentation
CS 1430: Programming in C++ 1

CS 1430: Programming in C++ 1

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

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

Sentinel Controlled Loops cin >> a. Value; // Prime read! while (a. Value !=

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

Count Controlled Loops cin >> the. Count; loop. Count = 0; while (loop. Count

Count Controlled Loops cin >> the. Count; loop. Count = 0; while (loop. Count < the. Count) { // Input inside the loop cin >> a. Value; // Do Work loop. Count ++; } 4

EOF Controlled Loops cin >> a. Value; // Prime Read while ( !cin. eof()

EOF Controlled Loops cin >> a. Value; // Prime Read while ( !cin. eof() ) { // Do Work cin >> a. Value; } Prog 2! 5

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

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

C++ Code to Compute a Root C++ Function sqrt() cin >> coef. A >>

C++ Code to Compute a Root C++ Function sqrt() cin >> coef. A >> coef. B >> coef. C; delta = coef. B * coef. B - 4 * coef. A * coef. C; root 1 = (-coef. B + sqrt(delta)) / (2 * coef. A); 8

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

C++ Function sqrt() // Header file #include <cmath> 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

C++ Function Prototype // Function Prototype (Declaration) float sqrt(float x); // Could be double

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 10

Function Calls // Function Prototype float sqrt(float x); float sqrt(float); // Function Calls root

Function Calls // Function Prototype float sqrt(float x); float sqrt(float); // Function Calls root 1 = (-coef. B + sqrt(delta)) / (2 * coef. A); // Parameter is variable delta cout << “The square root of 10 is “ << sqrt(10); // Parameter is literal value 10 Value = sqrt (coef. B * coef. B - 4 * coef. A * coef. C); // Parameter is an expression 11

Return Value root 1 = (-coef. B + sqrt(delta)) / (2 * coef. A);

Return Value root 1 = (-coef. B + sqrt(delta)) / (2 * coef. A); // Used in computation cout << “The square root of 10 is “ << sqrt(10); // Inserted into output stream // Displayed on screen the. Value = sqrt (coef. B * coef. B - 4 * coef. A * coef. C); // Stored in variable Value sqrt(delta); // NO! // Good C++ statement? MUST use the return value! 12

Function Parameters float sqrt(float x); // Function Prototype // x: Parameter // Formal parameter

Function Parameters float sqrt(float x); // Function Prototype // x: Parameter // Formal parameter // In function call: Actual parameter Can be literal value, variable, or expression root 1 = (-coef. B + sqrt(delta)) / (2 * coef. A); // Actual parameter: // delta cout << “The square root of 10 is “ << sqrt(10); // Actual parameter: // 10 Value = sqrt (coef. B * coef. B - 4 * coef. A * coef. C); // Actual parameter: // coef. B * coef. B - 4 * coef. A * coef. C Actual parameters can be different. 13

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

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

Other C++ Math Functions Header File <cmath> int ceil(double x); int floor(double x); double

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

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

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

Summary • Function Prototype float sqrt(float x); int the. Function(char type, float value); •

Summary • Function Prototype float sqrt(float x); int the. Function(char type, float value); • Function Definition float sqrt(float x) { // compute and return the square root } • Function Call root 1 = (-coef. B + sqrt(delta)) / (2 * coef. A); int. Val = the. Function(char. Val, f. Value); • Function Parameters (Arguments) Formal Parameters Actual Parameters 17

Schedule Quiz 3 -4 Due 10 PM today Prog 2 Test 1 Quiz 1

Schedule Quiz 3 -4 Due 10 PM today Prog 2 Test 1 Quiz 1 Now 18