Friday, December 11, 2015

Simple Animation to Fill Box color using C Program



Program #18

Description:
Dynamically plotting dots on a specific area in the screen using multi-colors.  Once all positions plotting is done.  Program will come to an end.

#include<conio.h>
#include<stdlib.h>
#include<dos.h>

/* Global Array Declaration */
int Box[50][50];

int main(){
  int counter=0, rowValue=1, colValue=1;
  int boxStatus, colorValue;

  /* function prototype declaration */
  int getRow();
  int getColumn();
  int checkBoxCompletion();
 
/* Clear the Screen */
  clrscr();

 /* Iterate Indefinite loop */
  for(;!kbhit();){
    rowValue = getRow();
    colValue = getColumn();
    /* set Box position using Currrent Row,Column */
    if (Box[rowValue][colValue] != 1){
      Box[rowValue][colValue] = 1;
    } else {

 /* avoid overwritting plotted color
      get a new blank position.
      If you wish, just comment below "continue"
      and see the difference
      */
      continue;
    }

  /* Re-initialize counter as 0 once it reaches INT max value */
    if (counter == 32766) counter = 0;

  /* Increase the Counter */
    counter++;

 /* get random color */
    colorValue = getColor();

   /* set color using randow value */
    textcolor(colorValue);
    /* Move the cursor position */
    gotoxy(colValue, rowValue);

   /* do color print */
    cprintf("*");
   
/* set some delay in Milliseconds
    as much you decrease, that much fast system will
    fill the color + add mild-sound
    */
    sound(200*rowValue);
    delay(10);
   
/* after delay, mute the sound */
    nosound();
   
/* check box completion station after every dot plot */
    boxStatus = checkBoxCompletion();
    if (boxStatus == 1) {
      gotoxy(18, 18);

 /* print the success message using last dot color, Interesting!! */
      cprintf("Box Color Fill: Completed Successfully !!");
      getch();

  /* stop the execution */
      return 1;
    }
   }
   return 0;
}

int getRow(){
  int rowNum;

  /* getting a randow row */
   rowNum = random(25);
  if (rowNum<8 || rowNum>15){

 /* get next random */
    getRow();
  } else {
    return rowNum;
  }

  /* worst case, default return value */
  return 8;
}

int getColumn(){
  int colNum;

 /* getting a randow column */
   colNum = random(80);
   if (colNum<25 || colNum>50){

  /* get Next Column */
      getColumn();
   } else {
      return colNum;
   }
   /* worst case, default return value */
  return 25;
}

int getColor(){
   int color;

 /* getting a randow color */
   color = random(15);
   if (color == 0){

   /* 0 means BLACK color, it will spoil the Spot */
     getColor();
   } else {
     return color;
   }

  /* worst case, default return value */
   return 1;
}

int checkBoxCompletion(){
  int i,j;
  for (i=8;i<=15;i++){
    for (j=25; j<=50; j++){
      if(Box[i][j] != 1){

/* Color fill not yet completed */
 return 0;
      }
    }
  }
 
/* Color fill Finished */
  return 1;
}

 

Simple Animation using C Program


Program #17

Description:
Randomly move the position and rotate {/ \ | } symbols.


#include<conio.h>
#include<stdlib.h>
#include<dos.h>
void main(){
  int counter=0, rowValue=1, colValue=1, colorValue;

 /* function prototype declaration */
  int getRow();
  int getColumn();

 /* Clear the Screen */
  clrscr();

 /* Iterate Indefinite loop */
  for(;!kbhit();){
   
/* Move the Rotation every 32766 counter */
    if (counter%32766 == 0){
   /* Get New Row and Column */
      rowValue = getRow();
      colValue = getColumn();
     
/* Re-initialize counter as 0 once it reaches INT max value */
      if (counter == 32766){
 counter = 0;

/* Delay for a while in Milliseconds */
 delay(100);
      }
  
 /* Clear the Screen, and show a fresh as Rotation */
      //clrscr();
    }

   /* Increase the Counter */
    counter++;

    colorValue = getColor();
    textcolor(colorValue);
    gotoxy(colValue, rowValue);
    cprintf("|");
    gotoxy(colValue, rowValue);
    cprintf("/");
    gotoxy(colValue, rowValue);
    cprintf("\\");
   }
}

int getRow(){
  int rowNum=1, i=0;
  for(;i<25;i++){
   /* getting a randow row, if system gives 0, trying next number */
   rowNum = random(25);
    if (rowNum!=0){
      return rowNum;
    }
  }
  /* worst case, hope it won't return */
  return rowNum;
}

int getColumn(){
  int colNum=1, i=0;
  for(;i<80;i++){
   /* getting a randow column, if system gives 0, trying next number */
   colNum = random(80);
    if (colNum!=0){
      return colNum;
    }
  }
  /* worst case, hope it won't return */
  return colNum;
}

int getColor(){
   int color;
   /* getting a randow color */
   color = random(15);
   return color;
}


 

Indefinite loop in C Program


Program #16

Description:
To iterate indefinite loop until user press any keyboard characters.

void main(){
clrscr();
 /*
 Indefinite loop using kbhit() function
 kb  - KeyBoard
 hit - Type/Enter
 */
 for(;!kbhit();){
    printf(" * \t @ \t & \t %");
 }
 getch();
}

Note:
kbhit() functionality will not obey for few keyboard characters.. I am expecting the answer from blog readers.  Once you know the key.. Reply it.!!

Wednesday, December 9, 2015

Find a String is Palindrome or not in C Program

Program #15

Description:
To find a given string is PALINDROME or not

What is palindrome?

DUMMY != YMMUD
PALINDROME != EMORDNILAP

LIRIL   = LIRIL
AMMA = AMMA

If a string and its reversed string is same then that string is a "Palindrome".

#include<stdio.h>
void main(){
 char palind[50], compar[50];
 int i,j=0, IsPalind;

 clrscr();
 printf("Please Enter a String to find Palidrome or not?\n");
 scanf("%s", &palind);

 for(i=strlen(palind)-1; i>=0; i--){
   /* Assign last char of Input string into first position of Compare array */
   compar[j]=palind[i];
   j++;
 }

 /* Compare both array to find Palindrome or not */
 IsPalind = 1;
 for(i=0; i<strlen(palind); i++){
   if (palind[i] != compar[i] ){
      IsPalind = 0;
      /* If single character not match, go out of the Loop*/
      break;
   }
 }

 printf("\n\nResult:\n");
 printf("-------\n");

 if (IsPalind){
   printf("Input String is a Palindrome !!");
 } else {
   printf("Invalid Input, Better luck next time !!");
 }

 getch();
}

Sunday, December 6, 2015

Text and Background color print in C Program

Program #14

Description:
Set text color as well background color in C Program.

#include<stdio.h>
#include<conio.h>
int main(void)
{
   int i;

   clrscr();
   for (i=0; i<15; i++)
   {

       /* Just positioning to center */
       gotoxy(30,i+1);

       cprintf("Let Us C");
       cprintf("\r\n");
   
    /* set text color */
       textcolor(i+1);
   
    /* set background color */
       textbackground(i);
   }

   getch();
   return 0;
}



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();
}


 

Saturday, December 5, 2015

main() function and return datatype in c

Description:
Why we need to keep void always to main().
Shall we try assign different type of datatypes?

Program #8
/* Usual way of void as return to main() */
# include "stdio.h"
void main(){
  clrscr();
  printf("void main() function in c (no more return) !!");
  getch();
 
  //no return
}

Output:
void main() function in c (no more return) !!

Program #9
/* int as return datatype to main() */
# include "stdio.h"
int main(){
  clrscr();
  printf("main() function in c (return integer) !!");
  getch();
 
  //return integer value, any as you wish
  return 1306;
}

Output:
main() function in c (return integer) !!

Program #10
/* float as return datatype to main() */
# include "stdio.h"
float main(){
  clrscr();
  printf("main() function in c (return float) !!");
  getch();
 
  //return float value, any as you wish
  return 13.06;
}

Output:
main() function in c (return float) !!

Program #11
/* char as return datatype to main() */
# include "stdio.h"
char main(){
  clrscr();
  printf("main() function in c (return char) !!");
  getch();
 
  //return char value, any as you wish
  return 'm';
}

Output:
main() function in c (return char) !!

Program #12
/* double as return datatype to main() */
# include "stdio.h"
double main(){
  clrscr();
  printf("main() function in c (return double) !!");
  getch();
 
  //return double value, any as you wish
  return 130600;
}







Output:
main() function in c (return double) !!

Popular Posts