944,192 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1045
  • C RSS
Sep 10th, 2007
0

Trying to solve this problem

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joebenjamin is offline Offline
3 posts
since Sep 2007
Sep 10th, 2007
0

Re: Trying to solve this problem

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].
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 10th, 2007
0

Re: Trying to solve this problem

Thanks for the help I will try this out and keep you posted. Thanks a lot its appreciated greatly
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joebenjamin is offline Offline
3 posts
since Sep 2007
Sep 10th, 2007
0

Re: Trying to solve this problem

Don't you need to seed the random number?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 11th, 2007
0

Re: Trying to solve this problem

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: matrix transpose
Next Thread in C Forum Timeline: please check for me the error and tell me~~ thx!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC