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



 

2 comments:

  1. for(i=1;i<blocks;i++)
    The above loop runs for 24 times, is it intentional?

    char cc;
    The above variable is uninitialized, is it intentional to print any character from 0 - 255?


    ReplyDelete
  2. Dear Deepak,
    Thanks for yor valuable comments.

    Query #1
    You are right, code will iterate for 24 times. While running only blog user will feel or by observing whole code logic this will hit in their mind. Something wrong. So, Just i added like this. Thank you.

    Query #2
    Yes, character cc is not initialized. If it is not initialized. It will start its ASCII position from 0 to 255.
    User can use any character as their choice to draw.

    If you assign cc='50', then it means, we are intentionally overwriting the position and from that ASCII position it will get incremented.

    Thanks once again.
    Hope you will get clarified.

    ReplyDelete

Popular Posts