Loading

Archive for 18 Sept 2012

C - Program to implement Floyd's Algorithm


C - Program to implement Floyd's Algorithm 

 


#include<stdio.h>
#include<conio.h>
int min(int,int);
void floyds(int p[10][10],int n)
{
 int i,j,k;
 for(k=1;k<=n;k++)
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
    if(i==j)
     p[i][j]=0;
    else
     p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(int a,int b)
{
 if(a<b)
  return(a);
 else
  return(b);
}
void main()
{
 int p[10][10],w,n,e,u,v,i,j;;
 clrscr();
 printf("\n Enter the number of vertices:");
 scanf("%d",&n);
 printf("\n Enter the number of edges:\n");
 scanf("%d",&e);
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
   p[i][j]=999;
 }
 for(i=1;i<=e;i++)
 {
  printf("\n Enter the end vertices of edge%d with its weight \n",i);
  scanf("%d%d%d",&u,&v,&w);
  p[u][v]=w;
 }
 printf("\n Matrix of input data:\n");
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
   printf("%d \t",p[i][j]);
  printf("\n");
 }
 floyds(p,n);
 printf("\n Transitive closure:\n");
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
   printf("%d \t",p[i][j]);
  printf("\n");
 }
 printf("\n The shortest paths are:\n");
 for(i=1;i<=n;i++)
  for(j=1;j<=n;j++)
  {
   if(i!=j)
    printf("\n <%d,%d>=%d",i,j,p[i][j]);
  }
 getch();
}

Posted in | Leave a comment

C Program to Implement Warshall's Algorithm


C - Program to Implement Warshall's Algorithm


#include<stdio.h>
#include<conio.h>
#include<math.h>
int max(int,int);
void warshal(int p[10][10],int n)
{
 int i,j,k;
 for(k=1;k<=n;k++)
  for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
    p[i][j]=max(p[i][j],p[i][k]&&p[k][j]);
}
int max(int a,int b)
{                                                      
if(a>b)
 return(a);
else
 return(b);
}
void main()
{
 int p[10][10]={0},n,e,u,v,i,j;
 clrscr();
 printf("\n Enter the number of vertices:");
 scanf("%d",&n);
 printf("\n Enter the number of edges:");
 scanf("%d",&e);
 for(i=1;i<=e;i++)
 {
  printf("\n Enter the end vertices of edge %d:",i);
  scanf("%d%d",&u,&v);
  p[u][v]=1;
 }
 printf("\n Matrix of input data: \n");
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
   printf("%d\t",p[i][j]);
  printf("\n");
 }
 warshal(p,n);
 printf("\n Transitive closure: \n");
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=n;j++)
   printf("%d\t",p[i][j]);
  printf("\n");
 }
 getch();
}

Posted in | Leave a comment

How to Print Output sreen of a C Graphics Program

How to Print Output sreen of a C Graphics Program


Normally we press Prt Scr(Print Screen) button to copy the output of a c-program. However, this does not works for C Graphics Programs. So here's the alternate way to Copy/Print Output Screen of Graphical C-Program :
  1. Download DOSBox (an x86 emulator with dos).
  2. Open DOSBox and Follow the commands, 
  • mount c c: [press enter]
  • c: [press enter]
  • cd c:\tc\bin [press enter]
  • tc [press enter]
     3. This will open TC in DOSBox. Compile and Run the Program Normally. When output Screen appears, minimize screen with Alt + Enter. Now you can Copy the output Window by pressing Alt + Prt Scr.




Posted in | Leave a comment

C program for Countdown

C program for Countdown



#include<graphics.h>
#include<dos.h>
#include<conio.h>
 
main()
{
   int gd = DETECT, gm, i;
   char a[5];
 
   initgraph( &gd, &gm, "C:\\TC\\BGI");
 
   settextjustify( CENTER_TEXT, CENTER_TEXT );
   settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
   setcolor(RED);
 
   for(i = 30 ; i >=0 ; i--)
   {
      sprintf(a,"%d",i);
      outtextxy(getmaxx()/2, getmaxy()/2, a);
      delay(1000);
 
      if ( i == 0 )
         break;
      cleardevice();
   }
 
   getch();
   closegraph();
   return 0;
}

Posted in | Leave a comment

C - Program for Press me button game

C - Program for Press me button game




#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
 
union REGS i, o;
int left = 265, top = 250;
 
void initialize_graphics_mode()
{
  int gd = DETECT, gm, error;
 
  initgraph(&gd,&gm,"C:\\TC\\BGI");
 
  error = graphresult();
 
  if (error != grOk)
  {
    perror("Error ");
    printf("Press any key to exit...\n");
    getch();
    exit(EXIT_FAILURE);
  }
}
 
void showmouseptr()
{
  i.x.ax = 1;
  int86(0x33,&i,&o);
}
 
void hidemouseptr()
{
  i.x.ax = 2;
  int86(0x33,&i,&o);
}
 
void getmousepos(int *x,int *y)
{
  i.x.ax = 3;
  int86(0x33,&i,&o);
 
  *x = o.x.cx;
  *y = o.x.dx;
}
 
void draw_bar()
{
  hidemouseptr();
  setfillstyle(SOLID_FILL,CYAN);
  bar(190,180,450,350);
  showmouseptr();
}
 
void draw_button(int x, int y)
{
  hidemouseptr();
  setfillstyle(SOLID_FILL,MAGENTA);
  bar(x,y,x+100,y+30);
  moveto(x+5,y);
  setcolor(YELLOW);
  outtext("Press me");
  showmouseptr();
}
 
void draw()
{
  settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
  setcolor(BLUE);
  rectangle(0,0,639,450);
  setcolor(RED);
  outtextxy(160,25,"Try to press the \"Press me\" button");
  outtextxy(210,50,"Press escape key to exit");
  setfillstyle(XHATCH_FILL,GREEN);
  setcolor(BLUE);
  bar(1,1,75,449);
  bar(565,1,638,449);
  showmouseptr();
  draw_bar();
  draw_button(left,top);
}
 
void initialize()
{
  initialize_graphics_mode();
 
  if( !initmouse() )
  {
    closegraph();
    printf("Unable to initialize the mouse");
    printf("Press any key to exit...\n");
    getch();
    exit(EXIT_SUCCESS);
  }
 
  draw();
}
 
int initmouse()
{
  i.x.ax = 0;
  int86(0x33,&i,&o);
  return ( o.x.ax );
}
 
void get_input()
{
  int x, y;
 
  while(1)
  {
    getmousepos(&x,&y);
 
    /* mouse pointer in left of button */
 
    if( x >= (left-3) && y >= (top-3) && y <= (top+30+3) && x < left )
    {
      draw_bar();
      left = left + 4;
 
      if (left > 350)
        left = 190;
 
      draw_button(left,top);
    }
 
    /* mouse pointer in right of button */
 
    else if (x<=(left+100+3)&&y>=(top-3)&&y<=(top+30+3)&&x>(left+100))
    {
      draw_bar();
      left = left - 4;
 
      if (left < 190)
        left = 350;
 
      draw_button(left,top);
    }
 
    /* mouse pointer above button */
 
    else if(x>(left-3) && y>=(top-3) && y<(top) && x<= (left+100+3))
    {
      draw_bar();
      top = top + 4;
 
      if (top > 320)
        top = 180;
 
      draw_button(left,top);
    }
 
    /* mouse pointer below button */
 
    else if (x>(left-3)&&y>(top+30)&&y<=(top+30+3)&&x<=(left+100+3))
    {
      draw_bar();
      top = top - 4;
 
      if (top < 180)
        top = 320;
 
      draw_button(left,top);
    }
 
    if (kbhit())
    {
      if (getkey() == 1)
        exit(EXIT_SUCCESS);
    }
  }
}
 
int getkey()
{
  i.h.ah = 0;
  int86(22,&i,&o);
 
  return( o.h.ah );
}
 
main()
{
  initialize();
 
  get_input();
  return 0;
}

Posted in | Leave a comment

C Program for Pattern Matching

C - Program for Pattern Matching


#include <stdio.h>
#include <string.h>
 
int match(char [], char []);
 
int main() {
  char a[100], b[100];
  int position;
 
  printf("Enter some text\n");
  gets(a);
 
  printf("Enter a string to find\n");
  gets(b);
 
  position = match(a, b);
 
  if(position != -1) {
    printf("Found at location %d\n", position + 1);
  }
  else {
    printf("Not found.\n");
  }
 
  return 0;
}
 
int match(char text[], char pattern[]) {
  int c, d, e, text_length, pattern_length, position = -1;
 
  text_length    = strlen(text);
  pattern_length = strlen(pattern);
 
  if (pattern_length > text_length) {
    return -1;
  }
 
  for (c = 0; c <= text_length - pattern_length; c++) {
    position = e = c;
 
    for (d = 0; d < pattern_length; d++) {
      if (pattern[d] == text[e]) {
        e++;
      }
      else {
        break;
      }
    }
    if (d == pattern_length) {
      return position;
    }
  }
 
  return -1;
}

Posted in | Leave a comment

C -Program to Open Website

C -Program to Open Website 

or

 Web Browser in C



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
#include<string.h>
 
void initialize_graphics_mode();
int get_key();
void draw();
 
union REGS i, o;
 
main()
{
  int key, i = 0, xpos, ypos, button;
  char arr[200], temp[5], *ptr;
  char a[] = "C:\\Progra~1\\Mozill~1\\firefox ";
 
  strcpy(arr,a);
 
  i = strlen(a);
 
  initialize_graphics_mode();
 
  draw();
 
  while(1)
  {
    if(kbhit())
      key = get_key();
 
    if((key>=97&&key<=122)||(key>=65&&key<=90)||key==46||key==47||key==63)
    {
      arr[i] = key;
      sprintf(temp,"%c",arr[i]);
      outtext(temp);
      if(getx()>470)
      {
        clearviewport();
        moveto(5,2);
      }
      i++;
    }
    else if ( key == 13 )
    {
      arr[i] = '\0';
      system(arr);
      break;
    }
    else if ( key == 27 )
    {
      closegraph();
      exit(EXIT_SUCCESS);
    }
    if(button==1&&xpos>=150&&xpos<=480&&ypos>=300&&ypos<=330)
    {
      system("C:\\Progra~1\\Mozill~1\\firefox programmingsimplified.com");
      break;
    }
    key = -1;
  }
 
  closegraph();
  return 0;
}
 
void initialize_graphics_mode()
{
  int gd = DETECT, gm, errorcode;
 
  initgraph(&gd,&gm,"C:\\TC\\BGI");
  errorcode = graphresult();
 
  if( errorcode != grOk )
  {
    printf("Graphics error : %s\n",grapherrormsg(errorcode));
 
    printf("Press any key to exit...\n");
    getch();
    exit(EXIT_FAILURE);
  }
}
 
int get_key()
{
  i.h.ah = 0;
  int86(22,&i,&o);
 
  return( o.h.al );
}
 
void draw()
{
  settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
  outtextxy(275,11,"Web Browser");
  outtextxy(155,451,"<a href="http://www.programmingsimplified.com"">www.programmingsimplified.com"</a>);
  outtextxy(5,105,"Enter URL : ");
  rectangle(120,100,600,130);
  setviewport(121,101,599,129,1);
  moveto(5,1);
}

Posted in , | Leave a comment

C - Program for Smiling Face Animation

C - Program for Smiling Face Animation




#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
 
main()
{
   int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
   void *p;
 
   initgraph(&gd,&gm,"C:\\TC\\BGI");
 
   setcolor(YELLOW);
   circle(50,100,25);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(50,100,YELLOW);
 
   setcolor(BLACK);
   setfillstyle(SOLID_FILL,BLACK);
   fillellipse(44,85,2,6);
   fillellipse(56,85,2,6);
 
   ellipse(50,100,205,335,20,9);
   ellipse(50,100,205,335,20,10);
   ellipse(50,100,205,335,20,11);
 
   area = imagesize(left, top, left + 50, top + 50);
   p = malloc(area);
 
   setcolor(WHITE);
   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
   outtextxy(155,451,"Smiling Face Animation");
 
   setcolor(BLUE);
   rectangle(0,0,639,449);
 
   while(!kbhit())
   {
      temp1 = 1 + random ( 588 );
      temp2 = 1 + random ( 380 );
 
      getimage(left, top, left + 50, top + 50, p);
      putimage(left, top, p, XOR_PUT);
      putimage(temp1 , temp2, p, XOR_PUT);
      delay(100);
      left = temp1;
      top = temp2;
   }
 
   getch();
   closegraph();
   return 0;
}

Posted in | Leave a comment

C - Program for Traffic Light Simulation

C - Program for Traffic Light Simulation



#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
 
main()
{
   int gd = DETECT, gm, midx, midy;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   midx = getmaxx()/2;
   midy = getmaxy()/2;
 
   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 3);
   settextjustify(CENTER_TEXT, CENTER_TEXT);
   outtextxy(midx, midy-10, "Traffic Light Simulation");
   outtextxy(midx, midy+10, "Press any key to start");
   getch();
   cleardevice();
   setcolor(WHITE);
   settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy-50, 22);
   setfillstyle(SOLID_FILL,RED);
   floodfill(midx, midy-50,WHITE);
   setcolor(BLUE);
   outtextxy(midx,midy-50,"STOP");
   delay(2000);
   graphdefaults();
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy, 20);
   setfillstyle(SOLID_FILL,YELLOW);
   floodfill(midx, midy,WHITE);
   setcolor(BLUE);
   outtextxy(midx-18,midy-3,"READY");
 
   delay(2000);
   cleardevice();
   setcolor(WHITE);
   rectangle(midx-30,midy-80,midx+30,midy+80);
   circle(midx, midy+50, 22);
   setfillstyle(SOLID_FILL,GREEN);
   floodfill(midx, midy+50,WHITE);
   setcolor(BLUE);
   outtextxy(midx-7,midy+48,"GO");
   setcolor(RED);
   settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);
   outtextxy(midx-150, midy+100, "Press any key to exit...");
 
   getch();
   closegraph();
   return 0;
}

Posted in | Leave a comment