Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Wednesday, 10 July 2013

Creating Baloon in C Graphics

C Program to Create Baloon using Graphics





#include<dos.h>
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
union REGS in,o;

circ()
{
int i;
for(i=0;i<15;i++)
circle(320,240,i*10);
rectangle(240,160,400,320);
}

bulb()
{
int i=1,c=1;
randomize();
while(!kbhit())
{
if((i%2)==0)
setcolor(c++);
setfillstyle(1,getcolor());
circle(320,240,i*20);
putpixel(320-i+2,240-i+2,1);
floodfill(320-i+2,240-i+2,getcolor());
delay(1000);
i++;
if(i>=5)
i=1;
}
}

dot()
{
int i,j,of=40,wid=5;
setcolor(8);
setfillstyle(1,getcolor());
for(i=of;i<getmaxx();i+=of)
bar(i,0,i+wid,getmaxy());

for(i=of;i<getmaxy();i+=of)
bar(0,i,getmaxx(),i+wid);

for(i=of;i<getmaxx();i+=of)
for(j=of;j<getmaxy();j+=of)
{
setcolor(15);
setfillstyle(1,getcolor());
circle(i+2,j+2,4);
floodfill(i+2,j+2,getcolor());
}

}

void main()
{
int x=0,y=10,gd=DETECT,gm,i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(YELLOW);
settextstyle(1,0,6);
outtextxy(0,240,"Count the black dots.....");
sleep(4);
cleardevice();
dot();
getch();
cleardevice();
setcolor(YELLOW);
settextstyle(1,0,4);
outtextxy(0,240,"I bet the lines of rectangle are straight.....");
sleep(4);
getch();
cleardevice();
circ();
getch();
closegraph();
}

Friday, 21 September 2012

Game Programming in C langauge

Create Car Game  in C Langauge



#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>

void main(void)
{
   int gdriver = DETECT, gmode, errorcode;
   void
*body,*food,*tail1,*tail2,*tail3,*tail4,*head1,*head2,*head3,*head4;
   int x, y,X[5000],Y[5000],i=3,
maxx,maxy,speed=100,bo=10,t[10],score=0,hscore=20;
   unsigned int size;
   char a='6',b,scor[4],hs[4];
   int k=2,l,r1,r2,f=0,z=100,first=0,second=1;;
   r1=300;
   r2=350;
   ifstream infile("c:\\tc\\bin\\rattle.txt");
   infile.getline(hs,4);
   infile.close();
   hscore = atoi(hs);

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }
   maxx = getmaxx();
   maxy = getmaxy();
   y = 160;
   x = 80;
   for(int j=0; j<=3; j++)
   {
 X[i-j]=x-bo*(j+1);
 Y[i-j]=y;
   }
   //body
   setfillstyle(1,2);
   bar(10,10,20,20);
   setcolor(0);
   setlinestyle(0,1,3);
   line(9,9,21,21);
   line(9,21,21,9);
   size = imagesize(10,10,20,20);
   body = malloc(size);
   getimage(10,10,20,20,body);
   cleardevice();
   setcolor(0);
   //tail1-right
   t[0]=20; t[1]=10;
   t[2]=10; t[3]=10;
   t[4]=20; t[5]=15;
   t[6]=10; t[7]=20;
   t[8]=20; t[9]=21;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail1 = malloc(size);
   getimage(10,10,20,21,tail1);
   cleardevice();
   //tail2-left
   t[0]=10; t[1]=10;
   t[2]=20; t[3]=10;
   t[4]=10; t[5]=15;
   t[6]=20; t[7]=20;
   t[8]=10; t[9]=21;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail2 = malloc(size);
   getimage(10,10,20,21,tail2);
   cleardevice();
   //tail3-up
   t[0]=10; t[1]=20;
   t[2]=10; t[3]=10;
   t[4]=15; t[5]=20;
   t[6]=20; t[7]=10;
   t[8]=20; t[9]=21;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail3 = malloc(size);
   getimage(10,10,20,21,tail3);
   cleardevice();
   //tail4-down
   t[0]=10; t[1]=10;
   t[2]=10; t[3]=21;
   t[4]=15; t[5]=10;
   t[6]=20; t[7]=21;
   t[8]=20; t[9]=10;
   fillpoly(5, t);
   size = imagesize(10,10,20,21);
   tail4 = malloc(size);
   getimage(10,10,20,21,tail4);
   cleardevice();
   //head1-right
   setlinestyle(1,1,1);
   setcolor(2);
   fillellipse(10,10,10,5);
   setcolor(4);
   //eyes
   fillellipse(15,7,1,1);
   fillellipse(15,13,1,1);
   size = imagesize(10,5,20,15);
   head1 = malloc(size);
   getimage(10,5,20,15,head1);
   //head2-left
   //eyes
   fillellipse(5,7,1,1);
   fillellipse(5,13,1,1);
   size = imagesize(0,5,10,15);
   head2 = malloc(size);
   getimage(0,5,10,15,head2);
   cleardevice();
   //head3-up
   //eyes
   setcolor(2);
   fillellipse(12,12,5,10);
   setcolor(4);
   fillellipse(9,6,1,1);
   fillellipse(15,6,1,1);
   size = imagesize(7,2,18,12);
   head3 = malloc(size);
   getimage(7,2,18,12,head3);

   //head4-down
   //eyes
   fillellipse(9,17,1,1);
   fillellipse(15,17,1,1);
   size = imagesize(7,12,18,22);
   head4 = malloc(size);
   getimage(7,12,18,22,head4);
   cleardevice();

   //food
   setcolor(15);
   setfillstyle(1,15);
   fillellipse(10,10,3,5);
   size = imagesize(5,3,15,17);
   food = malloc(size);
   getimage(5,3,15,17,food);
   cleardevice();
    /* repeat until a key is pressed */
   maxx=getmaxx();
   maxy=getmaxy();
   setlinestyle(0,1,3);
   setcolor(9);
   rectangle(0,0,maxx,maxy);
   setlinestyle(0,1,2);
   line(maxx-150,0,maxx-150,maxy);
   setcolor(8);
   setfillstyle(1,8);
   bar(maxx-147,3,maxx-3,63);
   setcolor(12);
   int maze=1;
   if(maze==1)
   {
   setfillstyle(1,6);
   bar(4,4,maxx-154,14);
   bar(4,4,14,maxy-4);
   bar(4,maxy-4,maxx-154,maxy-14);
   bar(maxx-154-10,4,maxx-154,maxy-4);
   }

   settextstyle(0,0,1);
   delay(300);
   outtextxy(maxx-125,30,"RATTLE SNAKE");
   delay(300);
   setcolor(11);
   setlinestyle(0,1,2);
   rectangle(maxx-145,65,maxx-5,205);
   outtextxy(maxx-128,70,"INSTRUCTIONS");
   delay(200);
   setcolor(6);
   outtextxy(maxx-140,90,"Commands     Key");
   delay(300);
   setcolor(8);
   outtextxy(maxx-140,110," Move Up      8");
   delay(50);
   outtextxy(maxx-140,130," Move Down    5");
    delay(50);
   outtextxy(maxx-140,150," Move Right   6");
    delay(50);
   outtextxy(maxx-140,170," Move Left    4");
    delay(50);
   outtextxy(maxx-140,190," Exit         0");
   delay(200);
   setcolor(12);
   setlinestyle(0,1,2);
   rectangle(maxx-145,210,maxx-5,350);
   setcolor(1);
   outtextxy(maxx-130,220,"Player's Name");
   setcolor(15);
   outtextxy(maxx-100,230,"Yellow");
   setcolor(4);
   delay(200);
   itoa(score,scor,10);
   outtextxy(maxx-140,250,"Score");
   outtextxy(maxx-50,250,scor);
   delay(200);
   setcolor(10);
   outtextxy(maxx-140,280,"Level");
   delay(200);
   setcolor(13);
   outtextxy(maxx-140,310,"Maze        1");
   delay(200);
   setcolor(14);
   outtextxy(maxx-140,340,"High Score  ");
   outtextxy(maxx-50,340,hs);
   for(j=0; j<=1000; j++)
   {
 putpixel(random(maxx-150),random(maxy),6);
 delay(1);
   }
   while (a!='0')
   {
       // plot new image
      if(a=='6')
     putimage(x, y, head1, XOR_PUT);
      if(a=='4')
     putimage(x, y, head2, XOR_PUT);
      if(a=='8')
     putimage(x, y, head3, XOR_PUT);
      if(a=='5')
     putimage(x, y, head4, XOR_PUT);
      for(j=0; j<k; j++)
      {
    putimage(X[i-j], Y[i-j], body, XOR_PUT);
      }
      if((X[i-j-1]-X[i-j])<0  && (Y[i-j-1]-Y[i-j])==0)
     putimage(X[i-j], Y[i-j], tail1, XOR_PUT);
      if((X[i-j-1]-X[i-j])>0  && (Y[i-j-1]-Y[i-j])==0)
     putimage(X[i-j], Y[i-j], tail2, XOR_PUT);
      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])<0)
     putimage(X[i-j], Y[i-j], tail3, XOR_PUT);
      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])>0)
     putimage(X[i-j], Y[i-j], tail4, XOR_PUT);
      delay(speed);
      if(second==1)
      {
 setcolor(15);
 outtextxy(maxx-140,360,"Press Any Key...");
 getch();
 setcolor(0);
 outtextxy(maxx-140,360,"Press Any Key...");
 a='6';
 second=2;
      }

      if(first==1)
      {
 setcolor(15);
 outtextxy(maxx-140,360,"Congratulations  ");
 sound(100);
 delay(300);
 nosound();
 getch();
 setcolor(0);
 outtextxy(maxx-140,360,"Congratulations  ");
 second++;
 first=2;
      }
 // erase old image
      if(a=='6')
     putimage(x, y, head1, XOR_PUT);
      if(a=='4')
     putimage(x, y, head2, XOR_PUT);
      if(a=='8')
     putimage(x, y, head3, XOR_PUT);
      if(a=='5')
     putimage(x, y, head4, XOR_PUT);

      for(j=0; j<k; j++)
      {
    putimage(X[i-j], Y[i-j], body, XOR_PUT);
      }
      if((X[i-j-1]-X[i-j])<0  && (Y[i-j-1]-Y[i-j])==0)
     putimage(X[i-j], Y[i-j], tail1, XOR_PUT);
      if((X[i-j-1]-X[i-j])>0  && (Y[i-j-1]-Y[i-j])==0)
     putimage(X[i-j], Y[i-j], tail2, XOR_PUT);
      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])<0)
     putimage(X[i-j], Y[i-j], tail3, XOR_PUT);
      if((X[i-j-1]-X[i-j])==0  && (Y[i-j-1]-Y[i-j])>0)
     putimage(X[i-j], Y[i-j], tail4, XOR_PUT);

      if(f==0)
      {
 putimage(r1,r2,food,XOR_PUT);
 f=1;
      }
      z--;
      if((x>=r1 && y>=r2 && x<=r1+10 && y<=r2+10) || (x<=r1 && y<=r2 &&
x>=r1-10 && y>=r2-10) || z==0)
      {
 if(z!=0)
 {
  sound(800);
  delay(20);
  setcolor(0);
  score += 10;
  outtextxy(maxx-50,250,scor);
  itoa(score,scor,10);
  if(score>hscore)
  {
   strcpy(hs,scor);
   setfillstyle(1,1);
   bar(maxx-140,337,maxx-20,348);
   setcolor(14);
   outtextxy(maxx-50,340,hs);
   outtextxy(maxx-140,340,"High Score  ");
   if(first==0)
   {
   first=1;
   }
  }
  setcolor(4);
  outtextxy(maxx-50,250,scor);
  k++;
 nosound();
 }
 z=100;
 putimage(r1,r2,food,XOR_PUT);
 repr1:
 r1=random(450);
     if(r1<50)
        goto repr1;
 repr2:
 r2=random(400);
     if(r2<50)
  goto repr2;
 f=0;
      }

      i++;
      X[i]=x;
      Y[i]=y;
      b=a;
      if(kbhit())
      {
  rep:
  a=getche();
  if((b=='6' && a=='4') || (b=='4' && a=='6'))
   a=b;
  if((b=='8' && a=='5') || (b=='5' && a=='8'))
   a=b;
  if(a!='6' && a!='4' && a!='8' && a!='0' && a!='5')
   a=b;
      }
 /*     if(x>maxx-170)
 x=0;
      if(y>maxy)
 y=0;*/
      if(a=='8')
 y -= bo;
      if(a=='5')
 y += bo;
      if(a=='4')
 x -= bo;
      if(a=='6')
 x += bo;
      for(j=i+1; j<i-k; j--)
      {
 if((Y[i]>=Y[j] && Y[i]<=Y[j]+10) || (Y[i]<=Y[j] && Y[i]>=X[j]-10))
 {
  if((X[i]>=X[j] && X[i]<=X[j]+10) || (X[i]<=X[j] && X[i]>=X[j]-10))
  {
   a='0';
  }
   outtextxy(maxx-140,360,"C ");

 }
 if((X[i]>=X[j] && X[i]<=X[j]+10) || (X[i]<=X[j] && X[i]>=X[j]-10))
 {
  if((Y[i]>=Y[j] && Y[i]<=Y[j]+10) || (Y[i]<=Y[j] && Y[i]>=X[j]-10))
  {
   a='0';
  }
   outtextxy(maxx-140,360,"C  ");
 }
       }
/*      for(j=i; j<i-k; j--)
      {
 if((y>=Y[j] && y<=Y[j]+10) || (y<=Y[j] && y>=Y[j]-10))
 {
  if((x>=X[j] && x<=X[j]+10) || (x<=X[j] && x>=X[j]-10))
  {
   a='5';
  }
 }
 if((x>=X[j] && x<=X[j]+10) || (x<=X[j] && x>=X[j]-10))
 {
  if((y>=Y[j] && y<=Y[j]+10) || (y<=Y[j] && y>=Y[j]-10))
  {
   a='5';
  }
 }
 if(j==0)
  break;
      }      */

  /* if(x<=0)
 x=maxx-170;
 if(y<=0)
 y=maxy;*/
 if(maze==1)
 {
  if(x<=14 || x>=maxx-184 || y<=14 || y>=maxy-14)
  {
     a='0';
  }
 }
   }
   if(score>hscore)
   {
 ofstream onfile("c:\tc\bin\rattle.txt");
 onfile<<scor;
 onfile.close();
   }
   setcolor(15);
   outtextxy(maxx-140,390,"    Gameover");
   sound(100);
   delay(400);
   nosound();
   getch();
   /* clean up */
   free(body);
   closegraph();
}

Draw Triangle in C

Draw Triangle in C Language using C Graphics











#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
void rotabouty(float *x,float *y,float *z);
void rotaboutx(float *x,float *y,float *z);
void rotaboutz(float *x,float *y,float *z);
 char ch;
   float oldx, oldy, oldz;
   double angle;

int main()
{
   int gdriver=DETECT, gmode=DETECT;
   float x1,y1,z1;
   float x2,y2,z2;
   float x3,y3,z3;
   float x5,y5,z5;
   float x6,y6,z6;
   float x7,y7,z7;

   initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
   setviewport(320,235,639,479,0);
   x1=100;y1=100;z1=-100;
   x2=100;y2=-100;z2=-100;
   x3=-100;y3=-100;z3=-100;

   x5=100;y5=100;z5=100;
   x6=100;y6=-100;z6=100;
   x7=-100;y7=-100;z7=100;

   setcolor(1);
   moveto(x1,y1);
   lineto(x2,y2);
   lineto(x3,y3);
   lineto(x1,y1);
    setcolor(4);
   moveto(x5,y5);
   lineto(x6,y6);
   lineto(x7,y7);
   lineto(x5,y5);
   getch();
   do{
  ch=getch();
  if(ch=='s')
  {

  angle=(-10.0)*.017453; //converts the angle into radians
     rotabouty(&x1,&y1,&z1);
     rotabouty(&x2,&y2,&z2);
     rotabouty(&x3,&y3,&z3);
     rotabouty(&x5,&y5,&z5);
     rotabouty(&x6,&y6,&z6);
     rotabouty(&x7,&y7,&z7);

  }
  if(ch=='a')
  {

 angle=10.0*.017453;
     rotabouty(&x1,&y1,&z1);
     rotabouty(&x2,&y2,&z2);
     rotabouty(&x3,&y3,&z3);
     rotabouty(&x5,&y5,&z5);
     rotabouty(&x6,&y6,&z6);
     rotabouty(&x7,&y7,&z7);
  }
  if(ch=='r')
  {

  angle=(-10.0)*.017453;
     rotaboutx(&x1,&y1,&z1);
     rotaboutx(&x2,&y2,&z2);
     rotaboutx(&x3,&y3,&z3);
     rotaboutx(&x5,&y5,&z5);
     rotaboutx(&x6,&y6,&z6);
     rotaboutx(&x7,&y7,&z7);

  }
  if(ch=='v')
  {
  angle=10.0*.017453;

     rotaboutz(&x1,&y1,&z1);
     rotaboutz(&x2,&y2,&z2);
     rotaboutz(&x3,&y3,&z3);
     rotaboutz(&x5,&y5,&z5);
     rotaboutz(&x6,&y6,&z6);
     rotaboutz(&x7,&y7,&z7);

  }
  if(ch=='e')
  {

  angle=(-10.0)*.017453;
     rotaboutz(&x1,&y1,&z1);
     rotaboutz(&x2,&y2,&z2);
     rotaboutz(&x3,&y3,&z3);
     rotaboutz(&x5,&y5,&z5);
     rotaboutz(&x6,&y6,&z6);
     rotaboutz(&x7,&y7,&z7);

  }
  if(ch=='h')
  {
  angle=10.0*.017453;

     rotaboutx(&x1,&y1,&z1);
     rotaboutx(&x2,&y2,&z2);
     rotaboutx(&x3,&y3,&z3);
     rotaboutx(&x5,&y5,&z5);
     rotaboutx(&x6,&y6,&z6);
     rotaboutx(&x7,&y7,&z7);

  }
   setviewport(0,0,639,479,0);
   clearviewport();
   setviewport(320,235,639,479,0);
   setcolor(YELLOW);
   moveto(x1,y1);
   lineto(x2,y2);
   lineto(x3,y3);
   lineto(x1,y1);

   setcolor(15);
   moveto(x5,y5);
   lineto(x6,y6);
   lineto(x7,y7);
   lineto(x5,y5);

   } while (ch!='p');
   getch();
   closegraph();
   return 0;
}
 void rotabouty(float *x,float *y,float *z)
   {
  oldx=*x;
      oldy=*y;
      oldz=*z;
      *x = (oldx*cos(angle)) - (oldz*sin(angle));
      *y = *y;
      *z =(oldx*sin(angle)) + (oldz*cos(angle));
    }
    void rotaboutx(float *x,float *y,float *z)
    {
      oldx=*x;
      oldy=*y;
      oldz=*z;
      *x=*x;
      *y=(oldz*sin(angle))+(oldy*cos(angle));
      *z=(oldz*cos(angle))-(oldy*sin(angle));
    }
    void rotaboutz(float *x,float *y,float *z)
    {
      oldx=*x;
      oldy=*y;
      oldz=*z;
      *x=(oldx*sin(angle))+(oldy*cos(angle));
      *y=(oldx*cos(angle))-(oldy*sin(angle));
      *z=*z;
    }

Draw a Circle using Computer Graphics in C

Draw a Circle using Computer Graphics in C


#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include<stdlib.h>
void main()
{
 int dr,md,midx,i, midy,dx,dy,mx, my,k,x,y,ax[100], ay[100];
 int nx[100], ny[100];
 double pi,tpi,a,t;
 dr = DETECT;

 initgraph(&dr, &md, "c:\\tc\\bgi");
 midx = getmaxx()/2; midy = getmaxy()/2;
 pi = 3.14159;
 tpi = pi * 2.0;
 k=0;
 for (a = 0.0; a<=tpi; a=a+pi/3)
 {
  x = midx + 100 * cos(a);
  y = midy + 100 * sin(a);
  putpixel(x,y,WHITE);
  ax[k] = x;
  ay[k] = y;
  k++;
 }

 for (a=0.0; a<=tpi; a=a+pi/12)
 {
  for (i=0; i<k; i++)
  {
  nx[i]=( (ax[i]-midx)*cos(a)) - ((ay[i]-midy)*sin(a)  ) + midx;
  ny[i]=( (ax[i]-midx)*sin(a)) + ((ay[i]-midy)*cos(a) ) + midy;
  if (i==0)
  {
   dx = nx[i]; dy = ny[i];
  }
  else
  {
   line (nx[i],ny[i], dx,dy);
   dx=nx[i]; dy = ny[i];
  }
  }
  getch();

 }

 getch();
}

Make Graphics Program in C Language

Make Graphics Program in C Language


#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include<stdlib.h>
void main()
{
 int dr,md,midx,i, midy,dx,dy,mx, my,k,x,y,ax[100], ay[100];
 int nx[100], ny[100];
 double pi,tpi,a,t;
 dr = DETECT;

 initgraph(&dr, &md, "c:\\tc\\bgi");
 midx = getmaxx()/2; midy = getmaxy()/2;
 pi = 3.14159;
 tpi = pi * 2.0;
 k=0;
 for (a = 0.0; a<=tpi; a=a+pi/2)
 {
  x = 550 + 70 * cos(a);
  y = 200 + 70 * sin(a);

  setcolor(random(getmaxcolor()));
  putpixel(x,y,random(getmaxcolor()));
  ax[k] = x;
  ay[k] = y;
  k++;
 }



 for (a=0.0; a<=tpi; a=a+pi/50)
 {
  for (i=0; i<k; i++)
  {
  nx[i]=((ax[i]-midx)*cos(a)) - ((ay[i]-midy)*sin(a)) + midx ;
  ny[i]=((ax[i]-midx)*sin(a)) + ((ay[i]-midy)*cos(a)) + midy;
  if (i==0)
  {
   dx = nx[i]; dy = ny[i];
  }
  else
  {
   line (nx[i],ny[i], dx,dy);
   dx=nx[i]; dy = ny[i];
  }
  }
  getch();

 }


 getch();
}

Make Graphics Program in C Language

Make Graphics Program in C Language 

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include<stdlib.h>
void main()
{
 int dr,md,midx, midy, mx, my,i,j,k,x,y,x1,y1;
 double pi,tpi,a,t;
 dr = DETECT;


 initgraph(&dr, &md, "c:\\tc\\bgi");
 midx = getmaxx()/2; midy = getmaxy()/2;
 pi = 3.14159;
 tpi = pi * 2.0;
 t=100;

for (t = 0; t < 40 * 5; t += .1)
{
  x = midx+ (t+t * cos(t));
  y = midy+ (t * sin(t));
  x1 = midx+ (t * cos(t));
  y1 = midy+ (t+t * sin(t));
  putpixel(x,y,random(getmaxcolor()));
  putpixel(x1,y1,random(getmaxcolor()));

  setcolor(random(getmaxcolor()));
  if (t==0.0)
  circle(x,y,50);

  if (kbhit()) break;
}


 getch();

}

Friday, 14 September 2012

Transform Matrix in C Language

Transform Matrix in C Language


#include<stdio.h>

main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];

   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of matrix \n");

   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&matrix[c][d]);
      }
   }

   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }

   printf("Transpose of entered matrix :-\n");

   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      } 
      printf("\n");
   }

   return 0;
}

Subtraction of Matrix in C Language

Subtraction of Matrix in C Language

#include<stdio.h>

int main()
{
  int m, n, c, d, first[10][10], second[10][10], difference[10][10];

  printf("Enter the number of rows and columns of matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");

  for (c = 0; c < m; c++)
    for (d = 0 ; d < n; d++)
      scanf("%d", &first[c][d]);

  printf("Enter the elements of second matrix\n");

  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
        scanf("%d", &second[c][d]);

  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
      difference[c][d] = first[c][d] - second[c][d];

  printf("difference of entered matrices:-\n");

  for (c = 0; c < m; c++)
  {
    for (d = 0; d < n; d++)
      printf("%d\t",difference[c][d]);

    printf("\n");
  }

  return 0;
}

Array Multiplication in C Language

Array Multiplication in C Language


#include <stdio.h>

int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");

  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf("%d", &first[c][d]);

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if ( n != p )
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");

    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);

    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }

        multiply[c][d] = sum;
        sum = 0;
      }
    }

    printf("Product of entered matrices:-\n");

    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);

      printf("\n");
    }
  }

  return 0;
}

Program to Marge Two Array in C Language

Program to  Marge Two Array in C Language

#include <stdio.h>

void merge(int [], int, int [], int, int []);

int main() {
  int a[100], b[100], m, n, c, sorted[200];

  printf("Input number of elements in first array\n");
  scanf("%d", &m);

  printf("Input %d integers\n", m);
  for (c = 0; c < m; c++) {
    scanf("%d", &a[c]);
  }

  printf("Input number of elements in second array\n");
  scanf("%d", &n);

  printf("Input %d integers\n", n);
  for (c = 0; c < n; c++) {
    scanf("%d", &b[c]);
  }

  merge(a, m, b, n, sorted);

  printf("Sorted array:\n");

  for (c = 0; c < m + n; c++) {
    printf("%d\n", sorted[c]);
  }

  return 0;
}

void merge(int a[], int m, int b[], int n, int sorted[]) {
  int i, j, k;

  j = k = 0;

  for (i = 0; i < m + n;) {
    if (j < m && k < n) {
      if (a[j] < b[k]) {
        sorted[i] = a[j];
        j++;
      }
      else {
        sorted[i] = b[k];
        k++;
      }
      i++;
    }
    else if (j == m) {
      for (; i < m + n;) {
        sorted[i] = b[k];
        k++;
        i++;
      }
    }
    else {
      for (; i < m + n;) {
        sorted[i] = a[j];
        j++;
        i++;
      }
    }
  }
}

Bubble Sort Array Program in C Language

 Bubble Sort Array Program in  C Language

#include <stdio.h>

int main()
{
  int array[100], n, c, d, swap;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);

  return 0;
}

Saturday, 11 February 2012

String Concatenation in C


String Concatenation in C

#include<stdio.h>
#include<string.h>
void main()
{
char str1[10],str2[10],str[20];
int i=0,j=0,k=0;
clrscr();
printf ("enter the two strings\n");
gets (str1);
gets (str2);
while (str1[i]!='\0')
{
str[k++]=str1[i++];
}
while (str2[j]!='\0')
{
str[k++]=str2[j++];
}
str[k]='\0';
printf("The concatenated string is:");
puts (str);
getch();
}

Program to Compare Two Strings in C

 Program to Compare Two Strings in C

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string ");
gets(a);
printf("Enter the second string ");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
getch();
return 0;
}

C Program to show no of occurrences of a string in a paragraph


C Program to show no of occurrences of a string in a paragraph



#include<stdio.h>
#include <string.h>
void main()
{
char st1[25],st2[25];
int cnt,i,j,k,c,len,m,sign;
clrscr();
printf("Enter the first string\n");
gets(st1);
printf("Enter the second string\n");
gets(st2);
len=strlen(st1);
for(i=0;i<len;i++)
{
c=0;
if (st1[i]==st2[c])
{
m=i;
sign=0;
cnt=0;
while(st2[c]!='\0' && sign!=1)
{
if (st1[m]==st2[c])
{
m++;c++;
cnt++;
}
else
sign=1;
}
if (sign==0)
{
printf("The given string is present\n");
printf("The starting position %d & ending position %d\n",i+1,(i+cnt));
k=1;
}
}
}
if (k != 1)
if (sign!=0)
printf("The given string is not present\n");
getch();
}