Thread: How do i get this program to print random numbers?

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    12

    How do i get this program to print random numbers?

    okay, so this is the program so far, it can print one number that I set it to. how can I edit this so it will print a random number which will be printed on the front of the two squares??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define ROW 9
    #define COL 9
     
    char image[2][ROW][COL];
    //       COL
    //     +----+
    //     |    |    ROW
    //     |    |
    //     +----+
    //
    // 5x5 X and O images
    char shapes[2][5][6] = { { "*   *"
                             , " * * "
                             , "  *  "
                             , " * * "
                             , "*   *" }
                           , { " *** "
                             , "*   *"
                             , "*   *"
                             , "*   *"
                             , " *** " }
                           };
    void rectange()
    {
        for(int k = 0; k < 2; k++)
        for(int r = 0; r < ROW; r++)
            for(int c = 0; c < COL; c++)
            {
                image[k][r][c] = ' ';
                int top    = (r == 0);
                int bottom = (r == ROW - 1);
                int left   = (c == 0);
                int right  = (c == COL - 1);
                // corner
                if ((top || bottom) && (left || right))
                    image[k][r][c] = '+';
                else
                // top or bottom
                if (top || bottom) image[k][r][c] = '-';
                else
                // left or right
                if (left || right) image[k][r][c] = '|';
            }
    }
    void overlay_X_or_O()
    {
        for(int k = 0; k < 2; k++)
        {
            int shape = rand() % 2;
            for(int r = 0; r < 5; r++)
                for(int c = 0; c < 5; c++)
                    image[k][r + 2][c + 2] = shapes[shape][r][c];
        }
    }
    int main()
    {
        srand(time(NULL)); // initialise random number generator
        rectange(); // initialise the image with a rectange
        overlay_X_or_O(); // overlay image of X or O on top
        // print the image
        char roll_num = '2,3';
        image[0][4][4] = roll_num;
        // print the image
        for(int r = 0; r < ROW; r++)
        {
            for(int k = 0; k < 2; k++)
            {
                for(int c = 0; c < COL; c++)
                    printf("%c", image[k][r][c]);
                printf("  ");
            }
            printf("\n");
        }
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    ok take that code I gave you, and think about it
    Code:
    int main()
    {
        srand(time(NULL)); // initialise random number generator
        rectange(); // initialise the image with a rectange
        overlay_X_or_O(); // overlay image of X or O on top
      //  int num = (rand() % 6 +1);
        
    //    sprintf(roll_num, "%d", (rand() % 6 +1));
       // printf("RANDOM = %s", roll_num);
        //board[i_coord][j_coord]= x_mark;
     //   char roll_num = '3';
        //image[1][4][4] = roll_num;
    
    // you are going to need to remember, or think
    //about your code here and how you created your array
    //for X and O to print out, and how you're
    //going to get the both of them to have 
    // a number printed in each of them
    //Think about it. it is your code,
    //3D arrays
    //I've only read about them, and if
    //I can figure it out, so can you.
    //because you wrote it, 
    //that is only if you want both of them 
    //to show a number, of course. 
    
      char numbers[6] = {'1','2','3','4','5','6'};
       image[1][4][4] = numbers[(rand() % 6)];
    
        // print the image
        for(int r = 0; r < ROW; r++)
        {
            for(int k = 0; k < 2; k++)
            {
                for(int c = 0; c < COL; c++)
                    printf("%c", image[k][r][c]);
                printf("  ");
            }
            printf("\n");
        }
        return 0;
    }
    You need to print a char 1 - 6?? or whatever , but you need it to be random and you need to slip it in at run time. Rand give int .. How could you do to get a char 1 thru x to be random using rand() that returns an int and an char array that is filled with the numbers 1 thru x then call rand() to return to get the the element number to get the the char at a random 1 thru x the assign it to your X O char array?


    sorry I didn't catch your post to me in your other post on this.
    Last edited by userxbw; 11-29-2017 at 06:43 PM.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    right okay got you! I've sorted that thank you!, how do I do it so the random number appears in both squares? because at the moment it only appears in one of them
    also that's fine you have been very helpful!

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Santac View Post
    right okay got you! I've sorted that thank you!, how do I do it so the random number appears in both squares? because at the moment it only appears in one of them
    also that's fine you have been very helpful!
    come on, use that brain God gave you, think about it, look closely at how it is getting that one in one of your print outs. You wrote this 3D array what part of it separates the two ?
    Last edited by userxbw; 11-29-2017 at 06:49 PM.

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    okay I've tried and tried but no success. it just wont print a random number on the other square??

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Santac View Post
    okay I've tried and tried but no success. it just wont print a random number on the other square??
    You cannot see how it is actually putting that one number into your 3d array?

    did you write that 3D array? you got 3 element "holders"
    Code:
    image[2][ROW][COL]
    you wrote a bunch of complected code to get that to overlay into another 3d array. How did you figure that out? Now you're telling me you cannot figure out how to add the other number into the other part of the 3d array? what is the first [ ] for? what is the second [ ] for, what is the third [ ] for in a 3d array?

    Because it can be done
    Code:
    +-------+  +-------+  
    |       |  |       |  
    |  ***  |  | *   * |  
    | *   * |  |  * *  |  
    | * 5 * |  |   3   |  
    | *   * |  |  * *  |  
    |  ***  |  | *   * |  
    |       |  |       |  
    +-------+  +-------+

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    How do i get this program to print random numbers?-dice2-jpg

    okay ive done it! great thank you!
    but when ever the number 1 comes up it shifts over to one side?
    Attached Images Attached Images How do i get this program to print random numbers?-dice-code-jpg 

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Santac View Post
    How do i get this program to print random numbers?-dice2-jpg

    okay ive done it! great thank you!
    but when ever the number 1 comes up it shifts over to one side?
    yep looks like you got some more work ahead of you to make it do what you want it to do. maybe re think how you're implementing it even. I do not know, because I do not know what you're trying to ultimately accomplish. nor do I even know how to deal with a 3d array. per se'

    EDIT:
    Code:
    $ ./x_and_o_3darray
    +-------+  +-------+  
    |       |  |       |  
    | *   * |  |  ***  |  
    |  * *  |  | *   * |  
    |   1   |  | * 4 * |  
    |  * *  |  | *   * |  
    | *   * |  |  ***  |  
    |       |  |       |  
    +-------+  +-------+  
    
    $ ./x_and_o_3darray
    +-------+  +-------+  
    |       |  |       |  
    |  ***  |  |  ***  |  
    | *   * |  | *   * |  
    | * 6 * |  | * 1 * |  
    | *   * |  | *   * |  
    |  ***  |  |  ***  |  
    |       |  |       |  
    +-------+  +-------+
    what ?? show me your code please
    Last edited by userxbw; 12-01-2017 at 02:23 PM.

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    this is the code. how have you done that so the 1 doesn't shift everything??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define ROW 9
    #define COL 9
      
    char image[1][ROW][COL];
    //       COL
    //     +----+
    //     |    |    ROW
    //     |    |
    //     +----+
    //
    // 5x5 O and O images
    char shapes[2][5][6] = { { "*****"
                             , "*   *"
                             , "*   *"
                             , "*   *" 
                             , "*****" }
                           , { "*****"
                             , "*   *"
                             , "*   *"
                             , "*   *"
                             , "*****" }
                           };
    void rectange()
    {
        for(int k = 0; k < 2; k++)
        for(int r = 0; r < ROW; r++)
            for(int c = 0; c < COL; c++)
            {
                image[k][r][c] = ' ';
                int top    = (r == 0);
                int bottom = (r == ROW - 1);
                int left   = (c == 0);
                int right  = (c == COL - 1);
                // corner
                if ((top || bottom) && (left || right))
                    image[k][r][c] = '+';
                else
                // top or bottom
                if (top || bottom) image[k][r][c] = '-';
                else
                // left or right
                if (left || right) image[k][r][c] = '|';
            }
    }
    void overlay_X_or_O()
    {
        for(int k = 0; k < 2; k++)
        {
            int shape = rand() % 2;
            for(int r = 0; r < 5; r++)
                for(int c = 0; c < 5; c++)
                    image[k][r + 2][c + 2] = shapes[shape][r][c];
        }
    }
    int main()
    {
        srand(time(NULL)); // initialise random number generator
        rectange(); // initialise the image with a rectange
        overlay_X_or_O(); // overlay image of O or O on top
      //  int num = (rand() % 6 +1);
         
    //    sprintf(roll_num, "%d", (rand() % 6 +1));
       // printf("RANDOM = %s", roll_num);
        //board[i_coord][j_coord]= O_mark;
        //image[1][4][4] = roll_num;
        char numers[6] = {'1','2','3','4','5','6'};
        image[0][4][4] = numers[(rand() % 6 + 1)];
        image[1][4][4] = numers[(rand() % 6 + 1)];
        // print the image
        for(int r = 0; r < ROW; r++)
        {
            for(int k = 0; k < 2; k++)
            {
                for(int c = 0; c < COL; c++)
                    printf("%c", image[k][r][c]);
                printf("  ");
            }
            printf("\n");
        }
        return 0;
    }

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    741
    The numers array has valid indexes between 0 and 5, but (rand() % 6 + 1) returns a value between 1 and 6.

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    look at post 10 and then read this, and you should then know why I wrote it like that
    Code:
      char numbers[6] = {'1','2','3','4','5','6'};
       image[1][4][4] = numbers[(rand() % 6)];
    at least you got the other part right (finally) you just forgot you're now dealing with the element numbers 0 - 5 and no longer the numbers in the array 1 - 6. plus I redid it and added the b to numbers.
    Last edited by userxbw; 12-01-2017 at 02:58 PM.

  12. #12
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    done it! thank you!
    How do i get this program to print random numbers?-dice-eee-jpg

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    yeay! now you can move on to more complex coding.

  14. #14
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    not yet! I still need to make it interact with the user. haha

  15. #15
    Banned
    Join Date
    Aug 2017
    Posts
    861
    printing two O's or two X's at the same time looks problematic already.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to print the numbers in ascending order
    By abhi143 in forum C Programming
    Replies: 5
    Last Post: 10-18-2017, 04:36 AM
  2. program prints random numbers not the sum
    By tlxxxsracer in forum C Programming
    Replies: 5
    Last Post: 11-08-2015, 10:08 PM
  3. Replies: 4
    Last Post: 01-21-2015, 12:14 PM
  4. Replies: 1
    Last Post: 10-30-2011, 10:23 PM
  5. Program to print 3 numbers in ascending order
    By jadedreality in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 07:32 PM

Tags for this Thread