473,804 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1503
On Sep 9, 7:28 pm, "joebenjami n" <guitargu...@ho tmail.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_int o_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_numb er_in_range_1_t o_50();
++count[map_1_to_50_int o_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
3952
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 developing new products (specifically - desktop products) in C#/.NET instead of the usual C++/COM that we do. Since I am not an experienced .NET developer by any definition, I don't have a good grip on the "Pro" part. The argument that I hear...
23
8952
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 the remove function that is included with <cstdio> but cannot get it to work properly. My reference book has the following....
5
7828
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 Files\IBM\SQLLIB\bin\db2.exe CREATE TOOLS CATALOG SYSTOOLS USE EXISITING DATABASE DWCTRLDB FORCE" to initialize and\or migrate the DB2 tools catalog database. The return value is "5104".
0
1619
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 the first page of the application in the target machine's program menu. I have tried using "User's Program Menu". But there is no option to open a web page (start page of the application) using the "User's Program Menu". Does anyone know a solution...
6
1291
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 program would dynamically load, place and dispose each one as required. Is this a good idea from application size, modularity, performance of slower PC's, etc. I am also considering creating all modular forms dynamically. The main program would...
7
1471
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
2611
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 monomers) which are represented as doubly pointed noncomplete binary trees. There are three different types of monomers (hence the three different constructer calling functions) the first one is the "leaves" of the tree, the second adds length...
4
2212
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 implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
6
2133
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 implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
9
2640
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 object.The object is triangulated. The rays can undergo multiple reflections, diffractions etc of the same object i.e. a ray hits a surface of the object, undergoes reflection resulting in a reflected ray which can again hit a surface, corner or edge...
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10354
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9175
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.