pointers/arrays/structures
I'm trying to go through a text file I'm passing and keep track of the numbers I'm reading in and the frequency they occurs in the 12 number file. But here's the thing, I only want to use pointers. The problem I'm running into is at the frequency part. When I compile and run the code, this is the output:
N: 10 F: 3
N: 10 F: 3
N: 8 F: 1
N: 7 F: 65
N: 5 F: 0
N: 4 F: 63
N: 10 F: 3
Segmentation fault (core dumped)
I had it printing out all the numbers before without it core dumping, but they were still wrong, as it is now (except the first 2). The N (number) is being stored in the hist struct array properly, but the F (frequency) is wrong after the first tens tens. I know the problem is in calcHistogram somewhere in the loops of chaos. Any tips would be greatly appreciated!
My guess would be that I'm going out of the range of the array somewhere, somehow. I simply can't find it though. Thanks in advance.
Code:
Code:
#include <stdio.h>
struct freq {
int number;
int frequency;
};
//function prototypes
void readScores(int* ar, int* count);
void displayScores(int* ar, int* count);
void calcHistogram(int* ar, int* count, struct freq* hist, int* ct);
int main() {
int ar[100];
int count = 0;
int ct = 0;
readScores(ar, &count);
displayScores(ar, &count);
struct freq hist[count];
printf("\n");
calcHistogram(ar, &count, hist, &ct);
}
void readScores(int* ar, int* count) {
for (int i=0; i<11; i++) {
(*count)++;
scanf("%d", (ar+i));
}
}
void displayScores(int* ar, int* count) {
for(int i=0; i<*count; i++) {
printf("score %d: %d\n", i, *(ar+i));
}
}
void calcHistogram(int* ar, int* count, struct freq* hist, int* ct) {
int uniqueNumber;
for(int i=0; i<*count; i++){
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);
}
}