Friday, 17 January 2014

C++ Programming of DDA line drawing algorithm


# include <graphics.h>
# include <math.h>
# include <conio.h>
# include <iostream.h>
void DDALine(int x1,int y1,int x2,int y2,int iColor);
void main()
{
    int gDriver=DETECT,gMode;

    int x1,x2,y1,y2,iColor;

    initgraph(&gDriver,&gMode,"c:\\tc\\bgi");
    cleardevice();
    cout<<endl<<"Enter x1  : ";
    cin>>x1;
    cout<<"Enter y1  : ";
    cin>>y1;
    cout<<endl<<"Enter x2  : ";
    cin>>x2;
    cout<<"Enter y2  : ";
    cin>>y2;
    cout<<endl<<"Enter COLOR  : ";
    cin>>iColor;
    cleardevice();
    DDALine(320,1,320,480,12);
    DDALine(1,240,640,240,12);
    circle(320,240,2);
    DDALine(320+x1,240-y1,320+x2,240-y2,iColor%16);
    getch();
}

void DDALine(int x1,int y1,int x2,int y2,int iColor)
{
    float dX,dY,iSteps;
    float xInc,yInc,iCount,x,y;

    dX = x1 - x2;
    dY = y1 - y2;

    if (fabs(dX) > fabs(dY))
    {
        iSteps = fabs(dX);
    }
    else
    {
        iSteps = fabs(dY);
    }

    xInc = dX/iSteps;
    yInc = dY/iSteps;

    x = x1;
    y = y1;
    circle(x,y,1);

    for (iCount=1; iCount<=iSteps; iCount++)
    {
        putpixel(floor(x),floor(y),iColor);
        x -= xInc;
        y -= yInc;
    }
    circle(x,y,1);
    return;
}

Wednesday, 15 January 2014

C++ Program To Bubble Search

#include<iostream.h>
#include<conio.h>
void main()
{

int a[50];
clrscr();
cout<<"enter no of elements:";
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"a["<<i<<"]:";
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]:"<<a[i]<<endl;
}
for(int j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(a[i+1]<a[i])
{
   int temp=a[i+1];
   a[i+1]=a[i];
   a[i]=temp;
}
}
}
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]:"<<a[i]<<endl;
}
getch();

}

C++ Program To Binary Search

//BINARY SEARCH
#include<iostream.h>
#include<conio.h>

void main()
{
int a[50];
clrscr();
cout<<"enter no of elements:";
int n;
cin>>n;
cout<<"enter in asceding order::";
for(int i=0;i<n;i++)
{
cout<<"a["<<i<<"]:";
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]:"<<a[i]<<endl;
}
cout<<"enter no which you want to saerch:";
int m;
cin>>m;
int fi=0;
int la=n-1;
for(;!(m>a[la]||m<a[fi]);)
{
int mid=(fi+la)/2;

if(m==a[mid])
{
     cout<<"your no at:"<<mid;
     break;
}
else if(m>a[mid])
{
fi=mid+1;
}
else
{
la=mid-1;
}
}

getch();
}

Example Of Dijkstra's algorithm

Wednesday, 8 January 2014

C Program to print Product of Digits of a given number

/*
eg. 212=2*1*2=4
eg. 313=3*1*3=9
*/

#include<stdio.h>
#include<conio.h>
void main()
{
 int no,r,pt=1;
 clrscr();
 printf("Enter a no:");
 scanf("%d",&no);
 while(no>0)
 {
  r=no%10;
  pt=pt*r;
  no=no/10;
 }
 printf("Product of Digits= %d",pt);
 getch();
}

C program to sort 10 Array elements in descending order Using Array

# include<stdio.h>
# include<conio.h>
void main()
{
int arr[10];
int i,j,temp;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
for(i=0;i<9;i++)
{
  for(j=i+1;j<10;j++)
  {
   if(arr[i]<arr[j])
   {
   temp=arr[i];
   arr[i]=arr[j];
   arr[j]=temp;
   }
}
}
printf("\nSorted array elements :\n");
for(i=0;i<10;i++)
printf("%d ",arr[i]) ;
getch();
}

C Program to Reverse print an array


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

void main()
{
int arr[10];
int i,j,temp;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
printf("\nPrinting an sorted array\n");
for(i=9;i>=0;i--)
printf("%d ",arr[i]) ;
getch();
}