if (s.size() !=0)//s: HelloS: ABCS:321s[o] = toupper(s[o]);//S is now Hello S is now ABC S is now 321if (s[k] == ‘E’ || s[k] == ‘e’)if (tolower(s[k]) == ‘e’)s = toupper(s); //Error! wont compile. s is a string, not a chartoupper(s[0]); //compiles, but is uselessbool isValidPhoneNumber(string pn);string digitsOf(string s);int main(){ cout << “Enter a phone number: ”;string phone;getline( cin , phone);if (isValidPhoneNumber(phone))cout << “The digits in the number are “ << digitsOf(phone)<< endl;elsecout << “A phone number must have 10 digits.” << endl;bool isValidPhoneNumber(string pn){int numberOf Digits= 0;for (int k = 0; k!= pn.size(); k++){ if (isdigit(pn[k])numberOfDigits ++;}if (numberOfDigits == 10)return true;elsereturn false;return numberOfDigits == 10;string digitsOf(string s);{string digitsOnly = “”;for( int k = 0; k != s.size(); k++){ if( isdigit(s[k]))digitsOnly += s[k]; //append s[k] to the string}return digitsOnly;A function that tests a property of sth and returns a bool is often called a “predicate”.isdigit(s[k])isValidPhoneNumber(phone)hasMoreThanTwoPrimeFactors(n)livesIn(personName, city)makesMoreThan(employee1, employee2)double& “reference to double”A reference is just another name for an already existing object.#include mathvoid polarToCartesian(double rho, double theta, double& xx, double& yy);int main(){double r;double angle;----------------get values for r and angle;double x;double y;polarToCartesian(r, angle, x, y);….double x2;polarToCartedin(2*r, angle, x2, y);…..}void polarToCartesian(double rho, double thetha, double& xx, double& yy){xx = rho*cos(thetha)yy=rho*sin(thetha)}“passing by value” copy the arguments into new variables for the parameters“passing by reference” no copy is made; we’re just using another name for an already existing
View Full Document