Stuck on C++ problem

I've been currently stuck on a C++ problem for about an hour and half. Here's the question:

Write a program that generates one hundred random integers between 0 and 9 and displays the count for each number. (Hint: Use rand()
% 10 to generate a random integer between 0 and 9. Use an array of ten integers,
say counts, to store the counts for the number of O's, l 's, . .. , 9's.)

And here's what I have so far. I think I'm pretty close, but I keep on getting "0" for the occurrences (or counts) of each random integer. Any help would be greatly appreciated.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <fstream>

using namespace std;

const int SIZE = 100;

int main()
{
	int integers[SIZE];
	int index;
	int zero = 0;
	int one = 0;
	int two = 0;
	int three = 0;
	int four = 0;
	int five = 0;
	int six = 0;
	int seven = 0;
	int eight = 0;
	int nine = 0;

	cout << "The following 100 integers are random:" << endl;
	cout << endl;

	srand(time(0));

	for (index = 0; index < SIZE; index++)
	{
		integers[SIZE] = rand() % 10;
		cout << integers[SIZE] << " ";
	}

	cout << endl;

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 0)
		{
			zero += 1;
		}
	}

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 1)
		{
			one += 1;
		}
	}

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 2)
		{
			two += 1;
		}
	}

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 3)
		{
			three += 1;
		}
	}

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 4)
		{
			four += 1;
		}
	}

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 5)
		{
			five += 1;
		}
	}

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 6)
		{
			six += 1;
		}
	}

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 7)
		{
			seven += 1;
		}
	}

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 8)
		{
			eight += 1;
		}
	}

	for (index = 0; index < SIZE; index++)
	{
		if (integers[index] == 9)
		{
			nine += 1;
		}
	}

	cout << "The number of zeros in the random list are " << zero << endl;
	cout << "The number of ones in the random list are " << one << endl;
	cout << "The number of twos in the random list are " << two << endl;
	cout << "The number of threes in the random list are " << three << endl;
	cout << "The number of fours in the random list are " << four << endl;
	cout << "The number of fives in the random list are " << five << endl;
	cout << "The number of sixes in the random list are " << six << endl;
	cout << "The number of sevens in the random list are " << seven << endl;
	cout << "The number of eights in the random list are " << eight << endl;
	cout << "The number of nines in the random list are " << nine << endl;

	getch();

	return 0;

}
in your assignment there is said very clear "Use an array of ten integers"
And in any case this code is invalid

1
2
3
4
5
	for (index = 0; index < SIZE; index++)
	{
		integers[SIZE] = rand() % 10;
		cout << integers[SIZE] << " ";
	}


Last edited on
Vlad is exactly right.

Let me suggest that when you having a tonne of if statements often means your code is doing something repetitive that you should code differently (with fewer lines).

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
#include <iostream>
#include <algorithm>
#include <set>
#include <ctime>


int main()
{
    std::multiset<int> s;

    enum {SIZE = 100, LIMIT = 10};
    int array[SIZE];
    std::srand(std::time(nullptr));
    for (int i = 0; i < SIZE; ++i)
    {
        array[i] = std::rand() % LIMIT;
        s.insert(array[i]);
    }

    for (int i = 0; i < LIMIT; ++i)
        std::cout
            << i << " occurs "
            << s.count(i)
            << " times\n";

    return 0;
}


Edit: (Didn't read the task too clearly.)

Array of 10 ints here.

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 <ctime>


int main()
{
    enum {SIZE = 100, LIMIT = 10};

    int array[LIMIT];
    for (int i = 0; i < LIMIT; ++i)
        array[i] = 0;

    std::srand(std::time(nullptr));

    for (int i = 0; i < SIZE; ++i)
    {
        int temp = std::rand() % LIMIT;
        array[temp] += 1;
    }

    for (int i = 0; i < LIMIT; ++i)
        std::cout << i << " occurs " << array[i] << " times\n";

    return 0;
}
Last edited on
1
2
// int array[LIMIT];
int array[LIMIT] = {0} ;

I made some minor updates

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


int main()
{
    enum { SIZE = 100, LIMIT = 10 };

    int counts[LIMIT] = {};

    std::srand( ( unsigned int )std::time( nullptr ) );

    for ( int i = 0; i < SIZE; ++i ) ++counts[ std::rand() % LIMIT ];

    for ( int i : counts )
    {
        std::cout << i << " occurs " << counts[i] << " times\n";
    }

    return 0;
}
Last edited on
Topic archived. No new replies allowed.