NEED MAJOR HELP WITH THIS ASSIGNMENT

I AM TOTALLY LOST ON THIS ONE



Write a program that generates 100 random integers between 0 and 9 and displays the count for each number.

Hints:
Use rand() % 10 to generate random numbers from 0 … 9, and create an array to store the count of each number.
Create an array[100],using a loop fill the array with 100 random numbers from 0 to 9 rand() % 10.
Create another counter array with a size of 10 to capture the counts of each number
Use a for-loop to go through the array and increment the values of the array corresponding to the random values. (i.e. if the random number is 9, you should do counter[9]++, where counter[0] stores the count for zero, counter[1] stores the count for 1, etc.
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<cstdlib>
using std::rand;//The random function
using std::srand;//the initializes the rand generator
#include<ctime>
using std::time;//used to seed the generator to generate rand number

int main()
{
/*
*create an array to store the numbers generated
*create array for the number occurrence
*seed the generator 
*for(int i = 0***********)
*{
*    
*Hints:
*Use rand() % 10 to generate random numbers from 0 … 9,
*}
*for(int i*********)
*if the random number is 9, you should do counter[9]++
*/

return 0;
}


good luck ;)
and dont DOUBLE POST!! http://www.cplusplus.com/forum/beginner/215818/
Last edited on
HERE: figure out the rest.. this was very easy.. try a little harder :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main(void)
{
	int array[100] = { 0 };

	for (int j = 0; j < 100; j++)
	{
		array[j] = rand() % 10;
	}

	//Print Array
	for (int i = 0; i < 100; i++)
	{
		cout << "Number: " << i << " " << array[i] << endl;
	}

	system("PAUSE");
	return 0;
}

the program is supposed to generate 100 random numbers BETWEEN 0 and 9 not numbers all the way to 100
@AIRBONRE143

The program that Sanction is showing you, does only create numbers 0 to 9, and fills an array that can hold 100 numbers. Line 17 just prints out the array number, then shows what value is in array[i].

Lines 9 to 12, fills the 100 count array, with the numbers 0 to 9.
@whitenite1 I guess I was confused because the output that it should look like on my screen should be something like:

The number 0 was generated 21 times

The number 1 was generated 12 times

The number 2 was generated 14 times

The number 3 was generated 9 times

……….

The number 9 was generated 13 times

@AIRBONRE143

Just create another array that holds 10 numbers. (ie int holder[10]. When you run the j for loop, check what array[j] holds. If it's a 5, increase the count of holder[5] by 1.
as in..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main(void)
{
	int array[100] = { 0 };
	int holder[10] = {0};
	for (int j = 0; j < 100; j++)
	{
		array[j] = rand() % 10;
		holder[array[j]]++;
	}

	//Print Array
	for (int i = 0; i < 10; i++)
	{
		cout << "Number: " << i << " was chosen " << array[i] << " times." << endl;
	}

	system("PAUSE");
	return 0;
}
@whitenight1

I like that holder array idea, I would have done it a harder way but I like yours better. Learn something everyday :D :D
@whitenite1 That's pretty much it! now ive been trying to toy with your code to make the number of times it keeps track of the numbers 0-9 equel up to 100. So it should say the" number 0 was chosen 25 times" and so on. if that's not a good explanation than the best way I can describe it is 100 random numbers should be generated and split up evenly between 0-9.
Try this:
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
#include <iostream>

using namespace std;

int main(void)
{
	int array[100] = { 0 };
	int holder[10] = { 0 };

	for (int j = 0; j < 100; j++)
	{
		int x = rand() % 10;
		array[j] = x;
		holder[x] += 1;
	}

	//Print Array
	//for (int i = 0; i < 100; i++)
	//{
	//	cout << "Number: " << i << " " << array[i] << endl;
	//}

	for (int i = 0; i < 10; i++)
	{
		cout << "The Number " << i << " was generated " << holder[i] << " times!" << endl;
	}

	system("PAUSE");
	return 0;
}

@AIRBONRE143

Sorry. I forgot to change line 18 variable, from array[i], to holder[i].
Topic archived. No new replies allowed.