I'm trying to go through a text file I'm passing and keep track of it and the frequency it occurs in the 12 number file. 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
N: 2 F: 1
N: 2 F: 32769
N: 8 F: 32767
N: 7 F: 32767
N: 7 F: 12
I would like to set it up as to not create a struct for repeat numbers, instead ONLY increase F by one... as you can see the number prints out correctly but frequency is rather janked up. I know the problem is in calcHistogram somewhere in the loops of chaos. Any tips would be greatly appreciated!
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
N: 2 F: 1
N: 2 F: 32769
N: 8 F: 32767
N: 7 F: 32767
N: 7 F: 12
I would like to set it up as to not create a struct for repeat numbers, instead ONLY increase F by one... as you can see the number prints out correctly but frequency is rather janked up. I know the problem is in calcHistogram somewhere in the loops of chaos. Any tips would be greatly appreciated!
struct freq {
int number;
int frequency;
};
void calcHistogram(int* ar, int* count, struct freq* hist, int* ct);
void readScores(int* ar, int* count);
void displayScores(int* ar, int* count);
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) {
//while(2!=EOF){ do this later
for (int i=0; i<12; 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);
}
}