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))
}