Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS31: Introduction to Computer Science I Discussion 1A 4/23/2010 Sungwon Yang

Similar presentations


Presentation on theme: "CS31: Introduction to Computer Science I Discussion 1A 4/23/2010 Sungwon Yang"— 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 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. Where do we define functions?

4 Functions FAQ 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. I defined the function, why doesn’t it run?

5 Functions FAQ 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. Why does the return value not show up on the screen?

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 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; } What’s your name? Sungwon Sungwon!!! Nice to meet you! Sungwon copy value not affect

7 Passing arguments by reference passing in reference to a variable into functions – allow you to access/modify variables outside 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; } What’s your name? Sungwon Sungwon!!! Nice to meet you! Sungwon!!! reference to variable affected

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/Uno 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 += 3 should check first i+3 < dance.length() 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 3 0

15 more tips It would be useful to have two pointing variables – each beat ends with ‘/’ already checked with isDanceWellFormed() function – one is pointing the first character in one beat advance to the first letter of the next beat, if one beat is translated for ( size_t i = 0 ; i < dance.length() ; i++ ) advance to the next beat: i += 1, i += 2, i += 3, … – another is indicating the current beat increase the variable if one beat is translated translate each beat and append it to a string variable – if all beats are translated, copy them to the “instructions” variable – also, do not change the value of “badBeat” variable until any error found – the initial values of “instructions” and “badBeat” should not be changed if it is not translatable. 2u/u/2u//u/


Download ppt "CS31: Introduction to Computer Science I Discussion 1A 4/23/2010 Sungwon Yang"

Similar presentations


Ads by Google