Trying to solve this problem

Reply

Join Date: Sep 2007
Posts: 3
Reputation: joebenjamin is an unknown quantity at this point 
Solved Threads: 0
joebenjamin joebenjamin is offline Offline
Newbie Poster

Trying to solve this problem

 
0
  #1
Sep 10th, 2007
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.....
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. int i;
  7. int d1, d2;
  8. int a[100];
  9.  
  10. for(i = 1; i <= 50; i = i + 1)
  11. a[i] = 0;
  12.  
  13. for(i = 0; i < 100; i = i + 1)
  14. {
  15. d1 = rand() % 49 + 1;
  16.  
  17. a[d1] = a[d1] + 1;
  18. }
  19.  
  20. for(i = 1; i <= 50; i = i + 1)
  21. {
  22. printf("%d: %d\t", i, a[i]);
  23. printf("*", a[i]);
  24. printf("\n");
  25. }
  26. printf("Please press ENTER to terminate.\n");
  27. getchar ();
  28. return 0;
  29. }


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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Trying to solve this problem

 
0
  #2
Sep 10th, 2007
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:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 3
Reputation: joebenjamin is an unknown quantity at this point 
Solved Threads: 0
joebenjamin joebenjamin is offline Offline
Newbie Poster

Re: Trying to solve this problem

 
0
  #3
Sep 10th, 2007
Thanks for the help I will try this out and keep you posted. Thanks a lot its appreciated greatly
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Trying to solve this problem

 
0
  #4
Sep 10th, 2007
Don't you need to seed the random number?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,381
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Trying to solve this problem

 
0
  #5
Sep 11th, 2007
Originally Posted by iamthwee View Post
Don't you need to seed the random number?
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC