1 / 15

Quiz 2 Review

This quiz review covers topics from chapters 4 and 5, including structures, enumeration, pass by value and reference in functions, overloaded functions, and recursion.

lderr
Download Presentation

Quiz 2 Review

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. Quiz 2 Review

  2. Topics To Study • Chapters 4 and 5 • Structures • Enumeration • Functions: Pass by Value Pass by Reference Overloaded Functions Recursion

  3. Structures struct cppclass { int students; int room; }; int main() { cppclass first = {21, 1105}; cppclass second = {14, 1165}; first = second; cout << first.students << endl; cout << first.room << endl; return 0; }

  4. Program Output 14 1165

  5. Enumeration enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; int main() { days_of_week day1, day2; day1 = Mon; day2 = Thu; int diff = day2 - day1; cout << "Days between = " << diff << endl; if(day1 < day2) cout << "day1 comes before day2\n"; return 0; }

  6. Program Output Days between = 3 day1 comes before day2

  7. Functions Pass By Value #include <iostream> using namespace std; int quiz(int number) { number = 100; return number; } int main() { int variable = 0; cout << variable << endl; cout << quiz(variable) << endl; return 0; }

  8. Program Output 0 100

  9. Functions Pass By Reference #include <iostream> using namespace std; void quiz(int& number) { number = 0; } int main() { int variable = 100; quiz(variable); cout << variable << endl; return 0; }

  10. Program Output 0

  11. Function Overloading #include <iostream> using namespace std; void printnum(); void printnum(int); void printnum(int, int); int main() { int var = 15; printnum(); printnum(var); printnum(var, 10); return 0; }

  12. Function Overloading void printnum() { int num = 10; cout << num << endl; } void printnum(int num) { cout << num << endl; } void printnum(int num, int sub) { int ans = num - sub; cout << num << endl; }

  13. Program Output 10 15 15

  14. Functions - Recursion int getcount(int); int main() { int count; count = getcount(5); cout << count << endl; return 0; } int getcount(int cnt) { if (cnt != 1) cnt += getcount(cnt-1); else cnt = 1; return cnt; }

  15. Program Output 15

More Related