Find lowest index in array

For my C++ class we have to make a program that generates 5000 random numbers between 1 and 53, then outputs each number and how many times it was generated.Then we have to find and output the number that was generated the most, and the number that was generated the least. I have the code figured out for everything but I'm not sure about my code for finding the number generated least.

Here's my code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
int x, y, count = 0, max_number = 0, min_number = 200, number_count[53], random_number[5000];
srand(time(0));

cout << "Random Number Generation\n";
cout << "************************";

cout << "\n\n Number # of times\n\n";

for(x = 0; x < 5000; x++)
random_number[x] = 1 + rand() % 53;

for(y = 1; y <= 53; y++)
{
for(x = 0; x < 5000; x++)
{
if(random_number[x] == y)
count++;
}
number_count[y] = count;

if(number_count[max_number] < number_count[y])
max_number = y;

if(number_count[min_number] > number_count[y])
min_number = y;

cout << setw(3) << y << setw(15) << setiosflags(ios::right) << number_count[y] << endl;
count = 0;
}

cout << "\n\nThe number that came in the least was " << min_number << endl;
cout << "\n\nThe number that came in the most was " << max_number << endl;

system("PAUSE");
}


When I do this, it works correctly, but I'm not sure about initializing min_number to 200. It works correctly, but it seems like that's not the best way to do it. If I initialize it too low, it won't work properly, and if I initialize too high(I tried initializing it to 4999 as well) it results in a fatal error. It seems like there's a better way to go about doing it, but I'm not sure how exactly.

By the way, if there's anything else I can do better, let me know. Keep in mind that I don't know much beyond arrays.
Ok in the process of posting my post above I messed something up and now it tells me "The number that came in the most was 0"
Last edited on
I'll post some code here to get you a bit further, and then make some suggestions on how you could make it better.

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int x, y, count = 0, max_number, min_number;
	int max_count = 0, min_count = 5000, random_number[5000];
	srand(time(0));

	cout << "Random Number Generation\n";
	cout << "************************";

	cout << "\n\n Number # of times\n\n";

	for(x = 0; x < 5000; x++)
		random_number[x] = 1 + rand() % 53;

	for(y = 1; y <= 53; y++)
	{
		for(x = 0; x < 5000; x++)
		{
			if(random_number[x] == y)
				count++;
		}

		if(count > max_count)
		{										// "If the count for this number is greater
			max_number = y;						// than the previously highest count"
			max_count = count;
		}
			
		if(count < min_count)
		{							// Same as above, but for minimum count
			min_number = y;
			min_count = count;
		}

		cout << setw(3) << y << setw(15) << setiosflags(ios::right) << count << endl;
		count = 0;
	}

	cout << "\n\nThe number that came in the least was " << min_number << endl;
	cout << "\n\nThe number that came in the most was " << max_number << endl;

	system("PAUSE");
}


A brief description of the above:
1. Populate the 5000 randoms, like you did.
2. Repeat for each possible random number value:
Go through the randoms and find the number of occurrences, 'count'.
If this 'count' is greater than our previously greatest number of occurrences 'maxCount', then this integer now has our greatest count.
Same as above, but for minimum count.
Housekeeping (reset count to zero, increment y etc).
End loop over possible random numbers.
3. Print the numbers which had maxCount and minCount.

I hope you can see why I initialised maxCount and minCount as shown.

Initialising the min_number to 200 causes a problem on your original line:
if(number_count[min_number] > number_count[y]) {...}

'number_count' can't be indexed over 52, so any min_number greater than that is bad news. I'm surprised you said it "worked correctly"!

Suggestions to make it better:
1. Don't use system("PAUSE") because it's platform-specific (Windows). There are many threads on this site discussing better alternatives.
2. Don't hard-code numbers (even though I left them above). A better way would be to #define constants, or use iterators when going over a collection (worry about that later).
3. Challenge: Try to do it all in one loop. To do this you'd probably need to bring back in the 'number_count' array which I took out.

Hope that helps :).

EDIT: Hmmmm...I should work on reducing the width of my [code] blocks, sorry :).
Last edited on
Thanks. That makes sense. It works. That's odd that I had it working right before.

Thanks for the tips. I'm aware of the system("PAUSE") issue. But it's a lot easier to test these programs with it. If I was doing any serious work, I wouldn't be using it.

Can you explain how the #define would work? Also, I was wondering, you have you variable declarations on two lines...I'm assuming it doesn't, but does that make a difference compared to having it all on one line?

When I was first working on this I was trying to do it in one loop and I was getting confused. I thought it was due Tuesday so I was scrambling to figure it out, then I learned it was due Thursday lol. I'll see if I can get it to work in one loop.
A better way would be to #define constants, or use iterators when going over a collection (worry about that later).


This could be just a misunderstanding but do not use #define to create constants when writing C++ code. Declare const types at the appropriate scope.

1
2
3
const int SIZE(20);
int MyArray[SIZE];
std::generate(MyArray, MyArray + SIZE, rand);
Topic archived. No new replies allowed.