Sunday, December 6, 2015

Draw a box in C Program

Program #13

Description:
Draw a box dynamically in the screen based on co-ordinates and box size.

#include <stdio.h>
#include <conio.h>
void main(){
 int x,y,boxSize,i,getY,getX;
 clrscr();

 printf(" Enter the co-ordinate to Draw Box (x,y) ;\n");
 printf(" x (column):");
 scanf("%d", &x);
 printf(" y (row):");
 scanf("%d", &y);


/* validate co-ordinates */
 if (x != y) {
   printf("Invalid Co-ordinates!!");
   getch();
   /* Stop the Execution */
   abort();
 }
 printf(" Enter size of the Box:\n");
 scanf("%d",&boxSize);

/* Validate possibility of Drawing Box */
 if ((x+boxSize*2) > 80 ){
   printf("Invalid X Co-ordinate!!");
   getch();
   /* Stop the Execution */
   abort();
 } else if ((y+boxSize) > 25){
   printf("Invalid Y Co-ordinate!!");
   getch();
   /* Stop the Execution */
   abort();
 }


/* Move the cursor (x,y) and Draw top line */
 gotoxy(x, y);
 for(i=1; i<=boxSize; i++){
    printf(" *");
 }


 /* Move the cursor (x,y) and Draw left line */
 getY = y+1;
 gotoxy(x,y);
 for(i=1; i<boxSize-1; i++){
    gotoxy(y, getY);
    printf("*");

    /* To move cursor to next row */
    getY++;
 }


 /* Move the cursor (x,y) and Draw bottom line */
 gotoxy(x, y+boxSize-1);
 for(i=1; i<=boxSize; i++){
    printf(" *");
 }


/* Move the cursor (x,y) and Draw right line */
 getY = y+1;
 getX = x+boxSize*2;
 for(i=1; i<boxSize-1; i++){
    gotoxy(getX, getY);
    printf("*");
    /* To move cursor to next row */
    getY++;
 }
 getch();
}


 

No comments:

Post a Comment

Popular Posts