Thread: Last Time with this Program ... I swear!

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    Anyone?

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by DonFord81 View Post
    Anyone?
    be patient

    1. to zero arrays at the beginning enough to write

    Code:
    	char matrix_array[DIM][BITNSLOTS(DIM)] ={0};
    	char U_array[BITNSLOTS(UPPER)] ={0};
    	int combination[LOWER]={0};
    No need to call memset

    2.
    Code:
    #define BITMASK(b) (1 << ((b) % CHAR_BIT))
    What bit do you want to set when b == 1?

    3. test_all_zero instead of doing what its name suggests - checks only the

    Code:
    if (BITTEST(bitarray, 0))
    		{
    			return 0;
    		}
    		else
    		{
    			return 1;
    		}
    I could not compile and debug your program because some function is missing.

    Do you pay attention to the compiler warnings?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    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.

    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);
             }
    }
    And here's the <combination.h> header file:
    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;
    }
    However, for DIM = 5 and LOWER = 3, I'm still getting an incorrect answer.

    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?

  4. #19
    Registered User
    Join Date
    Nov 2008
    Posts
    38
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program closing at end, no time to read...
    By Jake.c in forum C Programming
    Replies: 8
    Last Post: 09-08-2008, 02:20 PM
  2. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  3. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  4. Replies: 3
    Last Post: 03-21-2004, 06:02 PM
  5. worst time to program
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 06-10-2002, 11:32 PM