6.11: Static Local Variables & 6.12: Default Arguments

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Write the definition of a function named alternator that receives no parameters and returns true the first time it is invoked, returns false the next time it is invoked, then true , false and so on, alternating between true /false on successive invocations.

bool alternator() { static int numCall = 0; numCall++; if( numCall%2 == 1 )return true; else return false; }

Write the definition of a function named newbie that receives no parameters and returns true the first time it is invoked (when it is a "newbie"), and that returns false every time that it is invoked after that.

bool newbie(void){ static bool x = true ; bool t=x; x=false; return t; }

Write the definition of a function named averager that receives a double parameter and returns-- as a double -- the average value that it has been passed so far. So, if you make these calls to average, averager(5.0), averager(15.0), averager(4,3), the values returned will be (respectively): 5.0, 10.0, 8.1.

double averager (const double &wrongNumber) { static double threeSum = 0.0; static size_t bodyCount = 0; bodyCount++; threeSum += wrongNumber; return threeSum / bodyCount; }

Write the definition of a function named timeOnHighway that receives three parameters , all of type double : mileEndingPoint, mileStartingPoint, and speed. The first two parameters indicate the mile markers on an interstate at which a vehicle goes to and starts at; the third parameter indicates the speed of the vehicle in miles per hour. The function returns the number of hours it takes a vehicle to go from the starting mile marker to the ending one. The function has a default value for the speed: 55 miles per hour, and a default value for mileStartingPoint: 0.0.

double timeOnHighway (double mileEndingPoint, double mileStartingPoint = 0.0, double speed = 55.0) { return (mileEndingPoint - mileStartingPoint) / speed; }

Write the definition of a function named counter that receives no parameters and returns 0 the first time it is invoked, returns 1 the next time it is invoked, then 2, 3 and so on.

int counter() { static int x = 0; return(x++); }

Write the definition of a function named max that receives an int parameter and returns the largest value that it has been called with so far. So, if you make these calls to max, max(5), max(3), max(12), max(4), the values returned will be (respectively): 5, 5, 12, and 12.

int max(int iInput) { static int iHighest=0; if (iInput>iHighest) iHighest=iInput; return iHighest; }


Kaugnay na mga set ng pag-aaral

Leadership Exam 2 practice questions

View Set

(March 2024) AI-900 Practice Questions

View Set

Chapter 14 - Speciation and Punctuated Equilibrium

View Set

Biological Psychology Chapter 5: Vision

View Set

Org Man Exam 2 Conflict and Negotiations Chapter 10

View Set

Chapter 17- employee and proprietors

View Set

American Indian Issues - Civil War, Regionalism, and Realism

View Set

7.7 arrays as function arguments

View Set