Need Help with a program I am creating....

J

joebenjamin

I am trying to write a program that will generate 100 random numbers
between 1 and 50. Using these numbers, generate a list that will tell the
number of random numbers that fell between 1-5, 6-10, 11-15, 16-20, ... ,
and 46-50. I want to then print out the results as a histogram. Basically
it might look like this:

1-5 (11) ***********
6-10 (8) ********
11-15 (12) ************
16-20 (9) *********
21-25 (10) **********
26-30 (11) ***********
31-35 (7) *******
36-40 (8) ********
41-45 (13) *************
46-50 (11) ***********

Any ideas or tutorials on histograms would be helpful. Also I need to know
how to generate random numbers then group them. At least if I could do this
without the histogram that would lead me in the right direction. I have
this started, but I know that it is not right. It will generate the
numbers but that is all I have so far.

Any help is appreciated....
 
C

Chris Johnson

I am trying to write a program that will generate 100 random numbers
between 1 and 50. Using these numbers, generate a list that will tell the
number of random numbers that fell between 1-5, 6-10, 11-15, 16-20, ... ,
and 46-50. I want to then print out the results as a histogram. Basically
it might look like this:

1-5 (11) ***********
6-10 (8) ********
11-15 (12) ************
16-20 (9) *********
21-25 (10) **********
26-30 (11) ***********
31-35 (7) *******
36-40 (8) ********
41-45 (13) *************
46-50 (11) ***********

Any ideas or tutorials on histograms would be helpful. Also I need to know
how to generate random numbers then group them. At least if I could do this
without the histogram that would lead me in the right direction. I have
this started, but I know that it is not right. It will generate the
numbers but that is all I have so far.

It sounds like you want algorithm help, and this isn't the right
newsgroup for that. Once you've got your algorithm sorted out, if
you're having trouble implementing it, come back and post what you've
got, and we should be able to help you figure it out.
 
R

Richard Heathfield

joebenjamin said:
I am trying to write a program that will generate 100 random numbers
between 1 and 50. Using these numbers, generate a list that will tell
the number of random numbers that fell between 1-5, 6-10, 11-15,
16-20, ... , and 46-50. I want to then print out the results as a
histogram.

First, we need to count the number of times each subrange is
encountered.

Clearly, an array is appropriate. Equally clearly, the array needs only
ten elements, since there are only ten subranges. Each should start
with the value 0 (because, until you start counting, you haven't
counted any, which isn't *quite* the same thing as saying you've
counted 0, but let's set sophistry aside for a moment).

When you generate a random number in the range 1 to 50 - which you say
you can already do - you need to decide into which bucket to place the
number. This is actually quite easy - all you have to do is bring the
value into the range 0 to 9, which you can do by subtracting 1 and then
dividing by 5, as follows:

#include <assert.h>
int map_1_to_50_into_0_to_9(int n)
{
assert(n >= 1 && n <= 50);
return (n - 1) / 5;
}

So now all you have to do is something like:

int r = 0;
int i = 0;
unsigned long count[10] = {0};
while(i++ < 100)
{
r = get_random_number_in_range_1_to_50();
++count[map_1_to_50_into_0_to_9(r)];
}

Then you just need to print the histogram, which is easy enough. For
each element in your count array:

1) print the range. If you're looping i from 0 to 9, the range starts
with i * 5 + 1 and ends with i * 5 + 5, which should be easy enough to
print, right?
2) print some whitespace (you may find \t helpful here).
3) print a parenthesis, count, and then a close-parenthesis.
4) more whitespace.
5) a loop: for(j = 0; j < count; j++) { putchar('*'); } followed by
putchar('\n');

That should be a big enough hint to let you have a very good stab at a
proper solution of your own.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top