Generate a set of random numbers in the range of 50-100 and, for each set, count how many values fall into particular ranges

I almost have it, but the calculation is off when I try sorting the numbers into group. 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
int main(){
	
int i;
int hundred;
int ninety;
int eighty;
for ( i = 0; i<1; i++){
cout << "set 1" <<endl;
int hundred;
int ninety;
int eighty;
int seventy;
int sixty;
int fifty;
		
srand (1);
int num1 = (rand() % 50) + 50;	
int num2 = (rand() % 50) + 50;
int num3 = (rand() % 50) + 50;
int num4 = (rand() % 50) + 50;			
int num5 = (rand() % 50) + 50;
int num6 = (rand() % 50) + 50;		
int num7 = (rand() % 50) + 50;	
int num8 = (rand() % 50) + 50;	
int num9 = (rand() % 50) + 50;	
int num10 = (rand() % 50) + 50;	
cout << num1 << endl<< num2 << endl<< num3<<endl<<num4<<endl<<num5<<endl<<num6<<endl<<num7<<endl<<num8<<endl<<num9<<endl<<num10<<endl;

if (num1==100 || num2==100 || num3==100 || num4==100 || num5==100 || num6==100 || num7==100 || num8==100 || num9==100 || num10==100){

	hundred++; 
}
else if (num1<=99>90 || num2<=99>90 || num3<=99>90 || num4<=99>90 || num5<=99>90 || num6<=99>90 || num7<=99>90 || num8<=99>90 || num9<=99>90 || num10<=99>90){

ninety++;}
 
else if (num1<=89>80 || num2<=89>80 || num3<=89>80 || num4<=89>80 || num5<=89>80 || num6<=89>80 || num7<=89>80 || num8<=89>80 || num9<=89>80 || num10<=89>80){

eighty++; }

else if (num1<=79>70 || num2<=79>70 || num3<=79>70 || num4<=79>70 || num5<=79>70 || num6<=79>70 || num7<=79>70 || num8<=79>70 || num9<=79>70 || num10<=79>70){

seventy++; }

else if (num1<=69>60 || num2<=69>60 || num3<=69>60 || num4<=69>60 || num5<=69>60 || num6<=69>60 || num7<=69>60 || num8<=69>60 || num9<=69>60 || num10<=69>60){

sixty++; }

else if (num1<=59>50 || num2<=59>50 || num3<=59>50 || num4<=59>50 || num5<=59>50 || num6<=59>50 || num7<=59>50 || num8<=59>50 || num9<=59>50 || num10<=59>70)
{
fifty++; }

cout << "100s count: "<< hundred <<" "<< " 90s count: "<< ninety <<" "<< "80s count: "<<eighty<< " 70s count: "<<seventy<< " 60s count: "<<sixty<<
" 50s count: "<<fifty<<""<<endl;
	
	
}



return 0; 
}
Last edited on
Take a look at http://www.cplusplus.com/reference/random/uniform_int_distribution/

Do not forget operators / and %.


Please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
I almost have it


No, not really.

First of all, this is wrong:

else if (num1<=99>90 || ...

This doesn't do what you think it does. You really want this:

else if ((num1<=99 && num1>90) || .... You need to compare num1 against 99 and 90 separately.

Second, you don't do anything with values that are multiples of 10. You check for <= 99 but > 90. Nothing is done with 90.

Third, even if your comparison logic were correct, this is wrong:

1
2
else if (<num1 in range> || <num2 in range> || <num3 in range> ...)
counter++;


You are processing all of the values at once. If any of them are in the range, the counter is incremented. No matter how many are in the range, the counter is only incremented 1 time. You need to process all of the values separately. You need to check num1 against all of the ranges, then num2, etc.

This would be a whole lot easier if you used an array (or better yet a std::vector) with 10 elements and then used a loop to check each value within a single block of code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const int TOTAL = 10;
int num[TOTAL];

for (int i = 0; i < TOTAL; i++)
{
    num[i] = (rand() % 50) + 50;	
}

for (int i = 0; i < TOTAL; i++)
{
    if (num[i] == 100)
    {
        hundred++;
    }
    else if (num[i] >=90)
    {
        ninety++;
    }
    //etc.
}


Edit: Corrected typo in line 2 of the code


Edit 2: I just realized that you are not getting all of the values that you want. (rand() % 50) will give you a value between and including 0 - 49. When you add this to 50, you will never get 100.
Last edited on
@doug thanks for your help I really appreciate it
Topic archived. No new replies allowed.