Here's my code. It's supposed to take an array of bits (size MAX, whatever I want MAX to be) and arrange them into a MAX-by-MAX two-dimensional array for printing to screen. I can't tell which of my functions (or the if/printf itself) is causing the current odd behavior.

Initially, I zero out the entire array, make the matrix based on the array, and then display both. Then, I invert the entire array (change all 0's to 1's - and theoretically vice versa, but I've only got 0's to start with), and make the matrix and display again. Only not all the entries in the new matrix are 1's. The example with MAX = 6 is shown below, after the code.

Here's the output of MAX = 6:Code:#include <stdio.h> #include <conio.h> #include <string.h> #include <limits.h> /* for CHAR_BIT */ #define MAX 6 #define BITMASK(b) (1 << ((b) % CHAR_BIT)) #define BITSLOT(b) ((b) / CHAR_BIT) #define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b)) #define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b)) #define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b)) #define BITNSLOTS(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT) void increment(char* bitarray, int n); int test_all_zero(char* bitarray, int n); void one_diag(char [][BITNSLOTS(MAX)], int n); void make_matrix(char [][BITNSLOTS(MAX)], char* U_array, int n); void invert(char* bitarray, int n); void display(char [][BITNSLOTS(MAX)], int n); main(){ char bitarray[MAX][BITNSLOTS(MAX)]; int i,j; for (i=0; i<MAX; i++){ memset(bitarray[i], 0, BITNSLOTS(MAX)); } char U_array[BITNSLOTS(MAX*(MAX-1)/2)]; memset(U_array, 0, BITNSLOTS(MAX*(MAX-1)/2)); /* TEST CODE START */ char test_array[BITNSLOTS(MAX*(MAX-1)/2)]; memset(test_array, 0, BITNSLOTS(MAX*(MAX-1)/2)); char test_matrix[10][BITNSLOTS(MAX)]; for (i=0; i<MAX; i++){ memset(test_matrix[i], 0, BITNSLOTS(MAX)); } printf("Starting:\n"); for (i=0; i<(MAX*(MAX-1)/2); i++){ if (BITTEST(test_array, i)){ printf("1"); } else{ printf("0"); } } printf("\n"); make_matrix(test_matrix, test_array, MAX); display(test_matrix, MAX); printf("Inverting:\n"); while (test_all_zero(test_array, MAX*(MAX-1)/2)){ invert(test_array, MAX*(MAX-1)/2); } for (i=0; i<(MAX*(MAX-1)/2); i++){ if (BITTEST(test_array, i)){ printf("1"); } else{ printf("0"); } } printf("\n"); make_matrix(test_matrix, test_array, MAX); one_diag(test_matrix, MAX); display(test_matrix, MAX); /* TEST CODE END */ getch(); } void increment(char* bitarray, int n){ int i = 0; while (i < n){ if (!BITTEST(bitarray, i)){ BITSET(bitarray, i); i = n; } else{ BITCLEAR(bitarray, i); i++; } } } int test_all_zero(char* bitarray, int n){ int i; int counter = 0; for (i=0; i<n; i++){ if (!BITTEST(bitarray, i)){ counter++; } } if (counter == n){ return 1; } else{ return 0; } } void one_diag(char bitarray[][BITNSLOTS(MAX)], int n){ int i,j; for (i=0; i<n; i++){ for (j=0; j<n; j++){ if (i == j){ BITSET(bitarray[i], j); } } } } void invert(char* bitarray, int n){ int i; for (i=0; i<n; i++){ if (BITTEST(bitarray, i)){ BITCLEAR(bitarray, i); } else{ BITSET(bitarray, i); } } } void make_matrix(char bitarray[][BITNSLOTS(MAX)], char* U_array, int n){ int i,j,k; for (i=0,j=i+1,k=0; k<((n*(n-1))/2); j++,k++){ if (j > n){ i++; j = i+1; } if (BITTEST(U_array, k)){ BITSET(bitarray[i], j); BITSET(bitarray[j], i); } else{ BITCLEAR(bitarray[i], j); BITCLEAR(bitarray[j], i); } } } void display(char bitarray[][BITNSLOTS(MAX)], int n){ int i, j; for (i=0; i<n; i++){ for (j=0; j<n; j++){ if (BITTEST(bitarray[i], j)){ printf("1"); } else{ printf("0"); } } printf("\n"); } printf("\n"); }

The second matrix should be all 1's. What's going on?Code:Starting: 000000000000000 000000 000000 000000 000000 000000 000000 Inverting: 111111111111111 111111 111111 111111 111100 111010 111001