Help counting occurrences.

Hey, im supposed to make a program that makes an array list of 1000 random numbers between 1 - 100 then find a way to count how many times each number shows up in that list. then display to the user the number that showed up the most and tell them how many times, and the number that showed the least showing them also how many times.

output example:

68 showed up 5 times making it the least.

3 showed up 56 times making it the most.

note: I understand some of the functions are unnecessary and could just be written in main() but part of this exercise is getting familiar with using functions, arrays, for loops and if statements

Anyone who can help me out putting me in the right direction would be grate. thanks for any help. the code ive done so far is below

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 #include <iostream>
#include <string>
#include <time.h>


using namespace std;

void randomizeSeed();
int randomRange(int min, int max);
int maxValue(int a, int b);
int minValue(int a, int b);

int main()
{

	randomizeSeed();

	const int num = 1000;
	const int minNumber = 1;
	const int maxNumber = 100;
	int Values[num] = {};
	int Occur[maxNumber] = {};
	
	for (int i = 0; i < num; i++)
	{
		Values[i] = randomRange(minNumber, maxNumber);
		//cout << Values[i];
	}
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < maxNumber; j++)
		{
			if (j + 1 == Values[i])
			{
				Occur[j]++;
			}
		}

	}
	cout << Occur;

	system("pause");

	return 0;
}

void randomizeSeed()
{
	srand(time(NULL));
}

int randomRange(int min, int max)
{
	int randomValue = rand() % (max + 1 - min) + min;
	return randomValue;
}

int maxValue(int a, int b)
{
	return (a < b) ? b : a;
}

int minValue(int a, int b)
{
	return (a < b) ? a : b;
}
Last edited on
closed account (E0p9LyTq)
#1 question, are you required to use the C library random functions, or are you allowed to use the C++ library?
Last edited on
Without even looking at your code... Here is a simple (maybe not the correct) way to do this...

Make an array of 1000 ints.
for loop to 1000
a=rand()*100
arrayname[a]=+1;
Loop your array, if arrayname[]>0 then cout arrayname[]'s value
Go to sleep
get an A+
Last edited on
got the array of 1000 ints using a rand function to generate the 1000 ints to random nums
have a for loop of 1000
i have a if for arrayname[a] = + 1
i dont really understand everything else you are saying though could you explain more?

as to what furryguy is asking:

Im using #include <time.h> library along with

void randomizeSeed()
{
srand(time(NULL));
}

to randomize the numbers if thats what you are asking. im only using the C++ library as far as i know
I mean all you need to accomplish this is 2 loops.

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 <stdlib.h>
#include <time.h>

using namespace std;

int main(){

    srand(time(0));
    int number=0;
    int val=0;
    int rnumber[1000]={0};   // Make an array of 1000 ints.


for (int i=0; i<=999; i++){
number=rand()%100;  // grab a number
rnumber[number]=rnumber[number]+1;
}


for (int i=0; i<=999; i++){
if (rnumber[i] >0){
cout << i << " was selected "<< rnumber [i]<< " times." << endl;
val=val+rnumber[i];
}
}
cout << val << endl; //  adds up the amount of all selected numbers. Proof you did 1000 loops


return 0;
}


Now from there, go add your functions, and whatever you need to make it right.
Last edited on
Topic archived. No new replies allowed.