Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void init_counter(int size, int b_counter[]){
  6.  
  7. for(int i = 0; i < size; i++){
  8. b_counter[i] = 0;
  9. }
  10.  
  11. }
  12.  
  13. void increment_counter(int size, int b_counter[]){
  14.  
  15. int position = size - 1;
  16.  
  17. while(position >= 0 && b_counter[position] != 0){
  18. b_counter[position] = 0;
  19. position--;
  20. }
  21.  
  22. if(position >= 0) b_counter[position] = 1;
  23. }
  24.  
  25. void increment_counter_math(int size, int b_counter[]){
  26.  
  27. int position = size - 1;
  28. int to_add = 1;
  29. int temp;
  30.  
  31. while(position >= 0 && to_add == 1){
  32.  
  33. temp = (b_counter[position] + to_add) % 2;
  34. to_add = (b_counter[position] + to_add) / 2;
  35.  
  36. b_counter[position] = temp;
  37. position--;
  38. }
  39.  
  40. }
  41.  
  42.  
  43. void print_counter(int size, int b_counter[]){
  44.  
  45. for(int i = 0; i < size; i++){
  46. printf("%d ", b_counter[i]);
  47. }
  48.  
  49. printf("\n");
  50. }
  51.  
  52. int main()
  53. {
  54.  
  55. int b_counter[3];
  56.  
  57. init_counter(3, b_counter);
  58.  
  59. /* test b_counter. */
  60.  
  61. for(int i = 0; i < pow(2, 3); i++){
  62. print_counter(3, b_counter);
  63. increment_counter(3, b_counter);
  64. }
  65.  
  66. /* space out the test results. */
  67. printf("\n\n\n");
  68.  
  69. init_counter(3, b_counter);
  70.  
  71. for(int i = 0; i < pow(2, 3); i++){
  72. print_counter(3, b_counter);
  73. increment_counter_math(3, b_counter);
  74. }
  75.  
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement