Hello guys, this is my first time in these forums, so I apologize for my bad English. Well, here's the problem:
I need to get the frequency of integer numbers that are stored in a file, their range is from 0 to 100, their amount is unknown and depends on the length of the file. Working with the file is the easy part, but getting the frequency of each 100 numbers is.
I tried everything, even putting integers from 1 to 100 in an array, and comparing each of them with the numbers in file using two for loops, or just run loops to increment their counter that would be compared to the integers in the file, so it would be a typical code, like this:
// NOTE: dato is a pointer to a file
for (x=1; x<=100;x++){
freq=0;
for ( i=0; !feof( dato ) ; i++ ){
fscanf( dato, "%d", &n );
if (x==n) freq++;
}
printf ("\n number %d appears %d times", x, freq);
}
The problem is that I the only frequency result I get is 0. I know it's set to be 0 in the loop, but the value should be printed after each integer is checked and then reset to 0 at the beginning of the next x value, or am I wrong?
If I would try to get the freq of a single integer number, I would get the correct result:
freq=0;
for ( i=0; !feof( dato ) ; i++ ){
fscanf( dato, "%d", &n );
if (n==45) freq++;
}
printf ("\n number 45 appears %d times", freq);
But this way I would have to declare like 100 integer variables, and check their frequency individually with 100 lines of code (I'm still not that desperate, but getting close to it).
Like I've said I tried the same thing with arrays, to get the loops compare the numbers in file with the ones in one dimensional array and store their frequency in another array, but then freq value is 149086 or something like that.
I'm using Borland C++ 4.5, and I get the same result if I try to check frequency of any numbers. For some reason that just doesn't work.
I saw some posts in here about similar problems with numbers and character frequencies , and I tried to implement some of those solutions and codes to mine, but that didn't work.
So I'd like to say my question is simple, but I doubt it: how do I get integer numbers frequencies from a file? I'm not sure if I covered all of the details of the problem, but any help would be much appreciated.