Anyone?
Printable View
Anyone?
be patient
1. to zero arrays at the beginning enough to write
No need to call memsetCode:char matrix_array[DIM][BITNSLOTS(DIM)] ={0};
char U_array[BITNSLOTS(UPPER)] ={0};
int combination[LOWER]={0};
2.
What bit do you want to set when b == 1?Code:#define BITMASK(b) (1 << ((b) % CHAR_BIT))
3. test_all_zero instead of doing what its name suggests - checks only the
I could not compile and debug your program because some function is missing.Code:if (BITTEST(bitarray, 0))
{
return 0;
}
else
{
return 1;
}
Do you pay attention to the compiler warnings?
The function that's probably missing for you is <combination.h>, which is not standard in the C library. I can include the code snippet I used for it, since I found it on an internet forum for such things.
I changed the test_all_zero code per your suggestion.
And here's the <combination.h> header file:Code: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);
}
}
However, for DIM = 5 and LOWER = 3, I'm still getting an incorrect answer.Code:/*
next_comb(int comb[], int k, int n)
Generates the next combination of n elements as k after comb
comb => the previous combination ( use (0, 1, 2, ..., k) for first)
k => the size of the subsets to generate
n => the size of the original set
Returns: 1 if a valid combination was found
0, otherwise
*/
int next_comb(int comb[], int k, int n) {
int i = k - 1;
++comb[i];
while ((i >= 0) && (comb[i] >= n - k + 1 + i)) {
--i;
++comb[i];
}
if (comb[0] > n - k) /* Combination (n-k, n-k+1, ..., n) reached */
return 0; /* No more combinations can be generated */
/* comb now looks like (..., x, n, n, n, ..., n).
Turn it into (..., x, x + 1, x + 2, ...) */
for (i = i + 1; i < k; ++i)
comb[i] = comb[i - 1] + 1;
return 1;
}
I can't figure out if my for/while loops are even engaging, since it seems that my program block is just getting skipped. Is that what's happening?
I still don't know what the program is doing wrong. I thought it might be something along the lines of having forgotten to run "make_matrix" after every instance of changing U_array, but I've checked the code, and I do run "make_matrix" after every change in U_array, so that's not the problem.
Can anyone please help me?