Thread: Drawing a Box with Coordinates

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    13

    Drawing a Box with Coordinates

    Hello, I am a beginning student in a C programming class. I am having trouble understanding an assignment my instructor issued but has not lectured over it yet (it is due before she lectures over it).

    The assignment is:

    1) Write a program that asks the user for a a single character and two XY coordinates. The two X and two Y values should all be integers between 0 and 50. The character should be a printable ASCII character with values between and including ' !' (ascii value 33) and '~' (ascii value 126).

    2) Your program should then draw a rectangle made up of the user selected character where the upper left corner is at X1; Y 1 and the lower right corner is at X2; Y2. Be sure to print the appropriate number of blank lines (having spaces in the blank rows is OK) in the beginning and pad each row of your rectangle with X1 leading spaces.

    The Output is supposed to be similar to this:

    (X1,Y1) = (0,0) , (X2,Y2) = (4,4), the character = ^

    ^^^^
    ^^^^
    ^^^^
    ^^^^

    What I am having trouble understanding is printing between certain ASCII values (ASCII has never been discussed in class).

    Another thing I am having trouble with is the main part of the assignment. From what we are currently discussing is loops and the assignment is covering nested loops. My code looks similar to this:

    Code:
    #include <stdio.h>
    
    int main (void) {
    
    int X1, Y1, X2, Y2;
    char cRec;
    
    printf("Enter a character: ");
    scanf("%c", &cRec);
    printf("\n");
    
    printf("Enter point 1 (close to origin): ");
    scanf("%d,%d", &X1, &Y1);
    printf("\n");
    
    printf("Enter point 2 (farthest from origin): ");
    scanf("%d,%d", &X2, &Y2);
    printf("\n");
    
    for (X1=0; X1 <= X2; ++X1) {
         for (Y1=0; Y1 <= Y2; ++Y1) {
             printf("%c", cRec);
        }
    }
    return 0;
    }
    My thinking on the assignment is that you want the X1 coordinate to increase to the value of X2 (same for Y1 and Y2). Is this thinking wrong?
    Last edited by ZeroBoyd; 09-23-2014 at 09:27 PM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    It's a bit more complicated than that, but not difficult.

    You also want to use the values input for x1 and y1.

    So the rectangle has not only a size specified by the input,
    but also a specified location on the screen.

    That's the purpose of this:

    "Be sure to print the appropriate number of blank lines (having spaces in the blank rows is OK) in the beginning and pad each row of your rectangle with X1 leading spaces."

    The blank lines and spaces position the recangle.

    -
    Last edited by megafiddle; 09-23-2014 at 10:44 PM.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    13
    Quote Originally Posted by megafiddle View Post
    It's a bit more complicated than that, but not difficult.

    You also want to input values for x1 and y1.


    -
    Isn't that what the printf and scanf functions were for? Or do I define two new terms, x1 and x2?

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    My mistake. Should have been "you want to use the values". It's corrected above.

    You are using X1 (and Y1) as loop counters. You need a separate counter,
    which ranges from X1 to X2:

    for(x = X1; x <=X2; x++)

    -

  5. #5
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by ZeroBoyd View Post
    Isn't that what the printf and scanf functions were for? Or do I define two new terms, x1 and x2?
    If (X1,Y1) is considered the upper left corner of the Rectangle, then you can consider Y1 the amount of '\n' characters you need to get to the correct y coordinate. The y axis usually starts at 0 at the top of the screen, and gets larger as you go down (think of how you read text). The X1 value is the number of ' ' characters to get to the correct x coordinate.

    After printing each row, you will then need to print X1 number of ' ' characters before starting the new row. So I would recommend one loop before the rest, that prints Y1 number of '\n' characters, then a nested that prints X1 number of spaces before printing charaters.

    Think of it like this:

    X1 is x coordinate
    Y1 is y coordinate (starting at top of screen)
    X2 is X1 plus width
    Y2 is Y1 plus height

    Example:

    X1 is 2,
    Y1 is 2 (starting from --- line),
    X2 is 6 (width of 4)
    Y2 is 4 (height of 2)

    ----------------------------------------------


    ****
    ****

    ----------------------------------------------

    (Please pretend the rectangle is 2 spaces from the left edge of my post, as it's not showing properly)



    Page of ascii character values : Table of ASCII Characters
    Last edited by Alpo; 09-23-2014 at 11:26 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    13
    Thank you for your help. My current code is:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void) {
       char cRec;
       int X1, Y1; //Point A
       int X2, Y2; //Point B
       int i;         //Height
       int j;         //Width
    
       printf("Enter a character: ");
       scanf("%c", &cRec);
       printf("\n");
     
       printf("Enter Point One (closest to origin): ");
       scanf("(%d,%d)", &X1, &Y1);
       printf("\n");
     
       printf("Enter Point Two (farthest from origin): ");
       scanf("(%d,%d)", &X2, &Y2);
       printf("\n");
    
       for (j = X1; j <= X2; j++) {
          printf("%c\n", letterRectangle);
          for (i = Y1; i <= Y2; i++) {
             printf("%c", letterRectangle);
          }
       }
       return 0;
    }
    From this, by entering in "*" for the character, "(0,0)" for Point 1 and "(4,4)" for Point 2 my box looks like:

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

    Could someone explain why this happens?
    Last edited by ZeroBoyd; 09-25-2014 at 11:28 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Looks like you posted the wrong code.
    That code neither compiles nor works:

    Code:
       printf("Enter Point One (closest to origin): ");
       scanf("(%d,%d)", &X1, &Y1);                     <<<<<  extra set of parentheses
       printf("\n");
     
       printf("Enter Point Two (farthest from origin): ");
       scanf("(%d,%d)", &X2, &Y2);                     <<<<<  extra set of parentheses
       printf("\n");
    
       for (j = X1; j <= X2; j++) {
          printf("%c\n", letterRectangle);                <<<<<  should be cRec
          for (i = Y1; i <= Y2; i++) {
             printf("%c", letterRectangle);               <<<<<  should be cRec
    Also, basically, you want something like this:
    Code:
        for(i = 0; i < Y1; i++)
        {
            code here to print blank lines before top of rectangle
        }
        
        for(i = Y1; i <= Y2; i++)
        {  
            for(j = 0; j < X1; j++)
            {
                code to print spaces before left of rectangle 
            }
    
            for(j = X1; j <= X2; j++)
            {
                code to print lines of rectangle (characters)
            }
        }
    -
    Last edited by megafiddle; 09-25-2014 at 12:45 PM.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by ZeroBoyd View Post
    From this, by entering in "*" for the character, "(0,0)" for Point 1 and "(4,4)" for Point 2 my box looks like:

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

    Could someone explain why this happens?
    Code:
       for (j = X1; j <= X2; j++) {
          printf("%c\n", letterRectangle);
          for (i = Y1; i <= Y2; i++) {
             printf("%c", letterRectangle);
          }
       }
    First, you have x and y swapped. Because the rectangle is printed row by row,
    and not column by column, you want y in the outer loop. (y represents rows).

    Anyway, what's happening, is that you have an extra character being printed:

    Code:
        printf("%c\n", letterRectangle);
    You want only a newline in that printf statement.
    Also, the newline should be just after the inner loop, not before.

    Notice that a rectangle defined by (0, 0) and (4, 4) will be 5 by 5 characters.

    Your output should look like this for (0, 0) and (4, 4):

    Code:
    *****
    *****
    *****
    *****
    *****
    and like this for (10, 10) and (14, 14):

    Code:
    
    
    
    
    
    
    
    
              *****
              *****
              *****
              *****
              *****
    -
    Last edited by megafiddle; 09-25-2014 at 01:40 PM.

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    13
    Thank you. So the main problem was the extra character in the new line statement. As well as not changing my char variable. This brings a different question. The code I generate now, using (0,0) and (4,4), generates a 5x5 box, but it is supposed to generate a 4x4 box.
    The code I have now is:
    Code:
    for (j = Y1; j <= Y2; j++) {
      printf("\n");
      for (i = X1; i <= X2; i++) {
       printf("%c", letterRectangle);
      }
     }
    And the code for a 4x4 Box is:
    Code:
    for (j = 1; j <= 4; j++) {
      printf("\n");
      for (i = 1; i <= 4; i++) {
       printf("%c", letterRectangle);
      }
     }
    So, the code needs to represent the difference between the two points.
    So when I define diffX = X2 -X1 and diffY = Y2 - Y1 as integers and use the following code:
    Code:
    for (j = 1; j <= diffY; j++) {
      printf("\n");
      for (i = 1; i <= diffX; i++) {
       printf("%c", letterRectangle);
      }
     }
    The code continuously generates a massive wall (paragraph) going infinte with the character inputed. Help?

    EDIT: Still looking into it, Values for X1 though Y2 are not being registered for DiffX and DiffY.
    Last edited by ZeroBoyd; 09-25-2014 at 02:48 PM.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by ZeroBoyd View Post
    Thank you. So the main problem was the extra character in the new line statement. As well as not changing my char variable. This brings a different question. The code I generate now, using (0,0) and (4,4), generates a 5x5 box, but it is supposed to generate a 4x4 box.
    If (0, 0) is part of the rectangle, and (4, 4) is also part of the rectangle,
    then it will be 5 by 5.

    The first row represents:

    (0, 0) (1, 0) (2, 0) (3, 0) (4, 0)

    and the last row represents

    (0, 4) (1, 4) (2, 4) (3, 4) (4, 4)

    If you really want 4 by 4, just change the <= to < in the first code, like this:

    Code:
    for (j = Y1; j < Y2; j++) {
      printf("\n");
      for (i = X1; i < X2; i++) {
       printf("%c", letterRectangle);
      }
     }
    Quote Originally Posted by ZeroBoyd View Post
    The code continuously generates a massive wall (paragraph) going infinte with the character inputed. Help?

    EDIT: Still looking into it, Values for X1 though Y2 are not being registered for DiffX and DiffY.
    Are you maybe trying to assign values to DiffX and DiffY before you read values into X1, X2, Y1, Y2?

    -

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    13
    Thank you for your patience megafiddle. What was happening was my compiler was not downloading the new code correctly. After restarting my computer it generated a box correctly. Apparantly compilers don't like it when you have them output massive walls of text and quit on you.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Glad you are making progress.

    Program crashes can do unpredictable things.
    I always restart the PC unless I know exactly why the program crashed.

    As I mentioned above, it could be done like this:
    Code:
        for(i = 0; i < Y1; i++)
        {
            code here to print blank lines before top of rectangle
        }
        
        for(i = Y1; i <= Y2; i++)
        {  
            for(j = 0; j < X1; j++)
            {
                code to print spaces before left of rectangle 
            }
    
            for(j = X1; j <= X2; j++)
            {
                code to print lines of rectangle
            }
        }
    Normally, you would just print newlines in the very first loop above to print the blank lines:
    Code:
        for(i = 0; i < Y1; i++)
        {
            printf("\n");
        }
    But this part is interesting:

    "Be sure to print the appropriate number of blank lines (having spaces in the blank rows is OK) in the beginning..."

    In the method above, there would be no need to print spaces in the blank rows.

    If you can use if statements (or if else statements), then there is another way
    to do it, which would have spaces in the blank rows:

    Code:
        for(i = 0; i <= Y2; i++)
        {  
            for(j = 0; j < X2; j++)
            {
                if i and j are within the rectangle, print the rectangle character
                if i and j are not within the rectangle, print a space
            }
    
            print a newline
        }
    -
    Last edited by megafiddle; 09-25-2014 at 04:22 PM.

  13. #13
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    I may misunderstand the instructions but isn't this more of what is asked for?
    Code:
    ~~~~~~~
    !     !
    !     !
    !     !
    ~~~~~~~
    That would be the rectangle with spaces inside. Then you would use the the new lines and leading spaces (or tabs) to position it on the screen in the corect position.

    as I say, I may misunderstand the teachers instructions but that is what I got from it.

    Going back and rereading the first post - I think I am wrong here. the rectangle is a filled rectangle after all.

    Code:
    $$$$$$$$$$
    $$$$$$$$$$
    $$$$$$$$$$
    $$$$$$$$$$
    Last edited by PaulS; 09-25-2014 at 04:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. drawing using relative coordinates
    By megafiddle in forum Windows Programming
    Replies: 9
    Last Post: 08-22-2011, 04:56 AM
  2. Coordinates
    By Smoki in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2006, 05:57 PM
  3. x,y coordinates?
    By Isix6568 in forum C Programming
    Replies: 6
    Last Post: 01-21-2005, 04:57 AM
  4. coordinates??
    By adrive in forum C++ Programming
    Replies: 3
    Last Post: 01-24-2002, 03:28 AM
  5. x y coordinates?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 11:05 AM