Thread: C program to create rectangle

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    63

    C program to create rectangle

    Hi all. I am very new to c programming and I need help creating a program.
    The description of the program is:
    Write a program that displays a hollow rectangle composed of ASCII characters, allowing the user to specify the number of rows and columns. The range for the number of rows and columns is 3 to 20. If the integers are outside of the range, they should be re-prompted until they get it right. The displayed rectangle should have + signs at the 4 corners, - signs along the top and bottom rows, and l along the 2 columns.

    I am supposed to do this using nested loops. Any help will be greatly appreciated. I have been working for this for 2 hours now and still don't really know how to do it. Thanks very much.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,656
    Hint, the inner loop will display a row, the outer loop will display all the rows, resulting in a rectangle. You sure you want to use the letter I as opposed to the character vertical bar | for the sides?

    You need to show an attempt in code.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    63
    I meant to put in the vertical bar. As I said before, I am an extreme beginner. I've been trying to just do the initial part where the program asks the user for the number of rows and columns. I can do that, but I can't get it to repeat when the person types in a value outside of the range. Here is what I have:

    insert
    Code:
    #include <stdio.h>
    Code:
     
    int main(void)
    {
        int rows, columns;
    	
    printf("Enter number of rows: ");
    	scanf("%d", &rows);
    	if ((rows >= 3) && (rows <= 20))
    		{
    printf("Enter number of columns: ");
    			scanf("%d", &columns);
    		} 
    else
    			{
    printf("Enter number of rows: ");
    			scanf("%d", &rows);
    			}
    					
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,656
    Try using while loops:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int rows, columns;
    
        while(1){
            printf("Enter number of rows: ");
            scanf("%d", &rows);
            if ((rows >= 3) && (rows <= 20))
                 break;
        }
    
        while(1){
            printf("Enter number of columns: ");
            scanf("%d", &columns);
            if ((columns >= 3) && (columns <= 20))
                 break;
        }
    
    /* ... */
    
        return(0);
    }
    Last edited by rcgldr; 07-18-2013 at 06:47 PM.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    63
    Thank you very much for the input! That it exactly what I was trying to do.

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    63
    I'm trying to do the next part of the code and I don't really understand what I am doing. I am trying to make the first and last characters + signs and the rest - signs and I don't know how to do this. Here is what I have:

    Code:
    #include <stdio.h>
    Code:
         
    int main(void)
    {
        int rows, columns;
     
        while(1)
        {
    printf("Enter number of rows: ");
            scanf("%d", &rows);
            if ((rows < 3) || (rows > 20))
                continue;
    printf("Enter number of columns: ");
                scanf("%d", &columns);
            if ((columns < 3) || (columns > 20))
                continue;
            break;
        }
        for (rows = 1)
    printf("+")
        for (rows = 1; rows++)
    printf("-")
        
    return0;
    }

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,656
    You need two more variables, such as i and j, or perhaps row and column to keep track of which row and column the program is working on. Then you'll need to make loop inside a loop. The inner loop works on columns and the outer loop works on rows.

  8. #8
    Registered User
    Join Date
    Jul 2013
    Posts
    63
    That's what I am trying right now and it isn't really working for me. I don't really understand how to do the loops. I'll show you what I have but I know it is really wrong.

    Code:
    #include <stdio.h>
     	
    int main(void)
    {
        int rows, columns, x, y;
     
        while(1)
    	{
    printf("Enter number of rows: ");
            scanf("%d", &rows);
            if ((rows < 3) || (rows > 20))
                continue;
    printf("Enter number of columns: ");
                scanf("%d", &columns);
            if ((columns < 3) || (columns > 20))
                continue;
            break;
        }
    	
    	for (x = 1 && x = rows)
    printf("+")
    	for (x = 1; x < rows; rows++)
    printf("-")
    	for (y = 1 && y = columns)
    printf("|")
    	
    return0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,656
    To make your code look nicer, indent your printf() statements so they line up with the rest of the code.

    The for loops could look like this:

    Code:
    /* ... */
        for(x = 1; x <= rows; x++){
            for(y = 1; y <= columns; y++){
                if(y == 1 || y = ... ){
    /* ... */
    Last edited by rcgldr; 07-18-2013 at 08:10 PM.

  10. #10
    Registered User
    Join Date
    Jul 2013
    Posts
    63
    Thank you again for all of your help. So I've been trying to do the loops and here is what I have. I know it isn't correct because the program doesn't compile. I still don't understand how to put in the + signs as well since I don't know how to identify the last row. And about the indenting, I am doing it correctly in my text editor, I think the website is messing it up.
    Code:
    {
    	for(x = 0; x < rows; x++)
    printf("-")
            for(y = 0; y < columns; y++)
    printf("\n")
                if(y == 0 || y = columns)
    printf("|")
    }

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    Type all the code in one.All the code you have right now.

  12. #12
    Registered User
    Join Date
    Jul 2013
    Posts
    63
    What does that mean?

  13. #13
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Break it down into smaller parts, think about each of these smaller parts of the problem and get each to work before trying to put it all together.

    To start lets get the top and bottom row to work.

    Is the top and bottom row the same?
    As it is we only need one code block to create it and call this code once for the first/top row and again on the bottom / last row.

    The for loop needs to print a character for each column.

    Code:
     for (y = 0; y < cols; y++)
    We need a '+' at each corner and a '-' for each column in between.

    This is an 'IF'

    Code:
    if (y == left corner OR y == right corner) 
    	print '+'
    else 
    	print '-'
    So what values are the corners at? (remember we are using zero based arrays)

    Get this to work and then we can move on to the rows.

    With the rows, for the top and bottom rows we need to call the column code (above) otherwise we need to print a wall ('I') at the edges.

    We need a FOR loop that runs for each row (which you already have).

    Then again an 'IF' to decide what to print

    Code:
    if (x == top OR x == bottom)
    	call the column loop
    else
    	print 'I'
    EDIT: This code will not work for the wall rows (not top and bottom) It will not print the right hand 'wall' only the left wall.

    We need another column printing loop to print the right wall.

    This loop is similar to the top and bottom row

    Code:
    //wall print
    if (y == left corner OR y == right corner) 
    	print 'I'
    else 
    	print ' ' (space character)
    You will need to work out which column loop to call for each ROW.
    Last edited by novacain; 07-18-2013 at 09:28 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  14. #14
    Registered User
    Join Date
    Jul 2013
    Posts
    63
    So I tried putting what you said into my code and here it is. I don't understand how to call a few things. Can you please look it over.
    Code:
    #include <stdio.h>
         
    int main(void)
    {
       int rows, columns, x, y;
     
       while(1)
        {
            printf("Enter number of rows: ");
            scanf("%d", &rows);
            if ((rows < 3) || (rows > 20))
                continue;
            printf("Enter number of columns: ");
                scanf("%d", &columns);
            if ((columns < 3) || (columns > 20))
                continue;
            break;
         }
            for (y = 0; y < columns; y++)
            {
                    if (y = left corner || y == right corner) 
                    printf("+");
                    else
                     printf("-");
            }
            for(x = 0; x < rows; x++)
            {
                if (x == top || x == bottom)
                call the column loop
                else
                printf("|")
        
    return0;
    }

  15. #15
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    What i meant,show the code you had up until now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object-oriented Rectangle program
    By LearnOnTheFly in forum C++ Programming
    Replies: 24
    Last Post: 02-18-2013, 10:38 PM
  2. Create a GUI for this program?
    By sellers04 in forum C Programming
    Replies: 4
    Last Post: 07-18-2011, 06:52 PM
  3. Program to create shapes in PPM
    By cpsestudent in forum C Programming
    Replies: 3
    Last Post: 02-08-2011, 02:11 AM
  4. Struct Program to find Point in Rectangle
    By DMJKobam in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 08:56 PM
  5. Trying to create an exp(x) program
    By toadkiwi in forum C Programming
    Replies: 12
    Last Post: 02-29-2008, 05:29 AM