Thread: help build rectangle from input

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    4

    help build rectangle from input

    hw1q3.ci need to build a rectangle with two eyes and mount from user input, the input need to be for int size, int column, and three chars one for the rectangle one for the eyes and one for the mount. the eyes need to be only in a even place >2 and the right eye will be at the same line as the left one, the size >6 , the mount should be between the eyes not include the eyes column. like:
    Please enter the size of the head: 5
    Invalid size. try again: 9
    Enter the left eye column: 4
    Invalid column. try again: 0
    Invalid column. try again: 2
    Enter three chars: * ^ _
    *********
    * *
    * ^ ^ *
    * *
    * *
    * *
    * ___ *
    * *
    *********
    i add my code.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Here is their code, for anyone interested:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int num, x, y;
        /* Asks the user to insert a number between 1 - 20 */
        printf("Please enter the size of the head: ");
        scanf("%d", &num);
    
       /* Checks if the user insert a correct number */
       do{
        if (num > 20){
            printf("Invalid size try again: ");
            scanf("%d", &num);
        }
        else if (num < 6)
        {
            printf("Invalid size try again: ");
            scanf("%d", &num);
        }
       }while(num < 6 || num > 20);
        if(num >= 6 && num <= 20)
            {
            for (x = 0; x < num ; ++x)
            {
            for (y = 0; y < num ; ++y)
            {
            if (x==0 || y==0 || x==(num-1))
            {
            printf("*");
            }
            else if (y==(num-1))
            {
            printf("*\n");
            }
            else
            {
            printf(" ");
            }
           }
          }
        }
    
        return 0;
    
    }
    Devoted my life to programming...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I guess you already know that the code is incomplete, so let's improve on the things it does so far.

    First of all, it doesn't output the rectangle correctly, because it prints the first two lines on the same line. One solution to this is to add the "y==(num-1)" condition to the first if statement( essentially removing the "else if" ) and move the newline outside of the inner for loop.

    In your do...while loop, you don't need two ifs, one is enough, with the compound condition "num < 6 || num > 20".

    May I suggest another way of printing the rectangle that will simplify things:
    Code:
    loop for all rows:
        if row == firstRow or row == lastRow:
            loop for all columns: print '*'
        else:
            print '*'
            loop from second to lastColumn-1:
                print ' '
            print '*'
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Aug 2016
    Posts
    4
    Quote Originally Posted by GReaper View Post
    I guess you already know that the code is incomplete, so let's improve on the things it does so far.

    First of all, it doesn't output the rectangle correctly, because it prints the first two lines on the same line. One solution to this is to add the "y==(num-1)" condition to the first if statement( essentially removing the "else if" ) and move the newline outside of the inner for loop.

    In your do...while loop, you don't need two ifs, one is enough, with the compound condition "num < 6 || num > 20".

    May I suggest another way of printing the rectangle that will simplify things:
    Code:
    loop for all rows:
        if row == firstRow or row == lastRow:
            loop for all columns: print '*'
        else:
            print '*'
            loop from second to lastColumn-1:
                print ' '
            print '*'
    tryed that and i got the rectangle sideways...

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    What did you try( post the code here in code tags ) and what's the problem with it?
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rectangle
    By juice in forum C Programming
    Replies: 10
    Last Post: 10-29-2011, 10:46 PM
  2. Rectangle
    By Vicious in forum Windows Programming
    Replies: 3
    Last Post: 06-03-2002, 10:15 PM
  3. hollow rectangle
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-07-2002, 07:03 AM
  4. rectangle
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 04-06-2002, 06:10 PM
  5. Help With Rectangle,
    By incognito in forum Windows Programming
    Replies: 3
    Last Post: 03-30-2002, 12:43 PM

Tags for this Thread