BVB Source Codes

Algorithm-Implementations Show 100-Doors.c Source code

Return Download Algorithm-Implementations: download 100-Doors.c Source code - Download Algorithm-Implementations Source code - Type:.c
  1. /*
  2. Kendrick Ledet 2014
  3.  
  4. There are 100 doors in a long hallway. They are all closed. The first time you walk by each door, you open it.
  5. The second time around, you close every second door (since they are all opened).
  6. On the third pass you stop at every third door and open it if it鈥檚 closed, close it if it鈥檚 open.
  7. On the fourth pass, you take action on every fourth door. You repeat this pattern for 100 passes.
  8. */
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <stdbool.h>
  12.  
  13. void pass_doors(int[]);
  14. bool test_doors(int[]);
  15. void print_doors(int[]);
  16.  
  17. int main(int argc, char const *argv[])
  18. {
  19.     // Allocate array with 100 elements initialized to 0 to represent all doors being closed.
  20.     int doors[100];
  21.     for (int i = 0; i <= 99; ++i)
  22.         doors[i] = 0;
  23.  
  24.     // Run algorithm
  25.     pass_doors(doors);
  26.  
  27.     // Print out final door states
  28.     print_doors(doors);
  29.  
  30.     // Test that all perfect square # doors are open
  31.     printf("Test: %s\n", test_doors(doors) ? "Pass" : "Fail");
  32.  
  33.     return 0;
  34. }
  35.  
  36. void pass_doors(int doors[])
  37. {
  38.     // Track the current pass number
  39.     for (int pass_num = 1; pass_num <= 99; ++pass_num) {
  40.  
  41.         // Offset into hallway, by current pass number
  42.         for (int i = pass_num-1; i <= 99; i += pass_num)
  43.             doors[i] = doors[i] == 1 ? 0 : 1;  // close if open, open if close
  44.     }
  45. }
  46.  
  47. void print_doors(int doors[])
  48. {
  49.     /* Print out door statuses: "Open" if 1, "Closed" if 0 */
  50.     for (int i = 0; i <= 99; ++i)
  51.         printf("Door #%d: %s\n", i+1, doors[i] == 1 ? "Open" : "Closed");
  52. }
  53.  
  54. bool test_doors(int doors[])
  55. {
  56.     for (int i = 0; i <= 99; ++i) {
  57.         if ( doors[i] && (int) sqrt(doors[i]) % 1 != 0 )  // if door is open and door # is not a perfect square
  58.             return false;
  59.     }
  60.     return true;
  61. }
download100-Doors.c Source code - Download Algorithm-Implementations Source code
Related Source Codes/Software:
raty - 2017-04-22
RDVTabBarController - Highly customizable tabBar and tabBarController fo... 2017-04-22
material-icon-lib - Library containing over 1500 material vector icons... 2017-04-21
httpdiff - Perform the same request against two HTTP servers ... 2017-04-21
jquerytools - The missing UI library for the Web ... 2017-04-21
mcrouter - Mcrouter is a memcached protocol router for scalin... 2017-04-22
dynomite - A generic dynamo implementation for different k-v ... 2017-04-22
kityminder - Baidu brain figure 2017-04-22
llvm - Mirror of official llvm git repository located at ... 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
ied - 2017-04-29
Nimble - A Matcher Framework for Swift and Objective-C 2017-04-29
MHVideoPhotoGallery - A Photo and Video Gallery 2017-04-29
shoulda-matchers - Collection of testing matchers extracted from Shou... 2017-04-29
Android-SlideExpandableListView - A better ExpandableListView, with animated expanda... 2017-04-29
AppSales-Mobile - App Sales allows iPhone and Mac App Store develope... 2017-04-29
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28

 Back to top