150 likes | 166 Views
Learn how to use recursive functions in C programming to solve complex problems. This tutorial includes example code and explanations for beginners.
E N D
More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language, 2nd ed., by Kernighan and Ritchie and from C: How to Program, 5th ed., by Deitel and Deitel) More Miscellaneous Topics
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? More Miscellaneous Topics
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 More Miscellaneous Topics
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? More Miscellaneous Topics
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! More Miscellaneous Topics
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! More Miscellaneous Topics
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! More Miscellaneous Topics
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! More Miscellaneous Topics
Answer for disks = n is simple from the code and disks = n-1 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! More Miscellaneous Topics
Questions? More Miscellaneous Topics
What does C do for you … • … at compile time? • … at run time? More Miscellaneous Topics
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 More Miscellaneous Topics
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 … More Miscellaneous Topics
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! More Miscellaneous Topics
Questions? More Miscellaneous Topics