1 / 15

More Miscellaneous Topics

Learn how to use recursive functions in C programming to solve complex problems. This tutorial includes example code and explanations for beginners.

cbasham
Télécharger la présentation

More Miscellaneous Topics

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. 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

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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. Questions? More Miscellaneous Topics

  11. What does C do for you … • … at compile time? • … at run time? More Miscellaneous Topics

  12. 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

  13. 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

  14. 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

  15. Questions? More Miscellaneous Topics

More Related