Can someone help simplify the end of this?

can some one simply the end of this program with while loops and please explain why you solution works?
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
srand(time(0));
int number;
int max = 100;
int min = 0;
int repetetion;
int oneToTen = 0;
int elevenToTwenty = 0;
int twentyToThrity = 0;
int thirtyToFourty = 0;
int fourtyToFifty = 0;
int fiftyToSixty = 0;
int sixtyToSeventy = 0;
int seventyToEighty = 0;
int eightyToNinety = 0;
int ninetyToOneHundred = 0;
repetetion = 0;
while (repetetion < 10000)

{
number = rand()% (max - min + 1) + min;
if (number < 11 && number > 0)
{
++oneToTen;
}
if (number < 21 && number > 10)
{
++elevenToTwenty;
}
if (number < 31 && number > 20)
{ ++twentyToThrity;
}
if (number < 41 && number > 30)
{
++thirtyToFourty;
}
if (number < 51 && number > 40)
{
++fourtyToFifty;
}

if (number < 61 && number > 50)
{
++fiftyToSixty;
}
if (number < 71 && number > 60)
{
++sixtyToSeventy;
}
if (number < 81 && number > 70)
{
++seventyToEighty;
}
if (number < 91 && number > 80)
{
++eightyToNinety;
}
if (number < 101 && number > 90)
{
++ninetyToOneHundred;
}
++repetetion;
{

}
}
{
cout << "Welcome to the Random Number Historgram Program" << endl;
cout << " =================================== " <<endl;
cout << " Range Frequency (1 line = 50 occurances)" << endl;
cout << "===================================" << endl;
}

{
oneToTen = oneToTen/50;
elevenToTwenty = elevenToTwenty/50;
twentyToThrity = twentyToThrity/50;
thirtyToFourty = thirtyToFourty/50;
fourtyToFifty = fourtyToFifty/50;
fiftyToSixty = fiftyToSixty/50;
sixtyToSeventy = sixtyToSeventy/50;
seventyToEighty = seventyToEighty/50;
eightyToNinety = eightyToNinety/50;
ninetyToOneHundred = ninetyToOneHundred/50;

}

if (oneToTen == 18)
{
cout << "0-10: |||||||||||||||||| " << endl;
}
else if (oneToTen == 19)
{
cout << "0-10: |||||||||||||||||||" << endl;
}
else if (oneToTen == 20)
{
cout << "0-10: ||||||||||||||||||||" << endl;
}
else if (oneToTen == 21)
{
cout << "0-10: |||||||||||||||||||||" << endl;
}
else if (oneToTen == 22)
{
cout << "0-10: ||||||||||||||||||||||" << endl;
}
if (elevenToTwenty == 18)
{
cout << "11-20: |||||||||||||||||| " << endl;
}
else if (elevenToTwenty == 19)
{
cout << "11-20: ||||||||||||||||||| " << endl;
}
else if (elevenToTwenty == 20)
{
cout << "11-20: |||||||||||||||||||| " << endl;
}
else if (elevenToTwenty == 21)
{
cout << "11-20: ||||||||||||||||||||| " << endl;
}
else if (elevenToTwenty == 22)
{
cout << "11-20: |||||||||||||||||||||| " << endl;
}
if (twentyToThrity == 18)
{
cout << "21-30: |||||||||||||||||| " << endl;
}
else if (twentyToThrity == 19)
{
cout << "21-30: ||||||||||||||||||| " << endl;
}
else if (twentyToThrity == 20)
{
cout << "21-30: |||||||||||||||||||| " << endl;
}
else if (twentyToThrity == 21)
{
cout << "21-30: ||||||||||||||||||||| " << endl;
}
else if (twentyToThrity == 22)
{
cout << "21-30: |||||||||||||||||||||| " << endl;
}
if (thirtyToFourty == 18)
{
cout << "31-40: |||||||||||||||||| " << endl;
}
else if (twentyToThrity == 19)
{
ETC >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

The goal of the program is to produce a histogram like this im asking for someone to simply the ending of the program using while loops.


Welcome to the Random Number Generator Tester

===================================

Range Frequency (1 line = 50 occurances)
===================================

1-10 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||

11-20 |||||||||||||||||||||||||||||||||||||||||||||||||||||

21-30 |||||||||||||||||||||||||||||||||||||||||||||||

31-40 |||||||||||||||||||||||||||||||||||||||||||||||||||||||

41-50 ||||||||||||||||||||||||||||||||||||||||||||||||||||||

51-60 |||||||||||||||||||||||||||||||||||||||||||||

61-70 |||||||||||||||||||||||||||||||||

71-80 ||||||||||||||||||||||||||||||||||||||

81-90 ||||||||||||||||||||||||||||||||||||||||||||||||||

91-100 ||||||||||||||||||||||||||||||||||||||||||||||






system("PAUSE");
return EXIT_SUCCESS;
}
Last edited on
#include <string>

And then, replace the series of if statements with:

1
2
3
4
    const char symbol = '|' ;
    std::cout << std::string( oneToTen, symbol ) << '\n' ;
    std::cout << std::string( elevenToTwenty, symbol ) << '\n' ;
    std::cout << std::string( twentyToThrity, symbol ) << '\n' ;


Without changing its behaviour in any way, the program can be shortened to:

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 <cstdlib>
#include <iostream>
#include <ctime> // required for std::time()
#include <iomanip> // std::setw
#include <string> // std::string

int main()
{
    enum { MIN = 0, MAX = 100, N = MAX/10 - MIN/10 - 1, REPETITIONS = 10000, SCALE = 50 } ;
    int frequency[N] = {0} ;
    std::srand( std::time(0) ) ;

    for( int i=0 ; i < REPETITIONS ; ++i )
    {
       int number = std::rand() % ( MAX - MIN + 1 ) + MIN ;
       ++frequency[ (number-1) % 10 ] ;
    }

    std::cout << "Welcome to the Random Number Historgram Program\n"
                 " ===================================\n"
                 " Range Frequency (1 line = " << SCALE << " occurances)\n"
                 " ===================================\n" ;

    const char symbol = '|' ;
    for( int i=0 ; i<N ; ++i )
    {
        std::cout << std::setw(4) << i*10 + 1 << " - " << (i+1) * 10 << ' '
                  << std::string( frequency[i] / SCALE, symbol ) << '\n' ;
    }
}


Topic archived. No new replies allowed.