1 / 14

CS31: Introduction to Computer Science I

CS31: Introduction to Computer Science I. Discussion 1A 4/23/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang. Quick Review. What did we learn last week? while loop Repeat statements while condition holds do-while loop Execute the statements once then test condition

emma
Download Presentation

CS31: Introduction to Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS31: Introduction to Computer Science I Discussion 1A 4/23/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang

  2. Quick Review • What did we learn last week? • while loop • Repeat statements while condition holds • do-while loop • Execute the statements once then test condition • for loop • for-while conversion • initialization, condition, update • infinite loops • Make sure that it ends at some point • nested loops • goes back to the outer loop after complete inner loop • functions • function definition (proto type) • return type (void function) • arguments ( functions can have no arguments) • functions of string and character variables

  3. Functions FAQ Where do we define functions? There are two conventional ways, which are equivalent. The requirement is that the function must be defined before it can be used, just like variables. So you either completely define it before the function is used, or add the prototype and define it later in the program. The prototype is a way of telling your compiler that there is such a function, but that we will define it later. Remember to add a semicolon after the prototype, but not after the function header.

  4. Functions FAQ I defined the function, why doesn’t it run? Defining a function does not imply using it. You must explicitly call (or invoke) the function somewhere to see it running. When you call it, it will be run as you defined it. Where you call it and how you call it depend on you.

  5. Functions FAQ Why does the return value not show up on the screen? Because you did not display it, and it’s not meant to be displayed. There are people who confusing “returning” with “outputting,” which is different. When you return a value from a function, you return it to whoever called the function.

  6. Passing arguments by value • passing in values into functions • not allow you to access/modify variables outside • values of arguments exist only in functions What’s your name? void greeting (string name ) { name += "!!!"; cout << name << endl; cout << "Nice to meet you!" << endl; } int main() { string name; cout << "What’s your name?" << endl; getline (cin, name); greeting (name); cout << name << endl; } not affect Sungwon Sungwon!!! Nice to meet you! Sungwon copy value

  7. Passing arguments by reference • passing in reference to a variable into functions • allow you to access/modify variables outside What’s your name? void greeting (string& name ) { name += "!!!"; cout << name << endl; cout << "Nice to meet you!" << endl; } int main() { string name; cout << "What’s your name?" << endl; getline (cin, name); greeting (name); cout << name << endl; } affected Sungwon Sungwon!!! Nice to meet you! Sungwon!!! reference to variable

  8. a tip for Visual Studio • find the pair of {}, () • ctrl + ] before/after one of {} or () pair • if ( ) • { • ……… • if ( ) • { • if ( ) • { • ……… • ……… • if ( ) • { • ……. • ……. • } • ……. • ……. • } • ……… • ……… • } • } • if ( ) • { • ……… • if ( ) • { • if ( ) • { • ……… • ……… • if ( ) • { • ……. • ……. • } • ……. • ……. • } • ……… • ……… • } • } ctrl + ]

  9. Project #3 • bool isDanceWellFormed (string dance) • passing artist’s representation of dance into the function • the function check s the dance is well-formed • a slash (/) • a direction (i, L, u, U, r, R, d, D) followed by a slash • a digit followed by a direction followed by a slash • two digits followed by a direction followed by a slash • returns true if it is well-formed, otherwise return false • zero beats • D/ • u//D/3r///d/ • 03u///10r////////// • /// three beats • d/99d/88d/77d/6d/5d/ • 456r/ 3-digit • D/D/U no slash

  10. Project #3 • int translateDance (string dance, string& instructions, int& badBeat) • passing artist’s representation of dance, reference to instructions, and reference to badBeats into the function • the function checks the dance is transtlatable • The dance is well-formed. • For all beats for which a freeze is in effect, except the first, the beat consists only of a slash. (Thus, d/3r/// is translatable, but d/3r//u/ is not) • All beats that a freeze is in effect must be in the dance; in other words, the dance string can't end prematurely. (Thus, d/3r/// is translatable, but d/3r// is not.) • The length of a freeze cannot be 0 or 1. (We could have decided differently, but we're decreeing this. Thus, 03r/// is translatable, but 0r/ and 1r/ are not.) • the function translates the dance into instructions if it is translatable • ///  … • u/d/R/L/  udrl • 3u///02D//  UUUDD

  11. Pseudo code of isDanceWellFormed() if the dance is empty string, return true otherwise, check each character of the dance: for ( size_t i=0 ; i < dance.length() ; i++ ) if the character is ‘/’, it is well-formed. Do nothing if ( dance.at(i) == ‘/’ ) else if the character is a direction if it is followed by ‘/’: if (i+1 < dance.length() && dance.at(i+1) == '/'), advance to the next beat: i += 1, otherwise, return false else if the character is a digit if it is followed by a direction: should check first i+1 < dance.length() if it is followed by ‘/’, advance to the next beat: i += 2 should check first i+2 < dance.length() otherwise, return false else if it is followed by another digit, otherwise, return false should check first i+1 < dance.length() if it is followed by a direction, otherwise, return false should check first i+2 < dance.length() if it is followed by ‘/’, advance to the next beat: i += 3should check first i+3 < dance.length() otherwise, return false otherwise, return false if nothing wrong found during for loop, return true /u/2d//02d//

  12. Convert letter to integer • can define a function • int convert(char number) • { • switch (number) • { • case '0': • return 0; • case '1': • return 1; • case '2': • return 2; • case '3': • return 3; • case '4': • return 4; • case '5': • return 5; • case '6': • return 6; • case '7': • return 7; • case '8': • return 8; • case '9': • return 9; • } • }

  13. Convert 1 or 2-digit string to integer • int main() • { • string str = "12u/"; • int num; • for (size_t i=0; i<str.length(); i++) • { • if (isdigit(str.at(i))) • { • if (isdigit(str.at(i+1))) • { • num = 10*convert(str.at(i)) + convert(str.at(i+1)); • i+=1; • } • else • { • num = convert(str.at(i)); • } • } • } • cout << num << endl; • }

  14. Convert string to integer • atoi() function int main() { string str = "12u//3d/"; cout << atoi (&str.at(0)) << endl; cout << atoi (&str.at(1)) << endl; cout << atoi (&str.at(2)) << endl; cout << atoi (&str.at(3)) << endl; cout << atoi (&str.at(4)) << endl; cout << atoi (&str.at(5)) << endl; cout << atoi (&str.at(6)) << endl; } 12 2 0 0 0 3 0

More Related