Frequency of Numbers

Hey, I need to write a program that generates a set of 1000 numbers between 0-10. They need to be saved in a text file and the frequency of every number has to be displayed.

I can't figure out how to display the frequency, can anyone help? Here's what I have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

void main(void)
{
	  int num[1001];
	  ofstream output;

	  output.open("H:\\random_1000.txt");
		cout<<"Now generating numbers.\n";

	randomize();
	int x=0;
	for (int i = 0; i <= 1000; i++){
		x = rand()% 11;
		output<<x<<'\n';
		cout<<x<<endl;
	}
	output.close();

	char z;
	cin>>z;
}
I would probably just have a map or something that relates each number to the number of times it has appeared and increment that in the loop.

++(frequency[x]); or something.
how would I go about relating the number to the times it has appeared though?
You could have an array of 11 values which stores how many of each generated number as they appear:

 
int frequency[11];


And then in your loop:

 
frequency[x]++;


So the totals of each possible value from 0 to 10 will be accumulated.
Last edited on
Ok, that makes sense, I dunno how to display it though. When I run the program it seems as if the first number is right, but then it just adds one to it on through, or sometimes it will be a ridiculously huge number. How do I count it so that it will count the variable in the array instead of adding one? I'm not quite sure that i have my frequency array in the right spot too. Here's my updated code so far... Thanks for all the help too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

void main(void)
{
		  int num[1001];
	  int freq[11];
	  ofstream output;

	  output.open("H:\\random_1000.txt");
		cout<<"Now generating numbers.\n";

	randomize();
	int x=0;
	for (int i = 0; i <= 1000; i++){
		x = rand()% 11;
		freq[x]++;
		output<<x<<'\n';
		cout<<x<<endl;
}
	for (int i = 0; i <= 10; i++) {
		cout<<freq[x]<<endl;
		freq[11]++;
	}
output.close();

	char z;
	cin>>z;
}


Last edited on
Before incrementing your frequency (line 19 of your code), you need to initialize your freq array to zero. Otherwise, when you increment it you're incrementing whatever value that was in that memory location, and that's not good.

1
2
3
4
for ( int i = 0; i < 11; i++ )
    {
          freq[i] = 0;		
    }


You can also remove the incrementing being done to freq in line 25. That line is unnecessary. I'm not sure what your randomize function does, but make sure you seed before the randomization begins. Like I said, this may be in your randomization function, but I don't know.

How to seed:
http://cplusplus.com/reference/clibrary/cstdlib/srand/
I'm so confused by this. I read the article and I have no idea how i can implement this into a loop. So far it seems as if it's only outputting the first number in the array and nothing else. At least this time i'm not getting random huge integers. I've tried a couple different things and either it just adds one to the frequency, or nothing happens at all. I appreciate all the help, but i'm looking at a dead end here right now. Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	int num[1001];
	  int freq[11];
	  ofstream output;
	  int x = 0   ;

	  output.open("H:\\random_1000.txt");
		cout<<"Now generating numbers.\n";


	for ( int i = 0; i < 11; i++ )
	{
		  freq[i] = 0;
	}

srand ( time(NULL) );
	for (int i = 0; i <= 1000; i++){
		x = rand()% 11;
		freq[x]++;
		output<<x<<'\n';
		cout<<x<<endl;
}
	for (int i = 0; i <= 10; i++) {
		cout<<freq[x]<<endl;
	}
output.close();

	char z;
	cin>>z;
@Line 23: That should be i, not x.

Also, your file will be empty. Guess why?

-Albatross
Wow, I feel dumb now, lol. I also forgot to put in the output<<freq[i]<<'\n'; in my frequency for loop. Something doesn't feel right though, I think my integers before my frequency numbers are different than they are in the console than they are in the text document. Would that be because it's just outputting x and not the array values? Because I notice too that I don't even use my num[1001] array anywhere in the program.

EDIT: Ok, I see that it's basically writing two sets of random numbers, one to the text and one to the console... which is the whole reason why i'm using arrays. How would I go about and implent my num[1000] array to be filled with random numbers and using the counter?
Last edited on
How would I go about and implent my num[1000] array to be filled with random numbers and using the counter?


Something like the following should do the trick:

1
2
3
4
5
6
7
8
for ( int i = 0; i <= 1000; i++ )
    {
        x = rand() % 11;
        num[i] = x;
        freq[x]++;
        output << x << '\n';
        cout << x << endl;
    }


Alternatively, the output statements could be replaced by:
1
2
output << num[i] << '\n';
cout << num[i] << endl;
Thanks
Topic archived. No new replies allowed.