Anyone have any ideas on this one.....

Discussion in 'C Programming' started by joebenjamin, Sep 10, 2007.

  1. joebenjamin

    joebenjamin Guest

    I am trying to write a program that will generate 100 random numbers
    between 1 and 50. Using 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. Finally,I would like to print out the results as a
    histogram. I have written it out and would think it would 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) ***********

    I am trying to learn how to do this and I am stuck. Any suggestions would
    help. Even some possible tutorials to chek out would help.
     
    joebenjamin, Sep 10, 2007
    #1
    1. Advertisements

  2. To generate random number integer from set [1, 50] you can use: "number
    = rand() % 50 + 1;" (remember to initialise pseudo-random number
    generator with for instance srand(time(0))).

    To get bucket number you can use "bucket = (number - 1) / 5;" (so in
    fact it is better to generate random integer from set [0, 49] using
    "number = rand() % 50;" and then calculate bucket number using "bucket =
    number / 5;").

    Then you increment given bucket, ie.: "buckets[bucket]++". Buckets have
    to be zeroed first of course. And when results are ready you print the
    results using two nested for loops.
     
    Michal Nazarewicz, Sep 10, 2007
    #2
    1. Advertisements

  3. Michal Nazarewicz said:
    Better: int number = 50 * (rand() / (RAND_MAX + 1.0));
    If he wants repeatability, he'd be better off using a constant. If he
    wants unpredictability (as far as is practical), the following function
    (based on code written by Lawrence Kirby) does a better job of
    utilising all the available entropy.

    #include <stddef.h>
    #include <time.h>
    #include <limits.h>

    /* Usage: srand (time_seed ()); */

    /* Choose and return an initial random seed based on the current time.
    Based on code by Lawrence Kirby <>. */
    unsigned
    time_seed (void)
    {
    time_t timeval;
    unsigned char *ptr;
    unsigned seed;
    size_t i;

    timeval = time (NULL);
    ptr = (unsigned char *) &timeval;

    seed = 0;
    for (i = 0; i < sizeof timeval; i++)
    seed = seed * (UCHAR_MAX + 2U) + ptr;

    return seed;
    }
     
    Richard Heathfield, Sep 10, 2007
    #3
  4. joebenjamin

    tolkien Guest

    Try this:

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>

    int main(void)
    {
    int matrix[10];
    int i;

    int x;

    srand(time(NULL));
    for(i=0;i<10;i++)
    matrix=0;

    for(i=0;i<100;i++){
    x=rand()%50 + 1;

    if(x<6)
    matrix[0]++;
    else if(x<11)
    matrix[1]++;
    else if(x<16)
    matrix[2]++;
    else if(x<21)
    matrix[3]++;
    else if(x<26)
    matrix[4]++;
    else if(x<31)
    matrix[5]++;
    else if(x<36)
    matrix[6]++;
    else if(x<41)
    matrix[7]++;
    else if(x<46)
    matrix[8]++;
    else
    matrix[9]++;
    }


    printf("1-5 \t\t ( %3d ) ",matrix[0]);
    for(i=0;i<matrix[0];i++)
    printf("*");
    printf("\n");

    printf("6-10 \t\t ( %3d ) ",matrix[1]);
    for(i=0;i<matrix[1];i++)
    printf("*");
    printf("\n");

    return 0;
    }
     
    tolkien, Sep 10, 2007
    #4
  5. joebenjamin

    joebenjamin Guest

    Thanks Tolkien, that did help, and workd great. I have been working on
    that
    problem for about 2 weeks now... Much appreciated !!!!
     
    joebenjamin, Sep 10, 2007
    #5
  6. <snip - program>

    Hey man, thanks for doing my homework.
    Will you do my next assignment as well, so I can get an A+?
     
    Bart van Ingen Schenau, Sep 10, 2007
    #6
  7. Better still: read question 13.16 in the comp.lang.c FAQ,
    <http://www.c-faq.com/>.
     
    Keith Thompson, Sep 10, 2007
    #7
  8. Bart van Ingen Schenau said:
    You'd give an A+ for that? I'd give a C, tops.
     
    Richard Heathfield, Sep 11, 2007
    #8
  9. I am not that familiar with the letter-grade system.
    On a scale of 1 to 10 (10 being the highest score), I would give the
    program, if it was turned in as posted, at most a 5.
    For starters, the program does not meet the stated requirements.

    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Sep 11, 2007
    #9
  10. <OT>

    The usual letter-grade system is A, B, C, D, F (E is skipped).
    A is the best, F is failure. A '+' or '-' may be appended to the
    letter grade; B+ is better than B, but not quite as good as A-.
    F+ and F- are rarely given; failure is failure.

    So your '5' is probably equivalent to Richard's 'C'.
     
    Keith Thompson, Sep 11, 2007
    #10
  11. Usually, A = 80%, B = 75%, C = 65%, D = 55%, '+' means
    higher towards the next best range, '-' means lower towards
    the lesser range. So 5 in 1-10 scale would be D-
    which is as low as you can go and still pass.
     
    Walter Roberson, Sep 11, 2007
    #11
  12. joebenjamin

    Default User Guest


    That's not typical in the US. Normally:

    A: 90%-100%
    B: 80%-89%
    C: 70%-79%
    D: 60%-69%
    F: 0%-59%



    Brian
     
    Default User, Sep 11, 2007
    #12
  13. joebenjamin

    user923005 Guest

     
    user923005, Sep 11, 2007
    #13
  14. joebenjamin

    Richard Bos Guest

    In the 1-10 scale I'm used to - and which, I can only presume since he
    uses a news server which belongs to my own ISP and therefore is well
    acquainted with my country, Bart grew up with as well - 5 is as high as
    you can go and still fail. A 6-, or possibly a 5-and-a-half, is the
    lowest pass grade.

    Richard
     
    Richard Bos, Sep 12, 2007
    #14
  15. Any decent scale for grading C programming assignment should be 0 based,
    don't you think ?
    Yet another example of the culturally engraved refusal to cope with the
    decimal system that plagues the USA.
    Just for the fun of it, you should expand on how these grades convert to GPA
    points...
     
    Charlie Gordon, Sep 12, 2007
    #15
  16. joebenjamin

    Al Balmer Guest

     
    Al Balmer, Sep 12, 2007
    #16
  17. Al Balmer said:
    Does this body look dead to you? No? Then no.
     
    Richard Heathfield, Sep 12, 2007
    #17
  18. Well, you could think of the scale as running from 0 to 10. The first
    point is awarded for turning in a paper with your name on top. :)

    I should have made it more clear in my previous post.
    In the grading system in the Netherlands, the grades 1 (or 0) to 5
    (inclusive) are all fail grades, so they would all correspond to an F.
    The grades 6 to 10 are all pass grades, where 10 means a perfect score
    (100%).

    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Sep 12, 2007
    #18
  19. joebenjamin

    Al Balmer Guest

    I do wish that the US would follow the UK (and most of the rest of the
    world) and go metric. What's the name for the "4.0" grading system
    higher ed uses here?
     
    Al Balmer, Sep 12, 2007
    #19
  20. Al Balmer said:

    And I wish we would go back to Imperial weights and measures. Life in
    Britain is much more confusing than it needs to be. The last time I
    went to the grocer, I had to ask for 0.68038856 kilograms of carrots,
    2.2679619 kilograms of spuds, and 1.8927059 pints of milk.

    That's the trouble with these Europeans. Give them 25.4 millimetres and
    they'll take 1.1429982 metres.
     
    Richard Heathfield, Sep 12, 2007
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.