creating a rectangle

This is a discussion on creating a rectangle within the C Programming forums, part of the General Programming Boards category; Can someone please help me understand how to create a rectangle using random variables for the width and length? I ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    8

    creating a rectangle

    Can someone please help me understand how to create a rectangle using random variables for the width and length? I am completely lost on this project. My career will obviously not be in programming.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Can you give us an idea of what you've done so far or where your stuck? People are not going to write the whole thing for you but they'll certainly help if you can be more specific.
    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Creating the rectangle is a good exercise, I'll leave that part up to you, but the random values for width and height I can help you with.
    Code:
    #include <stdlib.h> /* For rand and srand */
    #include <time.h> /* For time */
    
    .
    .
    .
    
    /* Seed the random number generator */
    srand ( (unsigned)time ( NULL ) );
    
    /*
    ** Create random numbers from 1 to MAX_WIDTH and MAX_HEIGHT. You define these.
    */
    width = rand / ( RAND_MAX / MAX_WIDTH + 1 );
    height = rand / ( RAND_MAX / MAX_HEIGHT + 1 );
    
    /* Call your function (I hope you wrote it :D) with the random numbers */
    draw_rect ( width, height );
    Toodles!

    -Prelude
    My best code is written with the delete key.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you using two random variables or just one? If you are using two then you can put your geometry knowledge to good use. Post some code and I can help.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    frankly I am a bit lost. I know how to make the row and column equal to the random numbers (between 2 and 10) and I know I am supposed to use nested, count controlled for loops to display the box but I honestly don't know how. How about a hint??

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    here goes. it's really kind of all over the place. I have spent hours on this and I know I am running in circles.
    Code:
    # include <stdio.h>
    # include <math.h>
    # include <ctype.h>
    # include <stdlib.h>
    
    int width, length
    row = 0;
    col = 0;
    
    char response, again;
    
    /* Seed the random number generator */
    srand ( (unsigned)time ( NULL ) );
    
    
    int main ()       {
    
     /*read in an validate character*/
    
       fflush (stdin);
       printf ("Please enter a character: ");
       response = getchar();
       isgraph();
    
       if (isgrpah(response) == 0);
       printf "Please enter a valid character.");
    
       for (row = 2 + (rand %8), row >=2 && x <=10)
    
       for (crow = 2 + (rand %8), row >=2 && x <=10)
    
    
       while (again == 'Y' || again == 'y');
    
       return 0;
    Code tags added by Kermi3

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but I honestly don't know how
    This is a good time to pull out a notepad (No, not that one, real paper) and sketch out exactly how you can do it, step by step. Once you figure it out that way, it's easy to tell the computer how to do the same thing. Just remember that you can only print one line of text at a time, from left to right and then top to bottom.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    thanks but I have my flow chart done, that is not the problem...the problem is that I am a terrible terrible programmer.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >thanks but I have my flow chart done
    Flow chart? Bwahahaha! I have yet to see a flowchart help the thought process. No, I meant real spacial thinking exercises. Get a handfull of change from your purse/pocket and use the coins to make the figure you want to draw on the screen. Restrict yourself to rules like I stated in my last post and unless you have a problem making rectangles with nickels on a desk you should end up with a nice figure:

    * * * * *
    * * * * *
    * * * * *
    * * * * *

    In the process you should be analyzing how you made it. Start by placing the first coin in the upper left, then the next coin to the right of the first, keep doing that until you have a line of five coins and start over from the left side. This makes a handy algorithm:
    Code:
    int i, j;
    for ( i = 0; i < height; ++i ) {
      for ( j = 0; j < width; ++j )
        printf ( "*" );
      printf ( "\n" );
    }
    Think outside the box. (Hehe )

    p.s. That little change trick works great for just about any data structure such as linked lists, binary trees, etc.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    here is the narrative of wht I am trying to do

    Prompt the user to enter a character
    Validate the character is printable
    If the character is printable continue
    Else tell the user the character is invalid and return to the beginning

    Generate a random number between 2 and 10 and make it equal the value of the row
    Generate a random number between 2 and 10 and make the line above print the value of row the same number of the second random number

    When done, ask the user if the user wants to display another box,
    If not equal to N or n, continue
    Else end the program

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Well, you have an algorithm so far, what have you tried while attempting to implement it?

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    does this look like my loops are on the right track?
    Code:
    if  (col ==2 && row == 2)
        	{ printf ("%c, %c\n", response, response);
            printf ("%c, %c\n", response, response); }
    
        	else if  (col ==2 && row == 3)
    			{printf ("%c, %c\n", response, response);
            printf ("%c, %c\n", response, response);
            printf ("%c, %c\n", response, response); }
    Code Tags added by Kermi3

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >does this look like my loops are on the right track?
    You aren't using any loops, look at my little snippet above, that is pretty much how it should look. Here is the box print for my implementation of your problem:
    Code:
    for ( i = 0; i < row; ++i ) {
      for ( j = 0; j < col; ++j )
        fputc ( print, stdout );
      fputc ( '\n', stdout );
    }
    row and col are both random numbers, print is the character input by the user.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    we aren't up to the fputc function yet so I can't use it.

  15. #15
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    dlk24 -

    I am posting this because you have not been using code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [ code ] at the beginning of your code and [ /code ] at the end, only without the spaces. More information on code tags may be found at the link on my signature.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmetation fault (reading and writing file in c)
    By tasosa in forum C Programming
    Replies: 5
    Last Post: 04-13-2009, 07:04 AM
  2. Struct Program to find Point in Rectangle
    By DMJKobam in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 09:56 PM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  4. Point passed rectangle...
    By Hunter2 in forum Game Programming
    Replies: 15
    Last Post: 10-10-2003, 10:57 AM
  5. Help With pointers in classes.
    By indigo0086 in forum C++ Programming
    Replies: 12
    Last Post: 10-10-2002, 03:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21