Thread: arrays and random numbers

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    arrays and random numbers

    hi, i am currently trying to get my program to use an array to store 100 random numbers (0-20) or rand % 20, then print those numbers in 5x20 row column format. However i have tried everything possible and not been able to get my program to work. I was hoping someone could give me some help on this one. thankx

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Show us what you have so far and where you think you're going wrong.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    heres what i have so far

    ok here it is
    #include<time.h>
    #include<stdlib.h>
    #define ROWS = 5
    #define COLUMNS = 20

    int main (void)

    srand(time(NULL));
    int array[ROWS][COLUMNS];
    int i, j, x
    for (i=0; i<100; i++);
    array[i]
    if (i > 0 && i < 19)
    printf("%2d", array[i]);

    if (i > 19 && i < 39)
    printf("%2d", array[i]);
    esle if (i > 39 && i < 59)
    printf("%2d", array[i]);
    else if (i > 59 && i < 79)
    printf("%2d", array[i]);
    else if (i > 79 && i < 99)
    printf("%2d", array[i]);


    that what i have, but i know its wrong

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'll add corrections as I go...

    1) Use code tags.
    Code:
    #include<time.h>
    #include<stdlib.h>
    #define ROWS = 5
    #define COLUMNS = 20
    
    /*
        Error #1 - You have no { } around
        the main function.
    */
    int main (void)
    {
    /*
        srand(time(NULL));
    
        Error #2 - Declare variables before
        calling any functions. This is C, not
        C++.
    */
    int array[ROWS][COLUMNS];
    int i, j, x
    
        /*moved to here */
        srand(time(NULL));
    
    /*
        for (i=0; i<100; i++);
    
        Error #3 - Not technicly an error, but
        not what you want. Your ; at the
        end of the for() function makes your
        function not do anything useful.
    
        Error #4 - You should really use { }
        whenever you want to create blocks
        of code. Since I don't know where 
        you plan on blocking things off, I'll
        assume.
    
        Error #5 - You shouldn't treat
        multidimensional arrays as single 
        dimension arrays. I believe technicly
        they're the same thing, but it's
        generally bad form. As such, this
        should be a nested loop.
    
        Additionally, you should use your
        define here as a loop counter
        instead of a static number.
    
        If you want to tread them as a
        single dimensional array here, do
        like so:
    */
        for( i = 0; i < (ROWS*COLUMNS); i++ )
        {
            /*
                 This line does nothing. What are you trying to do here?
            */
            array[i]
    I'll assume you're trying to assign some random value, but I'll stop here and let you see what you can some up with.

    Remember: for every { you need one and only one }

    Quzah.
    Last edited by quzah; 10-23-2002 at 09:38 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Numbers
    By punter in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2006, 06:06 PM
  2. Random numbers to stay under 200
    By sloopy in forum C Programming
    Replies: 7
    Last Post: 11-20-2005, 09:12 PM
  3. arrays of random integers & time
    By meeloff in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2004, 04:35 PM
  4. Array's Help
    By bmx4christ in forum C Programming
    Replies: 15
    Last Post: 12-08-2003, 12:40 PM
  5. Random Problem?
    By dizz in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2002, 05:00 PM