473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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

--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html

Sep 10 '07 #1
2 1481
On Sep 9, 7:28 pm, "joebenjamin" <guitargu...@hotmail.comwrote:
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.

Sep 10 '07 #2
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[i], and then a close-parenthesis.
4) more whitespace.
5) a loop: for(j = 0; j < count[i]; 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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

55
by: Alex | last post by:
Hello people, The following is not a troll but a serious request. I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to...
23
by: da Vinci | last post by:
Greetings, Onwards with the school studying. Working on a program and need to delete a file from a known location on the hard drive but cannot get anything I do to work. I have tried to use...
5
by: azgoddess1 | last post by:
During the installation I get these error messages: ***** SQL1390C The environment variable DB2Instance is not defined or is invalid An error ocured while loading the command "C:\Program...
0
by: Chetna Joshi | last post by:
We have created a Web Application in ASP.NET using VB.NET. We have also created a Web Setup Project for installing the Web application on the target machine. I need help on creating a shortcut to...
6
by: Gary Kahrau | last post by:
I am planning a very large project in vb.net and I want to make it modular. If the project needs 200-300 forms what do you think about creating a user control for each of these forms? The main...
7
by: fidlee | last post by:
i am new to learning jython... i just tried compiling a small piece of code that is given below: def fac(x) if x<=1:return 1 return x*fac(x-1) on
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.