# 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. ## 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. 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.

3. 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

4. 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. 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. 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 ()       {

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;```

7. >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

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. >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

10. 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. Well, you have an algorithm so far, what have you tried while attempting to implement it?

-Prelude

12. 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); }```

13. >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

14. we aren't up to the fputc function yet so I can't use it.

15. ## 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.

Page 1 of 2 12 Last