Showing posts with label Function Prototype in C Program. Show all posts
Showing posts with label Function Prototype in C Program. Show all posts

Sunday, December 13, 2015

Draw Progress Bar using C Program

Program #29

Description:
Draw Progress Bar using C Program.  ASCII character plays to vital role to make it.
ASCII Positions of { 176, 177, 178 and 220} are good to make Progress Bar.

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

/* Define default postion to
Row, Column, Progress bar length and ASCII Position used to make progress Bar
*/
#define DEFAULT_COL 25;
#define DEFAULT_ROW 12;
#define BLOCKS_TO_PRINT 25;
#define ASCII_POSITION 220;

/* !Important:
  Character Position 176, 177, 178 and 220
  are good to make Progress Bar */

void main() {
   int i, col, row, blocks;
   char myAscii;

   /* prototype declaration */
   char getAscii();

   /* Clear the Screen */
   clrscr();

   /* Set default position to print */
   col     = DEFAULT_COL;
   row     = DEFAULT_ROW;
   blocks  = BLOCKS_TO_PRINT;

   /* get expected ASCII */
   myAscii = getAscii();

   for(i=1;i<blocks;i++) {
      gotoxy(col++, row);
      printf("%c", myAscii);
      delay(50);
   }

   getch();
}

/* Iterate the loop until expected ASCII character */
char getAscii(){
  int i, ascii_pos;
  char cc;

  /* Assign default character position */
  ascii_pos = ASCII_POSITION;
  for (i=0; i<256; i++) {

    /*  Break the Loop, once we got the Character */
    if (i == ascii_pos)  break;
    cc = cc + 1;
  }
  return cc;
}



 

Slow Motion print using C Program

Program #28

Description:
Slow motion print using C Program.  Default (column,row) set as (35,12).

/*
 Please wait ... Message print
 using C Program
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main(){

  /* function prototype declaration */
  void initialMessage();

  /* Clear the Screen */
  clrscr();

  /* Print Initial Message to the User */
  initialMessage();

  getch();
}

/* Print Message */
void initialMessage(){

  char *plsMessage = "Please wait ...";

  int i, columnPos=35;
  for(i=0; i<strlen(plsMessage); i++){
    gotoxy(columnPos++,12);
    printf("%c",plsMessage[i]);
    delay(50);
  }
}





 

Saturday, December 12, 2015

Simple Animation using Arrow Keys in C Program

Program #25

Description:
Write simple animation using Arrow key (Up, Down, Left and Right) only in C Program.


/* Arrow Programming in C Program
UP Arrow    - 72
DOWN Arrow  - 80
LEFT Arrow  - 75
RIGHT Arrow - 77
*/

#include<conio.h>

/* Global variable declaration, Row and Column */
int row=13, col=40;
void main(){
  /* Local variable declaration */
  char inputKey, arrowKey;

 /* Prototype declaration */
  void moveUp();
  void moveDown();
  void moveLeft();
  void moveRight();
 
/* Clear the Screen */
  clrscr();
 
/* Move Cursor to Center of the Screen by Default */
  gotoxy(col, row);

  /* Detect Arrow Keys in C Program
     Press ESCAPE - 27 to
     Get out from the Indefinite loop
  */

  while ( (inputKey = getch()) != 27){

    /* Find Arrow Key or not */
    if (inputKey == '\0'){

      /* Extract actual Arrow Key */
      arrowKey = getch();
      switch(arrowKey){
 case 72:
    //printf("UP");
    moveUp();
    break;

 case 80:
    //printf("DOWN");
    moveDown();
    break;

 case 75:
    //printf("LEFT");
    moveLeft();
    break;

 case 77:
    //printf("RIGHT");
    moveRight();
    break;
      }
    }
  }
}

/* Move Cursor - Upward */
void moveUp(){
 if (row != 1){
   row--;
 } else if (row < 1){
   row = 1;
 }

/* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}

/* Move Cursor - Downward */
void moveDown(){
 if (row != 25){
   row++;
 } else if (row > 25){
   row = 25;
 }

 /* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}

/* Move Cursor - Left Side */
void moveLeft(){
 if (col != 1){
   col--;
 } else if (col < 1){
   col = 1;
 }

 /* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}


/* Move Cursor - Right Side */
void moveRight(){
 if (col != 80){
   col++;
 } else if (col > 80){
   col = 80;
 }
 
/* Move the Cursor and Print */
 gotoxy(col, row);
 printf("*");
}



 

Get Alphabet Input using C Program

Program #24

Description:
To detect only character input either lower/upper character and print.  Rest of the keypress are ignored.

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

/* user defined Constant to restrict max length */
#define MAXLENGTH 10

/* Global String Array */
char userInput[MAXLENGTH];

int main(){
 int i, charCounter=0;
 char c=' ';

 /* prototype declaration */
 void printReceivedInputs(int);
 clrscr();

 printf("\n!Important: Maximum %d valid characters only.\n", MAXLENGTH);
 for(i=1; i<46; i++){
   printf("-");
   delay(100);
 }

 printf("\nPlease Enter Alphabets value [a-zA-Z]:\n");
 for(;!kbhit();){
    c = getch();

    /* Stop getting input, If user press below keys
     Enter Key - 13
     ESC Key   - 27
     TAB Key   -  9
     Space Bar - 32
     Backspace -  8
    */

    if (charCounter == MAXLENGTH){
      printf("\n\n\nThanks for your Input!\n");
      for (i=0; i<22; i++){
 printf("_");
 delay(100);
      }
      break;
    } else if (c == 13 || c == 27 || c == 9 || c == 32 || c == 8){
      printf("\nUser Intruption !!");
      getch();
      return 0;
    }

    /* Accept only Lower Case charaters, else ignore */
    else if (c >= 97 && c <= 122){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }

    /* Accept only Upper Case charaters, else ignore */
    else if (c >= 65 && c <= 90){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }
 }

 printf("\n\nInput #\n%s", userInput);
 getch();
 return 0;
}

void printReceivedInputs(int charLen){
  int i;
  gotoxy(15,8);
  printf("          ");
  gotoxy(15,8);
  for (i=0; i<charLen; i++){
    printf("%c",userInput[i]);
  }
}



 

Simple Animation - Drawing Border using C Program


Program #21

Description:
To draw Border in the Screen using dots.  All dots are in multi-colors. Once the border drawing completed.  System will prints success message.


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

/* global prototype declaration */
void setTextColor();

int main(){
  int leftPos=1, rightPos=25;
  int botPos=1, topPos=80;

 /* prototype declaration */
  void drawLeftBorder(int);
  void drawRightBorder(int);
  void drawTopBorder(int);
  void drawBottomBorder(int);
  void initialMessage();
  void finalMessage();
 
/* Clear Screen */
  clrscr();

  /* Initial Message */
  initialMessage();

  for(;!kbhit();){

     /* take a random color */
     setTextColor();

     if (botPos == 1 && topPos == 80 && leftPos <= 25 && rightPos >= 1){
       drawLeftBorder(leftPos++);
       drawRightBorder(rightPos--);
     }
     if (leftPos == 25 && rightPos == 1 && topPos >= 1 && botPos <= 80){
       drawBottomBorder(botPos++);
       drawTopBorder(topPos--);
     }

     if(leftPos == 25 && rightPos == 1 && botPos == 80 && topPos == 1){
       finalMessage();
       getch();

       /* End the Program Execution */
       return 0;
     }
  }
  getch();
  return 0;
}

/*  Draw Left Border */
void drawLeftBorder(int leftPos){
 gotoxy(1,leftPos);
 cprintf("*",leftPos);
 delay(100);
}

/*  Draw Right Border */
void drawRightBorder(int rightPos){
 gotoxy(80,rightPos);
 cprintf("*",rightPos);
 delay(100);
}

/*  Draw Top Border */
void drawTopBorder(int topPos){
 gotoxy(topPos,1);
 cprintf("*",topPos);
 delay(100);
}

/*  Draw Bottom Border */
void drawBottomBorder(int bottomPos){
 gotoxy(bottomPos, 25);
 cprintf("*",bottomPos);
 delay(100);
}

void finalMessage(){
  int i;
  gotoxy(30,12);
  setTextColor();
  cprintf("Border Drawing Completed!");
  gotoxy(30,13);
  for(i=1;i<=25;i++){
    printf("_");
    delay(100);
  }
}

void initialMessage(){
  int i;
  gotoxy(30,13);
  setTextColor();
  cprintf("Press any Key to EXIT!");
  gotoxy(30,14);
  for(i=1;i<=22;i++){
    printf("-");
    delay(100);
  }
}

void setTextColor(){
  int a;
  a = random(15);
  if (a == 0) {
    /* Restrict BLACK color */
    setTextColor();
  } else {
    textcolor(a);
  }
}



 

Friday, December 11, 2015

Get number input using C Program


Program #20

Description:
Get number [0-9] only as input from the user.  Even if user press characters, system will ignore and wait for valid input.  Once input reaches MAXLENGTH (user defined length) program will stop the execution.


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

/* user defined Constant to restrict max length */
#define MAXLENGTH 10

/* Global String Array */
char userInput[MAXLENGTH];
int main(){
 int i, charCounter=0;
 char c=' ';

 /* prototype declaration */
 void printReceivedInputs(int);

 clrscr();
 printf("\n!Important: Maximum %d valid characters only.\n", MAXLENGTH);
 for(i=1; i<46; i++){
   printf("-");
   delay(100);
 }
 printf("\nPlease Enter Integer value [0-9]:\n");

 for(;!kbhit();){
    c = getch();

    /* Stop getting input, If user press below keys
     Enter Key - 13
     ESC Key   - 27
     TAB Key   -  9
     Space Bar - 32
     Backspace -  8
    */

    if (charCounter == MAXLENGTH){
      printf("\n\n\nThanks for your Input!\n");
      for (i=0; i<22; i++){
 printf("_");
 delay(100);
      }
      break;
    } else if (c == 13 || c == 27 || c == 9 || c == 32 || c == 8){
      printf("\nUser Intruption !!");
      getch();
      return 0;
    }
   
/* Accept only Numbers, else ignore */
    else if (c >= 48 && c <= 57){
      userInput[charCounter++] = c;
     
/* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }
 }

 printf("\n\nInput #\n%s", userInput);
 getch();
 return 0;
}

void printReceivedInputs(int charLen){
  int i;
  gotoxy(15,8);
 
  /* Clear the Output area */
  printf("          ");
 
  /* Start to print on perfect position */
  gotoxy(15,8);
  for (i=0; i<charLen; i++){
    printf("%c",userInput[i]);
  }
}


 

 

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


 

Popular Posts