Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- void init_counter(int size, int b_counter[]){
- for(int i = 0; i < size; i++){
- b_counter[i] = 0;
- }
- }
- void increment_counter(int size, int b_counter[]){
- int position = size - 1;
- while(position >= 0 && b_counter[position] != 0){
- b_counter[position] = 0;
- position--;
- }
- if(position >= 0) b_counter[position] = 1;
- }
- void increment_counter_math(int size, int b_counter[]){
- int position = size - 1;
- int to_add = 1;
- int temp;
- while(position >= 0 && to_add == 1){
- temp = (b_counter[position] + to_add) % 2;
- to_add = (b_counter[position] + to_add) / 2;
- b_counter[position] = temp;
- position--;
- }
- }
- void print_counter(int size, int b_counter[]){
- for(int i = 0; i < size; i++){
- printf("%d ", b_counter[i]);
- }
- printf("\n");
- }
- int main()
- {
- int b_counter[3];
- init_counter(3, b_counter);
- /* test b_counter. */
- for(int i = 0; i < pow(2, 3); i++){
- print_counter(3, b_counter);
- increment_counter(3, b_counter);
- }
- /* space out the test results. */
- printf("\n\n\n");
- init_counter(3, b_counter);
- for(int i = 0; i < pow(2, 3); i++){
- print_counter(3, b_counter);
- increment_counter_math(3, b_counter);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement