Thread: Counting frequency of strings in 2D array in C

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    38

    Counting frequency of strings in 2D array in C

    I am somewhat stuck on a piece of code that I'm hoping I can get some insight on here. Essentially I am reading an input file, grabbing the bit values and storing them. in a 2d array.What I'm trying to accomplish is finding the number of occurrences of each string in the 2d array.For example, Here is what is being stored in the 1st couple of entries in the 2d array sortBuffer[][]
    Code:
    111000101
    110000101
    111110000
    101011000
    000000010
    101001000
    So what I need to accomplish, is creating a count array that will tell me the number of times, say 111000101, or 110000101 occurs within the sortBuffer. Since it's a 2D array I'm working with, I'm not really sure how to compare the entire string. Any help with this would be greatly appreciated. Thank you all in advance.As an additional step, I need to organize the array by bit values, starting with
    000000001
    000000010
    000000011
    000000100
    and so on...
    Code:
     int getBitVal(unsignedchar*keyStrBin,int keyIndex){
        int keyMod = keyIndex %8;  
        int keyIn = keyIndex /8;
    
        return(((keyStrBin[keyIn])>>(7-(keyMod)))&1);
    }
    
    void test(FILE*inputFile){
    int nRead;
        size_t fSize = size(inputFile);
        unsignedchar*inBuffer = malloc(fSize *sizeof(unsignedchar));
        memset(inBuffer,0, fSize);
    
        nRead = fread(inBuffer,1, fSize, inputFile);
    
            int h =0;
        int b =0;
            int m =9
            int n =24720
        char sortBuffer[m][n / m];
    
        for(i =0; i < fSize *8; i++){
            sortBuffer[b][h]= getBitVal(inBuffer, i);
            b++;
            if(i % m ==0&& i !=0){
                b =0;
                h++;
            }
     }
    
    //This is where I need to count the number of times any given string occurs within the sortBuffer char.
    Last edited by TyrantUT; 05-08-2016 at 04:29 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Is there a reason you aren't converting the binary numbers in the file from strings to integers? I imagine it would make various things simpler. My example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int less_than(const void *a, const void *b)
    {
        const int *v1 = a;
        const int *v2 = b;
     
        return (*v1 < *v2) ? -1 : (*v1 > *v2) ? +1 : 0;
    }
     
    int main(void) {
        char buffer[256];
        int array[6];
        size_t i;
     
        while (i < 6 && fgets(buffer, sizeof buffer, stdin) != NULL) {
            array[i] = strtol(buffer, NULL, 2);
            ++i;
        }
     
        qsort(array, sizeof array / sizeof array[0], sizeof array[0], less_than);
     
        for (i = 0; i < sizeof array / sizeof array[0]; ++i) {
            printf("array[%d] = %d\n", i, array[i]);
        }
     
        return 0;
    }
    Last edited by whiteflags; 05-08-2016 at 06:53 PM.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    38
    This is what I did.
    Code:
    int h = 0;
    	int b = 0;
    	char sortBuffer[m][n / m];
    	char **bitPtr = malloc(fSize * 8);
    	for (i = 0; i < fSize * 8; i ++) {
    		bitPtr[i] = malloc(9);
    		memset(bitPtr[i], 0, 9);
    	}
    	printf("\n");
    
    
    	// Read bit values into sortBuffer 2D array
    	for (i = 0; i < fSize * 8; i++) {
    		sortBuffer[b][h] = getBitVal(inBuffer, i);
    		b++;
    		if (i % m == 0 && i != 0) {
    			b = 0;
    			h++;
    		}
    
    
    	}
    
    
    	for (h = 0; h < k; h++) {
    		for (i = 0; i < m; i++) {	
    			memcpy(bitPtr[h] + i, &sortBuffer[i][h], 1);
    
    
    			//fprintf(stdout, "%i", sortBuffer[i][h]);
    
    
    		}
    			
    	}
    So now each binary representation is saved in bitPtr[h] I can print that with a small function

    Code:
    void printVal(char *bitPtr) {
    	int i;
    	for (i = 0; i < 9; i++) {
    		fprintf(stdout, "%i", bitPtr[i]);
    	}
    }
    From there I can convert that to a decimal.

    Code:
    void convertToDec(char *bitPtr) {
    	int decimal_val = 0;
    	int base = 1;
    	int rem;
    	int num = *(int *) bitPtr;
    
    
    	while (num > 0) {
    		rem = num % 10;
    		decimal_val = decimal_val + rem * base;
    		num = num / 10;
    		base = base * 2;
    
    
    	}
    
    
    	fprintf(stdout, "%i", decimal_val);
    }
    But that is not right. The values saved in the bitPtr is wrong.

    Code:
    printVal(bitPtr[0]);
    printf("\n");
    convertToDec(bitPtr[0]);
    The printVal is right, but the convert to dec is wrong. I don't know how to convert the bitPtr[0] to an int that is equal to the ascii characters within, so bitPtr is actually 111000101, but when converting to decimal its getting a different number.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    @whiteflags:
    Although he's not very clear and has left out a lot of pertinent code, I don't think he's reading a text file with binary strings in it. I think he's reading a binary file and converting the bytes to binary strings.

    @TyrantUT:
    You need to show all of your code. And you need to copy/paste it as plain text. It's also best to forgo actual tabs in your code. Set your editor to insert spaces instead of tabs.

    You also need to describe your input and the purpose of your code.

    Your constants are unexplained ("magical"). What does 24720 mean?

    As I understand it, you're slurping in a binary file. Then you're transfering that data into a 2D char array. You could check the number of times any given "bit string" occurs with the original data. What's the point of transferring it to the 2D char array?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Letter Frequency counting.
    By Rezi in forum C Programming
    Replies: 18
    Last Post: 05-06-2011, 10:16 AM
  2. Counting number of strings in an array
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 06-21-2010, 11:25 AM
  3. Counting Frequency using linked list
    By ChoCo in forum C Programming
    Replies: 16
    Last Post: 03-10-2010, 11:48 AM
  4. Character frequency counting
    By zcrself in forum C Programming
    Replies: 2
    Last Post: 03-01-2010, 11:04 AM
  5. optimizing loop (frequency counting)... HELP
    By skeptik in forum C Programming
    Replies: 22
    Last Post: 05-24-2004, 09:11 PM

Tags for this Thread