Thread: pointers/arrays/structures

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think the fact that you are displaying duplicates means you are not calculating your histogram correctly. Another function (or two) would be good to separate two different parts of your code. This could also help you isolate your problem and test your code.

    First, create a function for printing the histogram data. Something like:
    Code:
    void printHistogram(struct freq *hist, int histSize)  // note, histSize is not a pointer
    Make sure that works, so you can use it to see the contents of the histogram as you test your code. You can make a static array of histogram data to test:
    Code:
    struct freq testHist[3] = {
        {10, 2},
        {1, 1},
        {7, 3}
    };
    printHistogram(testHist, 3);
    Second, I would suggest a function like the following:
    Code:
    int addNumToHistogram(struct freq *hist, int *histSize, int maxHistSize, int num)
    That function will search hist for num. If num is alread in the histogram, it will increment the count, otherwise it adds it to the end (assuming it doesn't exceed maxHistSize), with a count of 1, and increments *histSize. Use that function by itself (without calcHistogram), and simply insert 5 or so numbers with this function, with, perhaps one or two duplicates to test. Use the print function to make sure it looks correct. Like so:
    Code:
    struct freq testHist[10] = {{0}};
    int histSize = 0;
    addNumToHistogram(hist, &histSize, 10, 7);  // add 7 to the histogram
    addNumToHistogram(hist, &histSize, 10, 7);  // add 7 to the histogram
    addNumToHistogram(hist, &histSize, 10, 3);  // add 3 to the histogram
    addNumToHistogram(hist, &histSize, 10, 4);  // add 4 to the histogram
    addNumToHistogram(hist, &histSize, 10, 3);  // add 3 to the histogram
    addNumToHistogram(hist, &histSize, 10, 1);  // add 1 to the histogram
    printHistogram(hist, histSize);
    Once you have those two functions working, the rest should be easy. You can simply call that addNumToHistogram from calcHistogram:
    Code:
    void calcHistogram(int* ar, int* count, struct freq* hist, int* ct)
    {
        for i from 0; < *count
            addNumToHistogram(hist, ct, *(ar + i))
    }

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by anduril462 View Post
    I think the fact that you are displaying duplicates means you are not calculating your histogram correctly. Another function (or two) would be good to separate two different parts of your code. This could also help you isolate your problem and test your code.

    First, create a function for printing the histogram data. Something like:
    Code:
    void printHistogram(struct freq *hist, int histSize)  // note, histSize is not a pointer
    Make sure that works, so you can use it to see the contents of the histogram as you test your code. You can make a static array of histogram data to test:
    Code:
    struct freq testHist[3] = {
        {10, 2},
        {1, 1},
        {7, 3}
    };
    printHistogram(testHist, 3);
    Second, I would suggest a function like the following:
    Code:
    int addNumToHistogram(struct freq *hist, int *histSize, int maxHistSize, int num)
    That function will search hist for num. If num is alread in the histogram, it will increment the count, otherwise it adds it to the end (assuming it doesn't exceed maxHistSize), with a count of 1, and increments *histSize. Use that function by itself (without calcHistogram), and simply insert 5 or so numbers with this function, with, perhaps one or two duplicates to test. Use the print function to make sure it looks correct. Like so:
    Code:
    struct freq testHist[10] = {{0}};
    int histSize = 0;
    addNumToHistogram(hist, &histSize, 10, 7);  // add 7 to the histogram
    addNumToHistogram(hist, &histSize, 10, 7);  // add 7 to the histogram
    addNumToHistogram(hist, &histSize, 10, 3);  // add 3 to the histogram
    addNumToHistogram(hist, &histSize, 10, 4);  // add 4 to the histogram
    addNumToHistogram(hist, &histSize, 10, 3);  // add 3 to the histogram
    addNumToHistogram(hist, &histSize, 10, 1);  // add 1 to the histogram
    printHistogram(hist, histSize);
    Once you have those two functions working, the rest should be easy. You can simply call that addNumToHistogram from calcHistogram:
    Code:
    void calcHistogram(int* ar, int* count, struct freq* hist, int* ct)
    {
        for i from 0; < *count
            addNumToHistogram(hist, ct, *(ar + i))
    }

    I think it'd be cool if I could get it to add to frequency rather than fill in another N, like i just said, but in the same function..i feel like that is doable...Here's my function

    Code:
    void calcHistogram(int* ar, int* count, struct freq* hist, int* ct) {
        int uniqueNumber;
        for(int i=0; i<*count; i++){
                (*(hist+i)).frequency = 0;
                uniqueNumber = *(ar+i);
                for(int j=0; j<=*ct; j++){
                       if(uniqueNumber != (*(hist+i)).number){
                              (*(hist+i)).number = uniqueNumber;
                       }
                       if(*(ar+i) == *(ar+j)){
                             (*(hist+i)).frequency += 1;
                             (*ct)++;
                   }
            }
            printf("N: %d F: %d\n", (*(hist+i)).number, (*(hist+i)).frequency);
        }
    }
    And here is the output of this:

    N: 10 F: 2
    N: 10 F: 2
    N: 8 F: 1
    N: 7 F: 1
    N: 5 F: 1
    N: 4 F: 1
    N: 10 F: 3
    N: 2 F: 2
    N: 2 F: 2
    N: 8 F: 2
    N: 7 F: 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays of pointers inside structures.
    By Posto2012 in forum C Programming
    Replies: 7
    Last Post: 11-18-2009, 12:58 AM
  2. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  3. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 06:29 PM

Tags for this Thread