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.

You can use a simple distribution count, I believe. Your verbosity left my eyes glazed over a bit. ;)

int count[101] = { 0 }; //assign all elements to zero value

if (your char is a number from 1 to 100)
  count[char + '0']++;

//remember count[0] will not be used here.

This is also called "bin sorting" or "binning", among other terms. Useful trick.

just run loops to increment their counter

So far so good.

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?

One immediate problem here is that after the very first iteration you've reached end of file, and nothing could be read anymore. That said, the approach is horrible.

...
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.

Most likely you forgot to initialize them. The array is a way to go. BTW, you don't need an array with numbers and comparison loop. A single array with frequencies will do it.

int count[101] = { 0 }; //assign all elements to zero value

if (your char is a number from 1 to 100)
  count[char + '0']++;

//remember count[0] will not be used here.

Your example is just a little bit wrong. The file contains integers, so if they are read as ints then no conversion is necessary

int count[101] = { 0 }; //assign all elements to zero value

if (the value of the integer is a number from 1 to 100)
  count[number]++;

//remember count[0] could be used here if the value of the
// integer in the file were 0

Good eye, AD, good eye! ;)

Now if only the OP will understand the significance of your post and how easy it will make his program's solution.

OMG, I don't believe this o_O

That's it? All I had to do was to check file numbers with the ones in the array and not vice-versa?

Thanks guys, this was helpful a lot. I was about to bang my head against the wall with this simple problem.

So... Why does the second loop brake after it reaches the end of the file? Shouldn't it start to read that file from the beginning once it starts all over in the next step of the first loop? I thought it doesn't matter, is it a compiler or logic error?

Thanks again.

When EOF is reached you have to call rewind(fp) to move the file pointer back to the beginning of the file so that it can be read all over again. But why??? Two loops are completely unnecessary and a waste of CPI time. Use an array as Adak suggested and you can do it with only one read of the data file. Your method would require 100 reads of the file :icon_eek:

Oh, yeah I know that, I do waste a lot of CPU time, so I already did it the way Adak suggested.

Thanks again people :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.