Need help on arrays

How would I modify this code appropriately using arrays?

[code]
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

void eggOdds();
using namespace std;

int main(){
eggOdds();
return 0;

}
void eggOdds(){
int freqGolden = 0,
freqBlue = 0,
freqNoEgg = 0,
freqGreen = 0,
freqPurple = 0;

int randOdd = 0;
srand(time(0));
for(int i = 1; i<= 1000; i++){
randOdd = 1 + rand() % 100;
if(randOdd <= 3)
freqGolden++;
else if(randOdd <= 13) freqBlue++;
else if(randOdd <= 30) freqNoEgg++;
else if(randOdd <= 50) freqGreen++;
else freqPurple++;
}//end for

cout << "Egg " << "\t\t" << "FREQUENCY" << endl; cout << " Golden Egg" << "\t" << freqGolden << "\n Blue Egg" << "\t" << freqBlue << "\n No Egg " << "\t" << freqNoEgg << "\n Green Egg" << "\t" << freqGreen
<< "\n Purple Egg" << "\t" << freqPurple << endl;
}
Last edited on
How would I modify this code appropriately using arrays?

Could you please explain better what you aim to do? Do you want all your "freq*" variables become 'cells' of an array?
If yes, may I suggest you a struct, instead? Unless, of course, you don't need an array for a specific purpose.

Hello sir jeffers

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I think you may have missed the closing [/code] tag.

I think this might be what you want. See how it differs from your program. I have some comments on what I added.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

void eggOdds();

enum COLOURS
{
	GOLDEN, BLUE, NOEGG, GREEN, PURPLE, MAXCOLOURS  // <--- Colours start at 0 and end up at 4.
	                                                // MAXCOLOURS lets you know how many usable colours there are.
};

int main()
{
	eggOdds();

	system("pause");

	return 0;
}

void eggOdds()
{
	//int freqGolden = 0,
	//	freqBlue = 0,
	//	freqNoEgg = 0,
	//	freqGreen = 0,
	//	freqPurple = 0;
	int freq[5]{ 0 };// <--- An array of 5 elements. { 0 } initialized all elements to zero.

	int randOdd = 0;

	srand(time(0));

	for (int i = 1; i <= 1000; i++)
	{
		randOdd = 1 + rand() % 100;
		if (randOdd <= 3)
			freq[GOLDEN]++;
		else if (randOdd <= 13) freq[BLUE]++;
		else if (randOdd <= 30) freq[NOEGG]++;
		else if (randOdd <= 50) freq[GREEN]++;
		else freq[PURPLE]++;
	}//end for

	cout << "  Egg " << "\t\t" << "FREQUENCY" << endl; cout << " Golden Egg" << "\t" << freq[GOLDEN]
		<< "\n Blue Egg" << "\t" << freq[BLUE] << "\n No Egg " << "\t" << freq[NOEGG]
		<< "\n Green Egg" << "\t" << freq[GREEN]
		<< "\n Purple Egg" << "\t" << freq[PURPLE] << endl;
}


The "enum" works with subscripts of the array to keep things in order.

Hope that helps,

Andy

P.S. Forgot to mention that srand(time(0)); is better placed in main.
Last edited on
Topic archived. No new replies allowed.