DOC PREVIEW
UCLA COMSCI 31 - CS Lecture 6

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

UCLA COMSCI 31 - CS Lecture 6

Download CS Lecture 6
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS Lecture 6 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS Lecture 6 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?