| | |
Trying to solve this problem
![]() |
•
•
Join Date: Sep 2007
Posts: 3
Reputation:
Solved Threads: 0
Here is my issue if anyone can help.
I am trying to write a program that will generate 100 random numbers between 1 and 50. With these numbers, I want to 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. Print out the results as a histogram. I am hoping to get results such as:
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) ***********
Here is my attempt so far, not so good.....
I hope someone can help, even if there is a tutorial or anything.
I am trying to write a program that will generate 100 random numbers between 1 and 50. With these numbers, I want to 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. Print out the results as a histogram. I am hoping to get results such as:
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) ***********
Here is my attempt so far, not so good.....
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main() { int i; int d1, d2; int a[100]; for(i = 1; i <= 50; i = i + 1) a[i] = 0; for(i = 0; i < 100; i = i + 1) { d1 = rand() % 49 + 1; a[d1] = a[d1] + 1; } for(i = 1; i <= 50; i = i + 1) { printf("%d: %d\t", i, a[i]); printf("*", a[i]); printf("\n"); } printf("Please press ENTER to terminate.\n"); getchar (); return 0; }
I hope someone can help, even if there is a tutorial or anything.
Last edited by Ancient Dragon; Sep 10th, 2007 at 5:13 am. Reason: add line numbers to code tags
What you need is an array of 10 integers to contain the count of each interval. For example a[0] is the number of random numbers that fall between 1 and 5, a[1] = 6-10, etc. After generating a random number at line 15 use a series of if statements to determine which array element to increment, such as if d1 >0 and d1 < 6 then increment a[0].
line 8: array a should be 10, not 100.
line 10: arrays are always numbered from 0 to the number of elements assigned to the array. So line 10 should look like this:
lines 20-25: you need two loops, not one. The outer loop counts from 0 to 10, and the inner loop from 0 to the value of a[i].
line 8: array a should be 10, not 100.
line 10: arrays are always numbered from 0 to the number of elements assigned to the array. So line 10 should look like this:
C Syntax (Toggle Plain Text)
for(i = 0; i < 10; i = i + 1)
lines 20-25: you need two loops, not one. The outer loop counts from 0 to 10, and the inner loop from 0 to the value of a[i].
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Not absolutely required -- if you don't see it then the program will generate the same set of random numbers every time it is run. Seeding only changes that behavior to generate a different set each time the program is run, assuming it uses a different seed such as the return value from time() function.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- plzz solve my problem (C++)
- Poor sap desperate to solve Shared object problem. Please help if you can. (Graphics and Multimedia)
- tell me how to solve "counting Problem" in a BTree ? (C++)
- can solve this problem? (C)
- please anybody help to solve keydown event for enter button problem (HTML and CSS)
- Please anybody help to solve error "class not registered" problem in vs 2003 (ASP.NET)
- Anyway can help me to solve this problem??? (C++)
- CDBException can't solve problem (ASP.NET)
- please help been at my pc for hours still cannot solve problem (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: matrix transpose
- Next Thread: please check for me the error and tell me~~ thx!
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






