Section%20306%20-%20Team%201

# Section%20306%20-%20Team%201 - MLC Lecture Competition...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: MLC Lecture Competition Section S ti 306 Team #1 Wrong! Engr 101 Please Choose a Category: Pl Ch C t Lecture 13: Vectors and Vectors of Vectors Lecture 15: Static variables/scope/lifetime Lecture 14: Sorting and Recursion Review What would this function return f nction ret rn if you o input the following: kebert.xela You have 2 minutes void print_backwards() { char character; cout << "Enter a character "; cin >> character; if (character != '.') { print_backwards(); i t b k d () cout << character; } else { return ; } } int main() { print_backwards(); cout << endl; return 0; } What would this function return f nction ret rn if you o input the following: kebert.xela Guesses? void print_backwards() { char character; cout << "Enter a character "; cin >> character; if (character != '.') { print_backwards(); cout << character; } else l { return ; } } int main() { print_backwards(); cout << endl; return 0; } What would this function return f nction ret rn if you o input the following: kebert.xela void print_backwards() { char character; cout << "Enter a character "; cin >> character; if (character != '.') { print_backwards(); cout << character; } else { return ; } } int main() { print_backwards(); cout << endl; return 0; } Answer: trebek t b k How d H does thi this work? void print_backwards() { char character; cout << "Enter a character "; cin >> character; if (character != '.') { print_backwards(); cout << character; } else { return ; } } int main() { print_backwards(); cout << endl; return 0; } When kebert.xela kebert ela Is inputted Each h E h character t gets inputted here one at a time kebert.xela When program reaches the `.' void print_backwards() { char character; cout << "Enter a character "; cin >> character; if (character != '.') { print_backwards(); cout << character; } else { return ; } } int main() { print_backwards(); cout << endl; return 0; } This if statement is no longer valid Therefore it returns with `trebek' return What has to happen pp in order for s to have a lifetime? int main() { int t, d; ifstream infile; infile.open("test"); p ( ) while(infile.fail()) { int s=3; ; cout<<s; } for(t=3;t>=0;t--) ( ; ; ) { infile>>d; cout d; cout<<d; } cout<<"End of file"; return 0; } You have 2 minutes What has to happen pp in order for s to have a lifetime? Guesses ? int main() { int t, d; ifstream infile; infile.open("test"); p ( ) while(infile.fail()) { int s=3; ; cout<<s; } for(t=3;t>=0;t--) ( ; ; ) { infile>>d; cout d; cout<<d; } cout<<"End of file"; return 0; } What has to happen pp in order for s to have a lifetime? Answer: Only if the INFILE fails f il int main() { int t, d; ifstream infile; infile.open("test"); p ( ) while(infile.fail()) { int s=3; ; cout<<s; } for(t=3;t>=0;t--) ( ; ; ) { infile>>d; cout d; cout<<d; } cout<<"End of file"; return 0; } What has to happen pp in order for s to have a lifetime? How does it work? k? int main() { int t, d; ifstream infile; infile.open("test"); p ( ) while(infile.fail()) { int s=3; ; cout<<s; } for(t=3;t>=0;t--) ( ; ; ) { infile>>d; cout d; cout<<d; } cout<<"End of file"; return 0; } The lifetime is the span of time during which a particular data object can be b accessed. d So when this happens All of this will happen int main() { int t, d; i tt d ifstream infile; infile.open("test"); ( ()) while(infile.fail()) { int s=3; cout<<s; } for(t=3;t>=0;t--) { infile>>d; cout<<d; } cout<<"End of file"; return 0; } Return int main() { vector <int> list; int s, t, c, n; for(c = 0; c < 4; c++) { cin >> n; list.push_back(n); } for(s = list.size()-1; s > 0; s = s1) for(t = 0; t< s; t = t + 1) ( ; ; ) if (list.at(s) > list.at(t)) swap(list.at(s), list.at(t)); for(c = 0; c < 4; c++) cout << list.at(c); return 0; } What will the program output if you input: 3, 1 4, 3 1, 4 1 You have 3 minutes int main() { vector <int> list; int s, t, c, n; for(c = 0; c < 4; c++) { cin >> n; list.push_back(n); } for(s = list.size()-1; s > 0; s = s-1) for(t = 0; t< s; t = t + 1) if (list.at(s) > list.at(t)) swap(list.at(s), list.at(t)); for(c = 0; c < 4; c++) cout << list.at(c); return 0; } What will the program output if you input: 3, 1 4, 3 1, 4 1 Guesses ? int main() { vector <int> list; int s, t, c, n; for(c = 0; c < 4; c++) { cin >> n; list.push_back(n); } for(s = list.size()-1; s > 0; s = s-1) for(t = 0; t< s; t = t + 1) if (list.at(s) > list.at(t)) swap(list.at(s), list.at(t)); for(c = 0; c < 4; c++) cout << list.at(c); return 0; } What will the program output if you input: 3, 1 4, 3 1, 4 1 Answer: 4, 3, 1, 1 int main() { vector <int> list; int s, t, c, n; for(c = 0; c < 4; c++) { cin >> n; list.push_back(n); } for(s = list.size()-1; s > 0; s = s-1) for(t = 0; t< s; t = t + 1) if (list.at(s) > list.at(t)) swap(list.at(s), list.at(t)); for(c = 0; c < 4; c++) cout << list.at(c); return 0; } What will the program output if you input: 3, 1 4, 3 1, 4 1 How does it work? int main() { vector <int> l list; int s, t, c, n; for(c = 0; c < 4; c++) { cin >> n; list.push_back(n); } for(s = list.size()-1; s > 0; s = s-1) for(t = 0; t< s; t = t + 1) if (list.at(s) > list.at(t)) swap(list.at(s), list.at(t)); for(c = 0; c < 4; c++) ( ; ; ) cout << list.at(c); return 0; ; } Return Important things to remember Strings g include the string class #include <string> , pp Note, that upper and lowercase letters are considered different. Uppercase letters come before lowercase letters alphabetically. Use the at( ) method to check to see that you don't go beyond the length of the string. To overcome recopying the string pass as a constant reference rather than by value: Important things to remember Vectors and vectors of vectors: cin ignores whitespace and uses it to separate input getline() reads in an entire line of text, including li () d i i li f i l di spaces use the safer .at( ) method to to index the list at( Be sure to include: #include <vector> Creating a vector of a vector is the easiest way of assigning the data to the elements Important things to remember Sorting and Recursion: g A program that calls itself is called recursive. ( find( ) locates a number in a sorted list. Important things to remember Static variables/ scope/ lifetime: If you want a variable to never change in the context of the code you can define it to be a const. Note that const does not replace the type, rather it p yp , modifies the data type. Note: References cannot be changed after they are created. created You cannot declare two identifiers with the same name on the same compound statement. When the lifetime expires the data object returned to memory for general use. g p it is a bad idea to make identifiers have global scope for non--constant identifiers, since they can change in a procedure or function without explicitly being passed in. ...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern