Thread: Create a rectangle with ASCII code.

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    3

    Create a rectangle with ASCII code.

    This first block of code was originally taken right here from the forum.
    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 = 0; x < rows; x++) 
        { 
            for (y = 0; y < columns; y++) 
            {
                if (x == 0 || x == rows - 1)            
                {
                    if (y == 0)  
                    printf("+");
                    if (y == columns - 1)
                    printf("+\n");
                    else
                    printf("-");
                }
            
                else
                {
                    if (y == 0)  
                    printf("|");
                    if (y == columns - 1)
                    printf("|\n");
                    else
                    printf(" ");
                }   
            }
        }    
    return 0;
    }
    
    For my need I said thus:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
        
    int main(void)
    {
        int largura, altura, x, y;
        //system("cls");
        while(1)
        {
            printf("Digite a medida da largura: ");
                scanf("%d", &largura);
            if ((largura < 3) || (largura > 100))
                continue;
            printf("Digite a medida da altura: ");
            scanf("%d", &altura);
            if ((altura < 3) || (altura > 100))
                continue;
    	break;
        }
    
    
        for (x = 0; x < altura; x++) 
        { 
            for (y = 0; y < largura; y++) 
            {
                if (x == 0 || x == altura - 1)            
                {
                    if (y == 0)  
                    //printf("+");
    		printf("%c", 218);
    
    
    		/*if (x != 0)  
                    //printf("+");
    		printf("a");*/
    
    
                    if (y == largura - 1)
                    //printf("+\n");
    		printf("%c\n", 191);
                    else
                    //printf("-");
    		printf("%c", 196);
               }
            
                else
                {
                    if (y == 0)  
                    //printf("|");
    		printf("%c", 179);
                    if (y == largura - 1)
                    //printf("|\n");
    		printf("%c\n", 179);
                    else
                    printf(" ");
                }   
            }
        }    
    getchar();
    return 0;
    }
    And I'm not getting the right characters on these two nodes as marked inside the white frames at the top of the print

    Create a rectangle with ASCII code.-rect_error-jpg

    Finally, I need to leave the rectangle completely closed as it should be, can any of your colleagues please help?
    Thank you and God Bless ...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,124
    Consult your character table to find out which ones to use.

    > //printf("+");
    > printf("%c", 218);

    > //printf("+\n");
    > printf("%c\n", 191);

    You need to find two more corner characters.
    Your edits only suffice for the top-left and top-right corners.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    3
    Good afternoon guys, how are you?
    Sorry for the delay, but, come on:
    I have refined this code in this way, and I just can not make the vertical "right" line to fill the rectangle, nor put it in its right position, on the right side of course, so I came to ask for this force. colleagues.
    Thank you and God Bless ...

    Attachment 15675

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void){
        int l = 0;
        int c = 0;
        int matrix [10][10] = {
                {  1,   2,   3,  4,   5,   6,   7,   8,  9, 10},
                {11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
                {21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
                {31, 32, 33, 34, 35, 36, 37, 38, 39, 40},
                {41, 42, 43, 44, 45, 46, 47, 48, 49, 50},
                {51, 52, 53, 54, 55, 56, 57, 58, 59, 60},
                {61, 62, 63, 64, 65, 66, 67, 68, 69, 70},
                {71, 72, 73, 74, 75, 76, 77, 78, 79, 80},
                {81, 82, 83, 84, 85, 86, 87, 88, 89, 90},
                {91, 92, 93, 94, 95, 96, 97, 98, 99,100}
        };
    
    
        system("cls");
        printf("Digite ' L ' : ");
        scanf("%i", &l);
        
        printf("Digite ' C ' : ");
        scanf("%i", &c);
        
        /*linha superior*/
        if (matrix[0][0]){
            printf("%c", 218);//superior esquerdo
        }
        
        for (matrix[0][0] = 0; matrix[0][0] < 1; matrix[0][0]++){
            for (matrix[0][0] = 0; matrix[0][0] < l; matrix[0][0]++){
                printf("%c", 196);
            }
        }
    
    
        if (matrix[0][10]){
            printf("%c", 191);//superior direito
        }
        //--------------------------------------------------*/
        
        
        /*linha esquerda*/
        for (matrix[0][0] = 0; matrix[0][0] < 1; matrix[0][0]++){
            for (matrix[9][0] = 0; matrix[9][0] < c; matrix[9][0]++){
                printf("\n%c", 179);
            }
        }
        //---------------------------------------------------*/
        
        
        /*linha direita*/
        for (matrix[0][9] = 10; matrix[0][9] < 10; matrix[0][9]++){
            for (matrix[9][9] = 100; matrix[9][100] < c; matrix[9][100]++){
                printf("%c\n", 179);
            }
        }
        //---------------------------------------------------*/    
        
        
        /*linha infeiror*/
        if (matrix[10][0]){
            printf("\n%c", 192);//inferior esquerdo
        }
        
        for (matrix[9][0] = 0; matrix[9][0] < 1; matrix[9][0]++){
            for (matrix[9][0] = 0; matrix[9][0] < l; matrix[9][0]++){
                printf("%c", 196);
            }
        }
    
    
        if (matrix[10][10]){
            printf("%c", 217);//inferior direito
        }
        //--------------------------------------------------*/
        
         
        printf("\n");
        getchar();
        return 0;
    
    
    }
    Attached Images Attached Images Create a rectangle with ASCII code.-test_jpprect-png 
    Last edited by aiodev; 03-08-2019 at 10:49 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,124
    Erm, delete all that matrix stuff and try again.

    Maybe you want to print the matrix INSIDE the box, but that's no reason for using random matrix values as loop control variables.

    > for (matrix[9][9] = 100; matrix[9][100] < c; matrix[9][100]++)
    MASSIVE array overrun, there is no element 100.

    Code:
        for (matrix[0][0] = 0; matrix[0][0] < 1; matrix[0][0]++){
            for (matrix[0][0] = 0; matrix[0][0] < l; matrix[0][0]++){
    Using the same variable for both the outer and inner loop will cause surprises.

    > if (matrix[10][10])
    More array overrun errors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    3
    Look, my friend, I've refactored the code and it came out completely from what I need, which is an array.
    I just need to be able to insert these "nodes" with circles in the image below.
    I looked, I searched and I found nothing, can you help me in this? Thank you and God Bless ...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void){
    	int l = 0;
    	int c = 0;
    	int matrix [10][10] = {
    			{ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10},
    			{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
    			{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
    			{31, 32, 33, 34, 35, 36, 37, 38, 39, 40},
    			{41, 42, 43, 44, 45, 46, 47, 48, 49, 50},
    			{51, 52, 53, 54, 55, 56, 57, 58, 59, 60},
    			{61, 62, 63, 64, 65, 66, 67, 68, 69, 70},
    			{71, 72, 73, 74, 75, 76, 77, 78, 79, 80},
    			{81, 82, 83, 84, 85, 86, 87, 88, 89, 90},
    			{91, 92, 93, 94, 95, 96, 97, 98, 99,100}
        };
    
    
    	system("cls");
    	printf("Digite ' L ' : ");
    	scanf("%i", &l);
    	
    	printf("Digite ' C ' : ");
    	scanf("%i", &c);
    	
    	/* linha superior -------------------------------------------------- */
    	printf("%c", 218);//superior esquerdo
    	
    	for (matrix[8][0] = 0; matrix[8][0] < l; matrix[8][0]++){
    		printf("%c", 196);
    	}
    
    
    	printf("%c", 191);//superior direito
    	/* ----------------------------------------------------------------- */
    		
    	if (matrix[7][0] = 71){
    		printf("%c", 195);
    	}
    	
    	/*linha esquerda*/
    	for (matrix[9][9] = 0; matrix[9][9] < c; matrix[9][9]++){
    		printf("\n%c", 179);
    	}	
    	
    	/*linha direita [ ? ]*/
    	
    	/* linha inferior -------------------------------------------------- */
    	printf("\n%c", 192);//inferior esquerdo
    	
    	for (matrix[9][0] = 0; matrix[9][0] < l; matrix[9][0]++){
    		printf("%c", 196);	
    	}
    
    
    	printf("%c", 217);//inferior direito
        /* ----------------------------------------------------------------- */
    	
    	
    	printf("\n");
    	getchar();
    	return 0;
    }
    Create a rectangle with ASCII code.-nodes_ascii-png

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,124
    > if (matrix[7][0] = 71)
    Suit yourself 'friend'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program to create rectangle
    By Mark Cohen in forum C Programming
    Replies: 28
    Last Post: 07-19-2013, 10:54 AM
  2. Create an ASCII bar graph of die rolls HELP!!!
    By Hannah Toloui in forum C Programming
    Replies: 3
    Last Post: 05-25-2013, 12:21 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Saving ASCII code
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 12:30 AM
  5. ascii code
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-31-2001, 03:24 PM

Tags for this Thread