Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Miscellaneous Topics CS-2301 B-term 20081 More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The.

Similar presentations


Presentation on theme: "More Miscellaneous Topics CS-2301 B-term 20081 More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The."— Presentation transcript:

1 More Miscellaneous Topics CS-2301 B-term 20081 More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language, 2 nd ed., by Kernighan and Ritchie and from C: How to Program, 5 th ed., by Deitel and Deitel)

2 More Miscellaneous Topics CS-2301 B-term 20082 Recursive Functions – Exam Question 9 void move (int disks, int a, int c, int b){ if (disks > 0){ move (disks-1, a, b, c); printf ("Move one disk from %d to %d\n", a, c); move (disks-1, b, c, a); } return; } How many calls to move for disks = 0 ? How many disks are moved for disks = 0 ?

3 More Miscellaneous Topics CS-2301 B-term 20083 Recursive Functions – Exam Question 9 void move (int disks, int a, int c, int b){ if (disks > 0){ move (disks-1, a, b, c); printf ("Move one disk from %d to %d\n", a, c); move (disks-1, b, c, a); } return; } How many calls to move for disks = 0 ?1 How many disks are moved for disks = 0 ?0

4 More Miscellaneous Topics CS-2301 B-term 20084 Recursive Functions – Exam Question 9 void move (int disks, int a, int c, int b){ if (disks > 0){ move (disks-1, a, b, c); printf ("Move one disk from %d to %d\n", a, c); move (disks-1, b, c, a); } return; } How many calls to move for disks = 0 ?1 How many disks are moved for disks = 0 ?0 How many calls to move for disks = 1 ? How many disks are moved for disks = 1 ?

5 More Miscellaneous Topics CS-2301 B-term 20085 Recursive Functions – Exam Question 9 void move (int disks, int a, int c, int b){ if (disks > 0){ move (disks-1, a, b, c); printf ("Move one disk from %d to %d\n", a, c); move (disks-1, b, c, a); } return; } How many calls to move for disks = 0 ?1 How many disks are moved for disks = 0 ?0 How many calls to move for disks = 1 ?3 One for disks = 1 and 2 times disks = 0 – i.e., 1 + 2*1! How many disks are moved for disks = 1 ? 1 One for disks = 1 and 2 times disks = 0 – i.e., 1 + 2*0!

6 More Miscellaneous Topics CS-2301 B-term 20086 Recursive Functions – Exam Question 9 void move (int disks, int a, int c, int b){ if (disks > 0){ move (disks-1, a, b, c); printf ("Move one disk from %d to %d\n", a, c); move (disks-1, b, c, a); } return; } How many calls to move for disks = 1 ?3 How many disks are moved for disks = 1 ?1 How many calls to move for disks = 2 ?7 One for disks = 2 and 2 times disks = 1 – i.e., 1 + 2*3! How many disks are moved for disks = 2 ? 3 One for disks = 2 and 2 times disks = 1 – i.e., 1 + 2*1!

7 More Miscellaneous Topics CS-2301 B-term 20087 Recursive Functions – Exam Question 9 void move (int disks, int a, int c, int b){ if (disks > 0){ move (disks-1, a, b, c); printf ("Move one disk from %d to %d\n", a, c); move (disks-1, b, c, a); } return; } How many calls to move for disks = 2 ?7 How many disks are moved for disks = 2 ?3 How many calls to move for disks = 3 ?15 One for disks = 3 and 2 times disks = 2 – i.e., 1 + 2*7! How many disks are moved for disks = 3 ? 7 One for disks = 3 and 2 times disks = 2 – i.e., 1 + 2*3!

8 More Miscellaneous Topics CS-2301 B-term 20088 Recursive Functions – Exam Question 9 void move (int disks, int a, int c, int b){ if (disks > 0){ move (disks-1, a, b, c); printf ("Move one disk from %d to %d\n", a, c); move (disks-1, b, c, a); } return; } How many calls to move for disks = 3 ?15 How many disks are moved for disks = 3 ?7 How many calls to move for disks = 4 ?31 One for disks = 4 and 2 times disks = 3 – i.e., 1 + 2*15! How many disks are moved for disks = 4 ? 15 One for disks = 4 and 2 times disks = 3 – i.e., 1 + 2*7!

9 More Miscellaneous Topics CS-2301 B-term 20089 Recursive Functions – Exam Question 9 void move (int disks, int a, int c, int b){ if (disks > 0){ move (disks-1, a, b, c); printf ("Move one disk from %d to %d\n", a, c); move (disks-1, b, c, a); } return; } How many calls to move for disks = 3 ?15 How many disks are moved for disks = 3 ?7 How many calls to move for disks = 4 ?31 One for disks = 4 and 2 times disks = 3 – i.e., 1 + 2*15! How many disks are moved for disks = 4 ? 15 One for disks = 4 and 2 times disks = 3 – i.e., 1 + 2*7! Answer for disks = n is simple from the code and disks = n-1

10 More Miscellaneous Topics CS-2301 B-term 200810 Questions?

11 More Miscellaneous Topics CS-2301 B-term 200811 What does C do for you … … at compile time? … at run time?

12 More Miscellaneous Topics CS-2301 B-term 200812 At Compile Time Managing headers, include files, linking, preparation for debugging, etc. Type checking and conversion between numeric types To avoid silly mistakes Managing The Stack Code optimization! Escapes from type rules For people who know what they are doing

13 More Miscellaneous Topics CS-2301 B-term 200813 At Run Time Very little No array bounds checking No pointer checking No validity checking of any kind No protection against run-time mistakes No …… No …

14 More Miscellaneous Topics CS-2301 B-term 200814 C Provides Enough flexibility to program the innermost details of a operating system, device driver, instrument, embedded system A comprehensive library of supporting functions and Enough rope to hang yourself with!

15 More Miscellaneous Topics CS-2301 B-term 200815 Questions?


Download ppt "More Miscellaneous Topics CS-2301 B-term 20081 More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The."

Similar presentations


Ads by Google