Your Ad Here

Sunday, December 21, 2008

Diamond In C

/* C program to display a diamond using arrays */

#include< iostream.h>
#include< conio.h>
void main()
{
int i,j;
clrscr();
int no;
cout< < "Enter A Value";
cin>>no;
for(i=no;i>=1;i--)
{
cout< < endl;
for(int k=1;k< =i;k++)
cout< < " ";

for(j=i;j< =no;j++)
cout< < "*";

for(j=i;j< no;j++)
cout< < "*";
}
//SECOND PART

for(i=no;i>=1;i--)
{
cout< < endl;
cout< < " ";
for(int k=no;k>=i;k--)
cout<<" ";

for(j=i-1;j>=1;j--)
cout< < "*";

for(j=i-1;j>1;j--)
cout<<"*";

}
getch();
}

Call By Reference in C

/* Program for inerchanging two numbers demonstrating Call By Reference in C */

#include<>
#include<>

void swap(int *,int *);

void main()
{
int x,y;
x=15;y=20;
clrscr();
printf("x=%d,y=%d\n",x,y);
swap(&x,&y);
//printf("\n%x %x",&x,&y);
printf("\n after interchanging x=%d,y=%d\n",x,y);
getch();
}

void swap(int *u,int *v)
{
int temp;
temp=*u;
*u=*v;
*v=temp;

return;
}

Call by Values in C

/* Program on interchanging two numbers demonstrating Call By Values in C*/
#include< stdio.h>
#include< conio.h>
void main()
{
int x,y;
x=15;y=20;
clrscr();
printf("x=%d,y=%d\n",x,y);
swap(x,y);
printf("\n after interchanging x=%d,y=%d\n",x,y);
getch();
}
swap(int u,int v)
{
int temp;
temp=u;
u=v;
v=temp;
return;
}

Pointers example

/* Example of pointers in C. Thsi program uses a function to modify a string using pointers */

#include< stdio.h>
#include< conio.h>
void main()
{
void getstr(char *ptr_str,int *ptr_int);
int var=5;
char *pstr="lionel";
clrscr();
getstr(pstr,&var);
printf("The value of var after modification using pointer in a function is %d,var");
}
void getstr (char *ptr_str,int *ptr_int)
{
printf("%s\n",ptr_str);
*ptr_int=6;
getch();
}

Two Dimentional array in C

/* C program to input and display a 2-d array*/

#include< stdio.h>
#include< conio.h>
void main()
{
int num[3][3],i,j;
clrscr();
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
scanf("%d",&num[i][j]);
}
}
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
printf("\n%d",num[i][j]);
}
printf("\n");
}
getch();
}

Thursday, December 11, 2008

Carlsberg launches Web-TV channel about football and fan life.

Recently Carlsberg Brewery launched a football web-TV-channel partofthegame.tv.

They launched 5 channels showing all aspects about football from the classic football matches to life as a fan.

Be sure not to miss the video clips about football funnies and rituals from the Football Magic channel or the bizarre story about fans in the stand and how fan culture sometimes go beyond reason.
As an extra feature you can upload your own favourite football and fan moments.

Its an amazing site with loads of features present in it. So log on to partofthegame.tv and experience the diference.

Thursday, October 2, 2008

ISO - OSI Model

/* Draw ISO - OSI Model using C */

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

void main()
{
int driver, mode;
int i,j;
driver = DETECT;
initgraph(&driver,&mode,"");

outtextxy(250,450,"ISO OSI Model");
delay(2000);
outtextxy(90,30,"Sender");
delay(500);
setcolor(6);
for(i=50;i< =350;i+=50)
{
rectangle(200,i,50,i+30);
delay(500);
}
setcolor(10);
outtextxy(70,60,"Application");
delay(500);
outtextxy(70,110,"Presentation");
delay(500);
outtextxy(70,160,"Session");
delay(500);
outtextxy(70,210,"Transport");
delay(500);
outtextxy(70,260,"Network");
delay(500);
outtextxy(70,310,"Data Link");
delay(500);
outtextxy(70,360,"Physical");
delay(500);
setcolor(15);
outtextxy(390,30,"Receiver");
delay(500);
setcolor(6);
for(i=50;i< =350;i+=50)
{
rectangle(350,i,500,i+30);
delay(500);
}
setcolor(10);
outtextxy(370,60,"Application");
delay(500);
outtextxy(370,110,"Presentation");
delay(500);
outtextxy(370,160,"Session");
delay(500);
outtextxy(370,210,"Transport");
delay(500);
outtextxy(370,260,"Network");
delay(500);
outtextxy(370,310,"Data Link");
delay(500);
outtextxy(370,360,"Physical");
delay(500);
// sender Lines
line(120,80,120,100);
delay(500);
line(120,130,120,150);
delay(500);
line(120,180,120,200);
delay(500);
line(120,230,120,250);
delay(500);
line(120,280,120,300);
delay(500);
line(120,330,120,350);
delay(500);
line(120,380,120,400);
delay(500);
// Physical Connection
line(120,400,420,400);
// Receiver Lines
line(420,380,420,400);
delay(500);
line(420,330,420,350);
delay(500);
line(420,280,420,300);
delay(500);
line(420,230,420,250);
delay(500);
line(420,180,420,200);
delay(500);
line(420,130,420,150);
delay(500);
line(420,80,420,100);
//virtual connection
setcolor(15);
delay(500);
outtextxy(210,35,"Virtual Connection");
delay(1000);
for(j=65;j< =365;j+=50)
{
for(i=0;i< 15;i++)
{
setcolor(i);
line(200,j,230,j);
delay(10);
line(235,j,265,j);
delay(10);
line(270,j,300,j);
delay(10);
line(305,j,335,j);
delay(10);
line(340,j,350,j);
delay(10);
}
}
delay(500);
}
/*

*/

PC to PC Communication using RS-232

/*Aim: PC to PC Communication using RS-232 in 'C'. */

#include
#include
#define COM1 0
#define DATA_READY 0x100
#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)
int main(void)
{
int in, out, status;
bioscom(0, SETTINGS, COM1); /*initialize the port*/
cprintf("Data sent to you: ");
while (1)
{
status = bioscom(3, 0, COM1); /*wait until get a data*/
if (status & DATA_READY)
if ((out = bioscom(2, 0, COM1) & 0x7F) != 0) /*input a data*/
putch(out);
if (kbhit())
{
if ((in = getch()) == 27) /* ASCII of Esc*/
break;
bioscom(1, in, COM1); /*output a data*/
}
}
return 0;
}

/* OUTPUT:-
Data sent to you: HI...how are you??
*/

DIJKSRTRA'S ALGORITHM

/*Implementation of Shortest Path Algorithm(DIJKSRTRA's ALGORITHM) in C*/

#include< stdio.h>
#include< conio.h>
#include< process.h>
#include< string.h>
#include< math.h>
#define IN 99
#define N 6

int dijkstra(int cost[][N], int source, int target);

void main()
{
int cost[N][N],i,j,w,ch,co;
int source, target,x,y;
clrscr();
printf("\tShortest Path Algorithm(DIJKSRTRA's ALGORITHM\n\n");
for(i=1;i< N;i++)
for(j=1;j< N;j++)
cost[i][j] = IN;
for(x=1;x< N;x++)
{
for(y=x+1;y< N;y++)
{
printf("Enter the weight of the path between node %d and %d: ",x,y);
scanf("%d",&w);
cost [x][y] = cost[y][x] = w;
}
printf("\n");
}
printf("\nEnter The Source:");
scanf("%d", &source);
printf("\nEnter The target");
scanf("%d", &target);
co = dijsktra(cost,source,target);
printf("\nShortest Path: %d",co);
getch();
}

int dijsktra(int cost[][N],int source,int target)
{
int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;
char path[N];
for(i=1;i< N;i++)
{
dist[i] = IN;
prev[i] = -1;
}
start = source;
selected[start]=1;
dist[start] = 0;
while(selected[target] ==0)
{
min = IN;
m = 0;
for(i=1;i< N;i++)
{
d = dist[start] +cost[start][i];
if(d< dist[i]&&selected[i]==0)
{
dist[i] = d;
prev[i] = start;
}
if(min>dist[i] && selected[i]==0)
{
min = dist[i];
m = i;
}
}
start = m;
selected[start] = 1;
}
start = target;
j = 0;
while(start != -1)
{
path[j++] = start+65;
start = prev[start];
}
path[j]='\0';
strrev(path);
printf("%s", path);
return dist[target];
}
/***** Output *********

Shortest Path Algorithm(DIJKSRTRA's ALGORITHM

Enter the weight of the path between node 1 and 2: 2
Enter the weight of the path between node 1 and 3: 3
Enter the weight of the path between node 1 and 4: 4
Enter the weight of the path between node 1 and 5: 5

Enter the weight of the path between node 2 and 3: 5
Enter the weight of the path between node 2 and 4: 2
Enter the weight of the path between node 2 and 5: 3

Enter the weight of the path between node 3 and 4: 1
Enter the weight of the path between node 3 and 5: 4

Enter the weight of the path between node 4 and 5: 5



Enter The Source:2

Enter The target4
CE
Shortest Path: 2
*/

Calculation Of CRC

/* Calculation of CRC (Cyclic Redundancy Check)*/

#include< stdlib.h>
#include< conio.h>
#include< stdio.h>
void main()
{
int i,j,n,g,a,arr[20],gen[20],b[20],q[20],s;
clrscr();
printf("Transmitter side:");
printf("\nEnter no. of data bits:");
scanf("%d",&n);
printf("Enter data:");
for(i=0;i< n;i++)
scanf("%d",&arr[i]);

printf("Enter size of generator:");
scanf("%d",&g);
do{
printf("Enter generator:");
for(j=0;j< g;j++)
scanf("%d",&gen[j]);

}
while(gen[0]!=1);
printf("\n\tThe generator matrix:");
for(j=0;j< g;j++)
printf("%d",gen[j]);

a=n+(g-1);
printf("\n\tThe appended matrix is:");
for(i=0;i< j;++i)
arr[n+i]=0;

for(i=0;i< a;++i)

printf("%d",arr[i]);

for(i=0;i< n;++i)
q[i]= arr[i];

for(i=0;i< n;++i)
{
if(arr[i]==0)
{
for(j=i;j< g+i;++j)
arr[j] = arr[j]^0;
}
else
{
arr[i] = arr[i]^gen[0];
arr[i+1]=arr[i+1]^gen[1];
arr[i+2]=arr[i+2]^gen[2];
arr[i+3]=arr[i+3]^gen[3];
}
}
printf("\n\tThe CRC is :");
for(i=n;i < a;++i)
printf("%d",arr[i]);
s=n+a;
for(i=n;i< s;i++)
q[i]=arr[i];
printf("\n");
for(i=0;i< a;i++)
printf("%d",q[i]);
getch();
}
/* Output

Transmitter side:
Enter no. of data bits:8
Enter data:1 0 1 0 0 0 0 1
Enter size of generator:4
Enter generator:1 0 0 1

The generator matrix:1001
The appended matrix is:10100001000
The CRC is :111
10100001111
*/

Saturday, September 13, 2008

LCM and HCM of two numbers

/* C++ program to calculate LCM and HCM of two numbers */
#include< stdio.h>
#include< conio.h>
void main()
{
int a,b,c;
cout< < "Enter two nos : ;
cin>>a>>b;
c=a*b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
cout< < "HCM \t= " < < a;
cout< < "LCM \t= " < < c/a;
getch();
}

Sunday, June 1, 2008

Simple C++ program

/* C++ program on Computer Details */

#include< iostream.h>
#include< conio.h>
#define max 80

int y;
int a = 0;
int b = 0;
long c = 0;
int d,m,p;

class computer
{
private:
char s[10],x[10],h[10];

public:
void getdata()
{
cout< < "Enter Computer Type : ";
cin>>h;
cout< < endl;
cout< < "Enter CPU Type : ";
cin>>x;
cout< < endl;
cout< < "Enter The Type Of OS : ";
cin>>s;
cout< < endl;
cout< < "Enter The Memory In MB : ";
cin>>m;
cout< < endl;
cout< < "Enter The Disk Space In GB : ";
cin>>d;
cout< < endl;
cout< < "Enter Yhe Price : ";
cin>>p;
cout< < endl;
}

int compare(int i)
{
if(m>a)
{
a = m;
b = d;
c = p;
y = i;
}
if((m==a)&&(d< b))
{
a = m;
b = d;
c = p;
y = i;
}
if((m==a)&&(d==b)&&(p< c))
{
a = m;
b = d;
c = p;
y = i;
}
return y;
}

void display()
{
cout< < "The Details Of The Best Computer Are : ";
cout< < endl;
cout< < "Disk Space :"< < d;
cout< < endl;
cout< < "Memory Space :"< < m;
cout< < endl;
cout< <"Price: "< < p;
cout< < endl;
cout< < "OS : "< < s;
cout< < endl;
cout< < "CPU : "< < x;
cout< < endl;
cout< < "Computer : "< < h;
cout< < endl;
}

};
void main()
{
clrscr();
computer d[max];
int r;
cout< < "Enter The Range : ";
cin>>r;
cout< < endl;

for(int i=0;i< r;i++)
{
d[i].getdata();
}
for(int j=0;j< r;j++)
{
d[j].compare(j);
}
d[y].display();
getch();
}

Hybrid inheritance

/******** IMPLEMENTATION OF HYBRID INHERITANCE ********/

#include< iostream.h>
#include< conio.h>

class student
{
private :
int rn;
char na[20];
public:
void getdata()
{
cout< < "Enter Name And Roll No : ";
cin>>na>>rn;
}
void putdata()
{
cout< < endl< < na< < "\t"< < rn< < "\t";
}
};

class test : public student
{
protected:
float m1,m2;
public:
void gettest()
{
cout< < endl< < "Enter your marks In CP 1 And Cp 2 :";
cin>>m1>>m2;
}
void puttest()
{
cout< < m1< < "\t"< < m2< < "\t";
}
};

class sports
{
protected:
float score;
public:
void getscore()
{
cout< < endl< < "Enter your score :";
cin>>score;
}
void putscore()
{
cout< < score< < "\t";
}
};



class results : public test , public sports
{
private :
float total;
public :
void putresult()
{
total = m1+m2+score;
cout< < total;
}
};

void main()
{
results s[5];
clrscr();
for(int i=0;i< 5;i++)
{
s[i].getdata();
s[i].gettest();
s[i].getscore();
}
cout< < "______________________________________________"< < endl;
cout< < endl< < "Name\tRollno\tCP 1\tCP 2\tScore\tTotal"< < endl;
cout< < "----------------------------------------------"< < endl;
for(i=0;i< 5;i++)
{
s[i].putdata();
s[i].puttest();
s[i].putscore();
s[i].putresult();
}
cout< < endl< < "----------------------------------------------";
getch();
}


/********** OUTPIUT **********
Enter Name And Roll No : Lionel
1

Enter your marks In CP 1 And Cp 2 :45

Enter your score :45
Enter Name And Roll No : Cyril
65

Enter your marks In CP 1 And Cp 2 :49
42

Enter your score :45
Enter Name And Roll No : Mayank
40

Enter your marks In CP 1 And Cp 2 :41
43

Enter your score :46
Enter Name And Roll No : Dylan
78

Enter your marks In CP 1 And Cp 2 :23
24

Enter your score :25
Enter Name And Roll No : Ansha
39

Enter your marks In CP 1 And Cp 2 :45
45

Enter your score :45
______________________________________________

Name Rollno CP 1 CP 2 Score Total
----------------------------------------------

Lionel 1 45 23 45 113
Cyril 65 49 42 45 136
Mayank 40 41 43 46 130
Dylan 78 23 24 25 72
Ansha 39 45 45 45 135
----------------------------------------------
*/

Multilevel Inheritance

/********* IMPLEMENTATION OF MULTILEVEL INHERITANCE *********/

#include< iostream.h>
#include< conio.h>

class student // Base Class
{
protected:
int rollno;
char *name;
public:
void getdata(int b,char *n)
{
rollno = b;
name = n;
}
void putdata(void)
{
cout< < " The Name Of Student \t: "< < name< < endl;
cout< < " The Roll No. Is \t: "< < rollno< < endl;
}
};

class test:public student // Derieved Class 1
{
protected:
float m1,m2;
public:
void gettest(float b,float c)
{
m1 = b;
m2 = c;
}
void puttest(void)
{
cout< < " Marks In CP Is \t: "< < m1< < endl;
cout< < " Marks In Drawing Is \t: "< < m2< < endl;
}
};

class result:public test // Derieved Class 2
{
protected:
float total;
public:
void displayresult(void)
{
total = m1 + m2;
putdata();
puttest();
cout< < " Total Of The Two \t: "< < total< < endl;
}
};

void main()
{
clrscr();
int x;
float y,z;
char n[20];
cout< < "Enter Your Name:";
cin>>n;
cout< < "Enter The Roll Number:";
cin>>x;
result r1;
r1.getdata(x,n);
cout< < "ENTER COMPUTER PROGRAMMING MARKS:";
cin>>y;
cout< < "ENTER DRAWING MARKS:";
cin>>z;
r1.gettest(y,z);
cout< < endl< < endl< < "************ RESULT **************"< < endl;
r1.displayresult();
cout< < "**********************************"< < endl;
getch();

}
/************ OUTPUT ************
Enter Your Name:Lionel
Enter The Roll Number:44
ENTER COMPUTER PROGRAMMING MARKS:95
ENTER DRAWING MARKS:90


************ RESULT **************
The Name Of Student : Lionel
The Roll No. Is : 44
Marks In CP Is : 95
Marks In Drawing Is : 90
Total Of The Two : 185
**********************************
*/

OPERATOR OVERLOADING - Unary

/********* IMPLEMENTATION OF OPERATOR OVERLOADING (UNARY)*********/
#include< iostream.h>
#include< conio.h>

class unary
{
private:
int x,y,z;
public:

unary(void)
{
cout< < "Enter Any Three Integer Nos. : ";
cin>>x>>y>>z;
}

void display(void)
{
cout< < endl< < " The Three Nos. Are : "< < x< < " , "< < y< < " , "< < z;
}

void operator --()
{
x = --x;
y = --y;
z = --z;
}

void operator ++()
{
x = ++x;
y = ++y;
z = ++z;
}
};

void main()
{
clrscr();
unary s;
s.display();

--s;
cout< < endl< < endl< < endl< < "******* The Decremented Values ********"< < endl;
s.display();

++s;
cout< < endl< < endl< < endl< < "******* The Incremented Values ********"< < endl;
s.display();
cout< < endl< < endl< < "***************************************";

getch();
}



/************ OUTPUT **************
Enter Any Three Integer Nos. : 4
-8
6

The Three Nos. Are : 4 , -8 , 6


******* The Decremented Values ********

The Three Nos. Are : 3 , -9 , 5


******* The Incremented Values ********

The Three Nos. Are : 4 , -8 , 6

***************************************
*/

Bubble Sort

/**** C Program For Implementation Of Bubble Sort. Sorting names entered by the user *****/
#include< stdio.h>
#include< conio.h>
#define MAX 10

char name[MAX][15];
void sort(int n)
{
int pa,cp,i,j,k,kk=0;
char temp[15];
pa=n-1;
cp=n-1;
for(i=1;i< =pa;i++)
{
for(j=1;j< =cp;j++)
{
kk=kk+1;
if(strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
}
printf("\n List after %d pass is ",i);
for(k=1;k< =n;k++)
printf("\n\t\t %s",name[k]);
getch();
}
clrscr();
printf("\n\t\t Total Comparisions Done : %d",kk);
}

void main()
{
int n,i,j;
clrscr();
printf("Enter How Many Names : ");
scanf("%d",&n);
if(n>MAX)
printf("\n\t\tArray Size IS Only %d",MAX);
else
{
printf("\n\t\tEnter %d Names :\n",n);
for(i=1;i< =n;i++)
{
printf("\t\t");
scanf("%s",name[i]);
}
sort(n);
printf("\n\n\t\tSorted List ");
for(i=1;i< =n;i++)
printf("\n\t\t%s",name[i]);
}
getch();
}
/********************* OUTPUT *******************
Enter How Many Names : 4

Enter 4 Names :
Malcolm
Lionel
Mayank
Pinto

List after 1 pass is
Lionel
Malcolm
Mayank
Pinto
List after 2 pass is
Lionel
Malcolm
Mayank
Pinto
List after 3 pass is
Lionel
Malcolm
Mayank
Pinto

*/

Binary Search Tree

/* C Program on binary Search Tree */

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

struct node
{
char data[15];
struct node *left,*right;
};

void insert(struct node *r,struct node *p)
{
if((r->right==NULL)&&(strcmp(p->data,r->data)>0))
r->right=p;
else if((r->right!=NULL)&&(strcmp(p->data,r->data)>0))
insert(r->right,p);
if((r->left==NULL)&&(strcmp(p->data,r->data)< 0))
r->left=p;
else if((r->left!=NULL)&&(strcmp(p->data,r->data)< 0))
insert(r->left,p);

}

void tree(struct node *r,int c)
{
int top,flag;
struct node *w,*stack[20];
if(r!=NULL)
{
if(c!=4)
{
if(c == 1)
printf(" %s ",r->data);
tree(r->left,c);
if(c == 2)
printf(" %s ",r->data);
tree(r->right,c);
if(c == 3)
printf(" %s ",r->data);
}
}
if(c == 4)
{
top = 0;
w = r;
flag = 0;
while((top != -1)&&(w!=NULL))
{
while((flag == 0) && (w->left!=NULL))
{
stack[top] = w;
top++;
w = w->left;
}
printf(" %s ",w->data);
if(w->right != NULL)
{
w = w->right;
flag = 0;
}
else
{
top--;
w = stack[top];
flag = 1;
}
}
}
}
void main()
{
int choice,c,i,flag;
char temp='N',temp1[15];
struct node *s,*root,*r,*q;
root = NULL;
do
{
clrscr();
printf("\n 1. Enter");
printf("\n 2. Delete ");
printf("\n 3. Search ");
printf("\n 4. Display");
printf("\n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("***** Data Entry ***** ");
do
{
s=malloc(sizeof(struct node));
s->left=NULL;
s->right=NULL;
printf("\nEnter Data : ");
scanf("%s",&s->data);
if(root==NULL)
root=s;
else
insert(root,s);
printf("\nEnter Your Elements[y/n] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;

case 2:printf("****** Delete Operation *******\n");
do
{
printf("\nEnter Element To Be Deleted : ");
scanf("%s",temp1);
s=root;i=0;flag=0;
do
{
if(strcmp(s->data,temp1)>0)
{
r=s;
s=s->left;
i=2;
}
if(strcmp(s->data,temp1)==0)
{
flag=1;
if(i==0)
{
if(root->right!=NULL)
{
q=root->left;
root=root->right;
insert(root,q);
}
if(root->right==NULL)
root=root->left;
}
else
{
if(i==1)
{
q=s->left;
r->right=s->right;
if(s->left!=NULL)
insert(r,q);
}
if(i==2)
{
q=s->right;
r->left=s->left;
if(s->right!=NULL)
insert(r,q);
}
}
}
}
while(flag==0&&s!=NULL);
printf("\n Delete Any More[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;
case 3:printf("****** Search Operation *******\n");
do
{
printf("\n Enter Name To Be Searched");
scanf("%s",temp1);
i=0;
s=root;
while(s!=NULL&&i==0)
{
if(strcmp(s->data,temp1)< 0)
s=s->right;
if(strcmp(s->data,temp1)>0)
s=s->left;
if(strcmp(s->data,temp1)==0)
i=1;
}
if(i==0)
printf("\nElement Not Found\n");
else
printf("\nElement Found\n");
printf("\nEnter More Elements[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;
case 4:clrscr();
do
{
clrscr();
printf("\n 1. Preorder\n 2. Inorder \n 3. Postorder \n 4. Non Recursion \n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&c);
if(root==NULL)
printf("Tree Not Started Yet");
else
tree(root,c);
printf("\n Press Any Key To Continue......");
getch();
}
while(c!=5);
break;
}
}
while(choice!=5);
}
/****************** OUTPUT ****************

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 1
/****** Data Entry *******

Enter Data : Lionel
Enter More Elements(Y/N) : y
Enter Data : Dylan
Enter More Elements(Y/N) : y
Enter Data : Daniel
Enter More Elements(Y/N) : y
Enter Data : Malcolm
Enter More Elements(Y/N) : y
Enter Data : Cyril
Enter More Elements(Y/N) : y
Enter Data : Jason
Enter More Elements(Y/N) : n

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 4

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 1
Lionel Dylan Malcolm Cyril Daniel Jason

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 2
Malcolm Cyril Dylan Lionel Jason Daniel

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 3
Cyril Malcolm Dylan Lionel Jason Daniel

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 4
Malcolm Cyril Dylan Lionel Jason Daniel


Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 5

Press Any Key To Continue.....

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 3

Enter Name To Be Searched : Dylan
Element Found

Enter More Elements(Y/N) : n

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 2

****** Delete Operation *******
Enter Element To Be Deleted : Dylan
Delete Any More(Y/n) : n

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 5

*/

Heap Sort

/************* C Program To Sort An Array Using Heap Sort *************/

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

void swap(int *x,int *y)
{
int temp;
temp=*x;
*x = *y;
*y = temp;
}

void heapsort(int a[],int n)
{
int i,s,f;
for(i=1;i< n;++i)
{
s=i;
f=(s-1)/2;
while( a[f]< a[s])
{
swap(&a[f],&a[s]);
s=f;
if(s==0)
break;
f=(s-1)/2;
}
}
for(i=n-1;i>=1;--i)
{
swap(&a[0],&a[i]);
f=0;
s=1;
if(i==1)
break;
if(i>2)if(a[2]>a[1])s=2;
while( a[f]< a[s] )
{
swap(&a[f],&a[s]);
f=s;
s=2*f+1;
if(i>s+1 )if(a[s+1]>a[s])s=s+1;
if (s>=i)
break;
}
}
}


void main()
{
int a[100],n,i;
clrscr();
printf("\t\tHEAP SORT\n");
printf("\nEnter The Number Of Elements\t: ");
scanf("%d",&n);
printf("\nEnter Elements\n");
for(i=0;i< n;++i)
scanf("%d",&a[i]);
heapsort(a,n);
printf("\n\t\t\tSorted List\n");
for(i=0;i< n;++i)
printf("\t%d",a[i]);
getche();
}
/***************** OUTPUT ******************
HEAP SORT

Enter The Number Of Elements : 6

Enter Elements
45
12
3
1
78
6

Sorted List
1 3 6 12 45 78
*/

Krushkal's Algorithm - 2

/* C Program on Krushkal's Algorithm */

#include< stdio.h>
#include< stdlib.h>
void main()
{
int graph[15][15],s[15],pathestimate[15],mark[15];
int num_of_vertices,source,i,j,u,predecessor[15];
int count=0;
int minimum(int a[],int m[],int k);
void printpath(int,int,int[]);
printf("\nEnter The No. Of Vertices\n");
m scanf("%d",&num_of_vertices);
if(num_of_vertices< =0)
{
printf("\nThis Is Meaningless\n");
exit(1);
}
printf("\nEnter The Adjacent Matrix\n");
for(i=1;i< =num_of_vertices;i++)
{
printf("\nEnter The Elements Of Row %d\n",i);
for(j=1;j< =num_of_vertices;j++)
scanf("%d",&graph[i][j]);
}
printf("\nEnter The Source Vertex\n");
scanf("%d",&source);
for(j=1;j< =num_of_vertices;j++)
{
mark[j] = 0;
pathestimate[j] = 999;
predecessor[j] = 0;
}
pathestimate[source]=0;

while(count< num_of_vertices)
{
u = minimum(pathestimate,mark,num_of_vertices);
s[++count] = u;
mark[u] = 1;
for(i=1;i< =num_of_vertices;i++)
{
if(graph[u][i]>0)
{
if(mark[i]!=1)
{
if(pathestimate[i]>pathestimate[u]+graph[u][i])
{
pathestimate[i] = pathestimate[u]+graph[u][i];
predecessor[i] = u;
}
}
}
}
}
for(i=1;i< =num_of_vertices;i++)
{
printpath(source,i,predecessor);
if(pathestimate[i]!=999)
printf("->(%d)\n",pathestimate[i]);
}
}

int minimum(int a[],int m[],int k)
{
int mi=999;
int i,t;
for(i=1;i< =k;i++)
{
if(m[i]!=1)
{
if(mi>=a[i])
{
mi = a[i];
t = i;
}
}
}
return t;
}

void printpath(int x,int i,int p[])
{
printf("\n");
if(i==x)
printf("%d",x);
else if(p[i]==0)
printf("Number Path From %d To %d",x,i);
else
{
printpath(x,p[i],p);
printf("..%d",i);
}
}
/****************

Enter The No. Of Vertices
6

Enter The Adjacent Matrix

Enter The Elements Of Row 1
0 1 1 1 0 0

Enter The Elements Of Row 2
0 0 1 0 1 0

Enter The Elements Of Row 3
0 0 0 1 1 1

Enter The Elements Of Row 4
0 0 0 0 0 1

Enter The Elements Of Row 5
0 0 0 0 0 1

Enter The Elements Of Row 6
0 0 0 0 0 0

Enter The Source Vertex
1

1->(0)


1..2->(1)


1..3->(1)


1..4->(1)



1..3..5->(2)



1..4..6->(2)

*/

Krushkal's Algorithm

/* C Program On Krushkal's Algorithm */

#include < stdio.h>
#include < conio.h>
typedef struct
{
int node1;
int node2;
int wt;
}edge;

void sortedges(edge a[],int n)
{
int i,j;
edge temp;
for(i=0;i< n-1;++i)
for(j=i+1;j< n;++j)
if(a[i].wt>a[j].wt){temp=a[i];a[i]=a[j];a[j]=temp;}
}

int checkcycle(int p[],int i,int j)
{
int v1,v2;
v1 = i;
v2 = j;
while(p[i]>-1)
i = p[i];
while(p[j]>-1)
j = p[j];
if(i!=j)
{
p[j]=i;
printf("%d %d\n",v1,v2);
return 1;
}
return 0;
}
void main()
{
edge e[100];
int parent[100];
int n,i,j,m,k = 1,cost = 0;
clrscr();
printf("KRUSKAL's ALGORITHM\n");
printf("Enter number of nodes\n");
scanf("%d",&n);
for(i=0;i< n;++i)
parent[i]=-1;
i = 0;
printf("Enter number of edges\n");
scanf("%d",&m);
for(i=0;i< m;++i)
{
printf("enter an edge and wt\n");
scanf("%d %d %d", &e[i].node1,&e[i].node2,&e[i].wt);
}
sortedges(e,m);
printf("\n\nEdges of the tree\n");
i = 0;
while(k< n)
{
if(checkcycle(parent,e[i].node1,e[i].node2))
{
k++;
cost=cost+e[i].wt;
i++;
}
}
printf("cost = %d",cost);
getch();
}

Radix Sort Algorithm

/* C program to sort an array using radix sort LINKED LIST implementation*/

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

void radix(int a[],int n,int m)
{
typedef struct node
{
int data;
struct node * next;
}NODE;

NODE * ptr,*start,*prev;
NODE *front[10], *rear[10];
int k=1,i,j,y,p;;
/*creating initial linked list*/
start=NULL;
for(i=0;i< n;++i)
{
ptr=(NODE *)malloc(sizeof(NODE));
ptr->data=a[i];
ptr->next=NULL;
if(start==NULL)
start=ptr;
else
prev->next=ptr;
prev=ptr;
}

/*radix sort*/

for(i=1;i< =m;++i)
{
for(j=0;j< 10;++j)
front[j]=NULL;
/*placing elements into queues*/
ptr=start;
while(ptr!=NULL)
{y=ptr->data/k %10;/*y is the digit*/
if(front[y]==NULL)
{
front[y]=ptr;
rear[y]=ptr;
}
else
{
rear[y]->next=ptr;
rear[y]=ptr;
}

ptr=ptr->next;
}

start=NULL;
for(j=0;j< 10;++j)
if(front[j]!=NULL)
{
if(start==NULL)
start=front[j];
else rear[p]->next=front[j];
p=j;
}
rear[p]->next=NULL;
k=k*10;
}
/*copying back to array*/
ptr=start;
for(i=0;i< n;++i,ptr=ptr->next)
a[i]=ptr->data;

}

void main()
{
int a[100],n,i,m;
char temp;
do
{
clrscr();
printf("===========================RADIX SORT===========================================\n");
printf("ENTER NUMBER OF NUMBERS AND NUMBER OF DIGITS\n");
scanf("%d%d",&n,&m);
printf("ENTER ELEMENTS\n");
for(i=0;i< n;++i)
scanf("%d",&a[i]);
radix(a,n,m);
printf("SORTED LIST\n");
for(i=0;i< n;++i)
printf("%d ",a[i]);
printf("\nDO YOU wish to continue?[y/n]\n");
scanf("%c",&temp);


}while(temp=='y'|| temp=='Y');
printf("\n---------------------------------------------------------------------------------\n");
getch();
}
/*OUTPUT:
===========================RADIX SORT===========================================

Enter number of numbers and number of digits
4
2
enter elements
25
65
35
45
sorted list
25 35 45 65
Do you wish to continue?[y/n]
n
--------------------------------------------------------------------------------*/

Monday, March 31, 2008

Circle Through Three Points

Program In C Language for Circle Through Three Points

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

int main()
{
clrscr();
double f,g,m,x1,x2,x3,y1,y2,y3;
double c,d,h,e,k,r,s;
for(;;)
{
if(scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)==EOF)
//checking for input
break;

f = x3*x3-x3*x2-x1*x3+x1*x2+y3*y3-y3*y2-y1*y3+y1*y2; //formula
g = x3*y1-x3*y2+x1*y2-x1*y3+x2*y3-x2*y1;

if(g==0)
m = 0;
else
m = (f/g);

c = (m*y2)-x2-x1-(m*y1); //formula
d = (m*x1)-y1-y2-(x2*m);
e = (x1*x2)+(y1*y2)-(m*x1*y2)+(m*x2*y1);

h = (c/2); //formula
k = (d/2);
s = (((h)*(h))+((k)*(k))-e);
r = pow(s,.5);

printf("(x");

if(h>=0)
printf(" + ");
else if(h< 0)
printf(" - ");
if(h< 0)
h=-h;
printf("%.3lf)^2",(h));
printf(" + ");
printf("(y");
if(k>=0)
printf(" + ");
else if(k< 0)
printf(" - ");
if(k< 0)
k=-k;
printf("%.3lf)^2 = %.3lf^2",(k),r);

printf("");

printf("x^2 + y^2");

if(c>=0) printf(" + ");
else if(c< 0) printf(" - ");

if(c< 0) c=-c;
printf("%.3lfx",c);

if(d>=0) printf(" + ");
else if(d< 0) printf(" - ");

if(d< 0) d=-d;
printf("%.3lfy",d);

if(e>=0) printf(" + ");
else if(e< 0) printf(" - ");

if(e< 0) e=-e;
printf("%.3lf = 0",e);
printf("");
}

getch();
return 0;
}

Decimal To Roman Conversion

/* Program in C for Decimal to Roman Number conversion */

#include< stdio.h>

main()
{
int a,b,c,d,e;
clrscr();
printf("Input a number (between 1-3000):");
scanf("%d",&e);
while (e==0||e>3000)
{
printf ("ERROR: Invalid Input!
");
printf ("Enter the number again:");
scanf ("%d",&e);
}
if (e>3000)
printf("Invalid");
a = (e/1000)*1000;
b = ((e/100)%10)*100;
c = ((e/10)%10)*10;
d = ((e/1)%10)*1;

if (a ==1000)
printf("M");
else if (a ==2000)
printf("MM");
else if (a ==3000)
printf("MMM");

if (b == 100)
printf("C");
else if (b == 200)
printf("CC");
else if (b == 300)
printf("CCC");
else if (b == 400)
printf("CD");
else if (b ==500)
printf("D");
else if (b == 600)
printf("DC");
else if (b == 700)
printf("DCC");
else if (b ==800)
printf("DCCC");
else if (b == 900)
printf("CM");


if (c == 10)
printf("X");
else if (c == 20)
printf("XX");
else if (c == 30)
printf("XXX");
else if (c == 40)
printf("XL");
else if (c ==50)
printf("L");
else if (c == 60)
printf("LX");
else if (c == 70)
printf("LXX");
else if (c ==80)
printf("LXXX");
else if (c == 90)
printf("XC");

if (d == 1)
printf("I");
else if (d == 2)
printf("II");
else if (d == 3)
printf("III");
else if (d == 4)
printf("IV");
else if (d ==5)
printf("V");
else if (d == 6)
printf("VI");
else if (d == 7)
printf("VII");
else if (d ==8)
printf("VIII");
else if (d == 9)
printf("IX");
getch();
}

Wednesday, February 27, 2008

Shuttle Sort - Simple Insertion Sort

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

void shutsort(int a[],int n)
{
int j,i=1,mid;

while(i< n)
{
j=i-1;
while(j>=0)
{
if(a[j]>a[j+1])
{
mid = a[j];
a[j] = a[j+1];
a[j+1]=mid;
j--;
}
else
break;
}
i++;
}
}

main()
{
int a[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
shutsort(a,n);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

/* OUTPUT
Enter The number Of Elements : 5

Element 1 : 21

Element 2 : 36

Element 3 : 54

Element 4 : 98

Element 5 : 1

Array Befor Sorting : 21 36 54 98 1
Array After Sorting : 1 21 36 54 98
*/

Shell Sort

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

void shellsort(int a[],int n)
{
int j,i,k,m,mid;
for(m = n/2;m>0;m/=2)
{
for(j = m;j< n;j++)
{
for(i=j-m;i>=0;i-=m)
{
if(a[i+m]>=a[i])
break;
else
{
mid = a[i];
a[i] = a[i+m];
a[i+m] = mid;
}
}
}
}
}

main()
{
int a[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
shellsort(a,n);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

/* OUTPUT

Enter The number Of Elements : 5

Element 1 : 21

Element 2 : 36

Element 3 : 54

Element 4 : 2

Element 5 : 0

Array Befor Sorting : 21 36 54 2 0
Array After Sorting : 0 2 21 36 54
*/

Merge Sort

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

void mergesort(int a[],int n)
{
int b[50],c,low1,high1,high2,low2;
int i,k,j;
c=1;
while(c< n)
{
low1=0;
k=0;
while(low1+c< n)
{
low2=low1+c ;
high1=low2-1;
if(low2+c-1< n)
high2=low2+c-1;
else
high2=n-1;
i=low1;
j=low2;
while(i< =high1 && j< =high2)
{
if(a[i]< =a[j])
b[k++] =a[i++];
else
b[k++] = a[j++];

}
while(i< =high1)
b[k++]=a[i++];
while(j< =high2)
b[k++] =a[j++];
low1=high2+1;
}
i=low1;
while(k< n)
b[k++] =a[i++];
for(i=0;i< n;i++)
a[i]=b[i];
c=c*2;
}
}

main()
{
int a[20],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
mergesort(a,n);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

/* OUTPUT

Enter The number Of Elements : 10

Element 1 : 12

Element 2 : 54

Element 3 : 98

Element 4 : 6566

Element 5 : 45

Element 6 : 12

Element 7 : 5

Element 8 : 1

Element 9 : 156

Element 10 : 21

Array Befor Sorting : 12 54 98 6566 45 12 5 1 156 21
Array After Sorting : 1 5 12 12 21 45 54 98 156 6566


*/

Quicksort

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

void quicksort(int [],int,int);
int partition(int [],int,int);


main()
{
int a[20],p,q,i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

p=0;
q=n-1;
printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
quicksort(a,p,q);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}


void quicksort(int a[],int p,int q)
{
int j;
if(p< q)
{
j=partition(a,p,q+1);
quicksort(a,p,j-1);
quicksort(a,j+1,q);
}
}

int partition(int a[],int m,int p)
{
int v,i,j;
int temp;
v=a[m];
i=m;j=p;
do
{
do
{
i += 1;
}
while(a[i]< v);
do
{
j -= 1;
}
while(a[j]>v);

if(i< j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
while(i< j);
a[m] =a[j];
a[j] = v;
return j;
}

/* OUTPUT

Enter The number Of Elements : 10

Element 1 : 12

Element 2 : 54

Element 3 : 98

Element 4 : 6566

Element 5 : 45

Element 6 : 12

Element 7 : 5

Element 8 : 1

Element 9 : 156

Element 10 : 21

Array Befor Sorting : 12 54 98 6566 45 12 5 1 156 21
Array After Sorting : 1 5 12 12 21 45 54 98 156 6566


*/

Monday, February 25, 2008

Simple Selection Sort

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

void simplesel(int a[],int b[],int n)
{
int j,i=0,k,H;
int L=32760;
k=0;
L=H;
while(i< n)
{
for(j=0;j< n;j++)
{
if(a[j]< L)
{
L = a[j];
k = j;
}
}
a[k] = H;
b[i] = L;
L = H;
i++;
}
}

main()
{
int a[10],b[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
simplesel(a,b,n);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",b[i]);
getch();
return 0;
}
/* OUTPUT
Enter The number Of Elements : 10

Element 1 : 21

Element 2 : 25

Element 3 : 63

Element 4 : 45

Element 5 : 1

Element 6 : 147

Element 7 : 10

Element 8 : 78

Element 9 : 96

Element 10 : 5
Array Befor Sorting : 21 25 63 45 1 147 10 78 96 5
Array After Sorting : 1 5 10 21 25 45 63 78 96 147
*/

Tuesday, February 12, 2008

Factorial

/* C++ program for calculating factorial of a number entered by the user */

# include < iostream.h>
# include < conio.h>
void fact(int);

void main()
{
clrscr();
int n;
cout < < "Enter limit : ";
cin>>n;
fact(n);
getch();
}
void fact(int x)
{
char a;
int i=1;
for(int j=1;j< =x;j++)
{
i*=j;
}
cout< < "Answer = "< < i< < endl;
cout< < "Want to continue ( y )/or press any key to continue ";
cin>>a;
if(a=='y')
{
main();
}
else
{
cout< < "Good Bye ";
}
getch();
}

Sunday, February 10, 2008

Huffman Coding

/* Huffman Coding in C . This program reads a text file named on the command line, then compresses it using Huffman coding. The file is read twice, once to determine the frequencies of the characters, and again to do the actual compression. */

#include < stdio.h>
#include < stdlib.h>
#include < string.h>
#include < time.h>

/* there are 256 possible characters */

#define NUM_CHARS 256

/* tree node, heap node */

typedef struct _treenode treenode;
struct _treenode {
int freq; /* frequency; is the priority for heap */
unsigned char ch; /* character, if any */
treenode *left, /* left child of Huffman tree (not heap!) */
*right; /* right child of Huffman tree */
};

/* this is a priority queue implemented as a binary heap */
typedef struct _pq {
int heap_size;
treenode *A[NUM_CHARS];
} PQ;

/* create an empty queue */

void create_pq (PQ *p) {
p->heap_size = 0;
}

/* this heap node's parent */

int parent (int i) {
return (i-1) / 2;
}

/* this heap node's left kid */

int left (int i) {
return i * 2 + 1;
}

/* this heap node's right kid */

int right (int i) {
return i * 2 + 2;
}

/* makes the subheap with root i into a heap , assuming left(i) and
* right(i) are heaps
*/
void heapify (PQ *p, int i) {
int l, r, smallest;
treenode *t;

l = left (i);
r = right (i);

/* find the smallest of parent, left, and right */

if (l < p->heap_size && p->A[l]->freq < p->A[i]->freq)
smallest = l;
else
smallest = i;
if (r < p->heap_size && p->A[r]->freq < p->A[smallest]->freq)
smallest = r;

/* swap the parent with the smallest, if needed. */

if (smallest != i) {
t = p->A[i];
p->A[i] = p->A[smallest];
p->A[smallest] = t;
heapify (p, smallest);
}
}

/* insert an element into the priority queue. r->freq is the priority */
void insert_pq (PQ *p, treenode *r) {
int i;

p->heap_size++;
i = p->heap_size - 1;

/* we would like to place r at the end of the array,
* but this might violate the heap property. we'll start
* at the end and work our way up
*/
while ((i > 0) && (p->A[parent(i)]->freq > r->freq)) {
p->A[i] = p->A[parent(i)];
i = parent (i);
}
p->A[i] = r;
}

/* remove the element at head of the queue (i.e., with minimum frequency) */
treenode *extract_min_pq (PQ *p) {
treenode *r;

if (p->heap_size == 0) {
printf ("heap underflow!\n");
exit (1);
}

/* get return value out of the root */

r = p->A[0];

/* take the last and stick it in the root (just like heapsort) */

p->A[0] = p->A[p->heap_size-1];

/* one less thing in queue */

p->heap_size--;

/* left and right are a heap, make the root a heap */

heapify (p, 0);
return r;
}

/* read the file, computing the frequencies for each character
* and placing them in v[]
*/
unsigned int get_frequencies (FILE *f, unsigned int v[]) {
int r, n;

/* n will count characters */

for (n=0;;n++) {

/* fgetc() gets an unsigned char, converts to int */

r = fgetc (f);

/* no more? get out of loop */

if (feof (f)) break;

/* one more of this character */

v[r]++;
}
return n;
}

/* make the huffman tree from frequencies in freq[] (Huffman's Algorithm) */

treenode *build_huffman (unsigned int freqs[]) {
int i, n;
treenode *x, *y, *z;
PQ p;

/* make an empty queue */

create_pq (&p);

/* for each character, make a heap/tree node with its value
* and frequency
*/
for (i=0; i< NUM_CHARS; i++) {
x = malloc (sizeof (treenode));

/* its a leaf of the Huffman tree */

x->left = NULL;
x->right = NULL;
x->freq = freqs[i];
x->ch = (char) i;

/* put this node into the heap */

insert_pq (&p, x);
}

/* at this point, the heap is a "forest" of singleton trees */

n = p.heap_size-1; /* heap_size isn't loop invariant! */

/* if we insert two things and remove one each time,
* at the end of heap_size-1 iterations, there will be
* one tree left in the heap
*/
for (i=0; i< n; i++) {

/* make a new node z from the two least frequent
* nodes x and y
*/
z = malloc (sizeof (treenode));
x = extract_min_pq (&p);
y = extract_min_pq (&p);
z->left = x;
z->right = y;

/* z's frequency is the sum of x and y */

z->freq = x->freq + y->freq;

/* put this back in the queue */

insert_pq (&p, z);
}

/* return the only thing left in the queue, the whole Huffman tree */

return extract_min_pq (&p);
}

/* traverse the Huffman tree, building up the codes in codes[] */

void traverse (treenode *r, /* root of this (sub)tree */
int level, /* current level in Huffman tree */
char code_so_far[], /* code string up to this point in tree */
char *codes[]) {/* array of codes */

/* if we're at a leaf node, */

if ((r->left == NULL) && (r->right == NULL)) {

/* put in a null terminator */

code_so_far[level] = 0;

/* make a copy of the code and put it in the array */

codes[r->ch] = strdup (code_so_far);
} else {

/* not at a leaf node. go left with bit 0 */

code_so_far[level] = '0';
traverse (r->left, level+1, code_so_far, codes);

/* go right with bit 1 */

code_so_far[level] = '1';
traverse (r->right, level+1, code_so_far, codes);
}
}

/* global variables, a necessary evil */

int nbits, current_byte, nbytes;

/* output a single bit to an open file */

void bitout (FILE *f, char b) {

/* shift current byte left one */

current_byte < < = 1;

/* put a one on the end of this byte if b is '1' */

if (b == '1') current_byte |= 1;

/* one more bit */

nbits++;

/* enough bits? write out the byte */

if (nbits == 8) {
fputc (current_byte, f);
nbytes++;
nbits = 0;
current_byte = 0;
}
}

/* using the codes in codes[], encode the file in infile, writing
* the result on outfile
*/
void encode_file (FILE *infile, FILE *outfile, char *codes[]) {
unsigned char ch;
char *s;

/* initialize globals for bitout() */

current_byte = 0;
nbits = 0;
nbytes = 0;

/* continue until end of file */

for (;;) {

/* get a char */

ch = fgetc (infile);
if (feof (infile)) break;

/* put the corresponding bitstring on outfile */

for (s=codes[ch]; *s; s++) bitout (outfile, *s);
}

/* finish off the last byte */

while (nbits) bitout (outfile, '0');
}

/* main program */

int main (int argc, char *argv[]) {
FILE *f, *g;
treenode *r; /* root of Huffman tree */
unsigned int n, /* number of bytes in file */
freqs[NUM_CHARS]; /* frequency of each char */
char *codes[NUM_CHARS], /* array of codes, 1 per char */
code[NUM_CHARS], /* a place to hold one code */
fname[100]; /* what to call output file */

/* hassle user */

if (argc != 2) {
fprintf (stderr, "Usage: %s < filename>\n", argv[0]);
exit (1);
}

/* set all frequencies to zero */

memset (freqs, 0, sizeof (freqs));

/* open command line argument file */

f = fopen (argv[1], "r");
if (!f) {
perror (argv[1]);
exit (1);
}

/* compute frequencies from this file */

n = get_frequencies (f, freqs);
fclose (f);

/* make the huffman tree */

r = build_huffman (freqs);

/* traverse the tree, filling codes[] with the codes */

traverse (r, 0, code, codes);

/* name the output file something.huf */

sprintf (fname, "%s.huf", argv[1]);
g = fopen (fname, "w");
if (!g) {
perror (fname);
exit (1);
}

/* write frequencies to file so they can be reproduced */

fwrite (freqs, NUM_CHARS, sizeof (int), g);

/* write number of characters to file as binary int */

fwrite (&n, 1, sizeof (int), g);

/* open input file again */

f = fopen (argv[1], "r");
if (!f) {
perror (argv[1]);
exit (1);
}

/* encode f to g with codes[] */

encode_file (f, g, codes);
fclose (f);
fclose (g);
/* brag */
printf ("%s is %0.2f%% of %s\n",
fname, (float) nbytes / (float) n, argv[1]);
exit (0);
}

Operator Overloading

/* C++ Program for the IMPLEMENTATION OF OPERATOR OVERLOADING(BINARY). The Given Program performs Basic Arithematic operation : Addition, Subtraction, Multiplication and Division for Two Complex Numbers. */

#include< iostream.h>
#include< conio.h>
#include< process.h>
class complex
{
float real;
float imag;
public:
complex()
{}
complex(float x,float y)
{
real=x;
imag=y;
}
complex operator + (complex);
complex operator - (complex);
complex operator * (complex);
complex operator / (complex);
void display(void)
{
cout< < real< < " +i" < < i mag< < endl;
}

};

complex complex :: operator +(complex c)
{
complex c2;
c2.real=real+c.real;
c2.imag=imag+c.imag;
return (c2);
}

complex complex :: operator -(complex c)
{
complex c2;
c2.real=real-c.real;
c2.imag=imag-c.imag;
return (c2);
}
complex complex :: operator *(complex c)
{
complex c2;
c2.real = ((real * c.real) - (imag * c.imag));
c2.imag = ((real * c.imag) + (imag * c.imag));
return (c2);
}
complex complex :: operator /(complex c)
{
complex c2;
c2.real=((real * c.real) + (imag * c.imag))/((real * c.real) + (imag * c.imag));
c2.imag=((imag * c.real) - (real * c.imag))/((real * c.real) + (imag * c.imag));
return (c2);
}

void main()
{
clrscr();
complex c1,c2,c3;
int op;
char ch,y,Y;

c1 = complex(5.6,2.7);
c2 = complex(3.5,5.6);
cout< < "Two Complex numbers Are :"< < endl;
c1.display();
c2.display();
do
{
cout< < endl< < "******** MENU *********"< < endl;
cout< < "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit"< < endl;
cout< < "Enter Your Choice : ";
cin>>op;
switch(op)
{
case 1:
c3 = c1 + c2;
cout< < "Addition of Two complex Nos. :";
c3.display();
break;

case 2:
c3 = c1 - c2;
cout< < "Subtraction of Two complex Nos. :";
c3.display();
break;

case 3:
c3 = c1 * c2;
cout< <" Multiplication of Two complex Nos. :";
c3.display();
break;

case 4:
c3 = c1 / c2;
cout< < "division of Two complex Nos. :";
c3.display();
break;

case 5:exit(0);

default: cout< < endl< < "Aborting!!!!!!!INVALID CHOICE"< < endl;
}
cout< < " Do you want to continue(Y/y)";
cin>>ch;
}
while(ch=='y'||ch=='Y');
getch();
}
/******************* OUTPUT ********************
Two Complex numbers Are :
5.6 +i2.7
3.5 +i5.6

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 1
Addition of Two complex Nos. :9.1 +i8.3
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 2
Subtraction of Two complex Nos. :2.1 +i-2.9
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 3
Multiplication of Two complex Nos. :4.48 +i46.48
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 4
division of Two complex Nos. :1 +i-0.631048
Do you want to continue(Y/y)n
*/

Friend Function

/* C++ program for the Implementation Of Friend Function */

#include< iostream.h>
#include< conio.h>
class complex
{
float real;
float imag;
public:
void getdata(float x,float y)
{
real=x;
imag=y;
}
friend complex add (complex c1,complex c2);
void display()
{
cout< < "the complex no is"< < real< < "+i"< < imag< < endl;
}

};
complex add (complex c1,complex c2)
{
complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return (c3);

}
void main()
{
clrscr();
complex c1,c2,c3;

c1.getdata(4.2,5.5);
c2.getdata(3.5,5.6);
c3=add(c1,c2);
c3.display();
}

Implementation Of Destructors

/* IMPLEMENTATION OF DESTRUCTORS */

#include< iostream.h>
#include< conio.h>

int count = 0;

class data
{
public:
data(void)
{
count++;
cout< < endl< < "The Number of The Object Created Is:"< < count;
}

~data()
{
cout< < endl< < "The Number Of The Object Destroyed Is"< < count;
count--;
}
};

void main()
{
clrscr();
data d1;
data d2;
{
data d3;
data d4;
}
data d5;
data d6;
}


/**********OUTPUT**********
The Number of The Object Created Is: 1
The Number of The Object Created Is: 2
The Number of The Object Created Is: 3
The Number of The Object Created Is: 4
The Number Of The Object Destroyed Is 4
The Number Of The Object Destroyed Is 3
The Number of The Object Created Is: 3
The Number of The Object Created Is: 4
The Number Of The Object Destroyed Is 4
The Number Of The Object Destroyed Is 3
The Number Of The Object Destroyed Is 2
The Number Of The Object Destroyed Is 1

*/

Monday, February 4, 2008

Implementation Of Constructors

A constructor is a member function with the same name as its class. For example:

class X
{
public:
X(); // constructor for class X
};

Constructors are used to create, and can initialize, objects of their class type.

You cannot declare a constructor as virtual or static, nor can you declare a constructor as const, volatile, or const volatile.

You do not specify a return type for a constructor. A return statement in the body of a constructor cannot have a return value.


Types?

Default Constructor:

Constructor() {
name = "";
size = 0;
text = "";
}

-takes 0 parameters, and initializes them as any other language could.


Parameter List Constructor:

Constructor(String n, int s, String t){
name = t;
size = s;
text = t;
}

-again similar to other languages, but this kind of work is not necessary, this is one of many places C++ shines.


Paramenter List Constructor 2:

Constructor(String n, int s, String t):name(n),size(s),text(t) {}


-this method utilizes a feature similar to the contructor, it is as if your primitive types now have there own constructors!! Simplifying your code.

*There is one more thing you can add to this last modification, it will combine the Default and List Parameter Constructors into one!
It will also allow for only partial constructors (eg: only enter a name or name and size only!)


List Parameter with Defaults Constructor:

Constructor(String n="", int s=0, String t=""):name(n),size(s),text(t) {}


-in this case when information is supplied, if only one field is given, the left most field is assumed to be the field supplied. If any fields are left out, or this constructor is called as the default, the values set equal will be used instead!!

Now that is all well and good for primatives, but what about objects?
C++ to the rescue, there is a copy constructor format wich you can use to initialize an object using another object of the same type!

Copy Constructor:

Constructor(const Constructor& c){
this->name = c.name;
this->size = c.size;
this->text = c.text;
}


/* IMPLEMENTATION OF DIFFERENT TYPES OF CONSTRUCTORS */

#include<>
#include<>

class data
{
private:
int x,y,z;

public:
data(void)
{
x = 0;
y = 0;
z = 0;
cout< <"This Is The First Type Of The Constructor"< < endl;
cout< < " The Three Values Are"< < x< < ","< < y< < ","< < z< < endl< < endl;
}

data(int a)
{
x = a;
y = 0;
z = 0;
cout< < "This Is The Second Type Of The Constructor"< < endl;
cout< < "The Three Values Are"< < x< < ","< < y< < ","< < z< < endl< < endl;
}

data(int a,int b)
{
x = a;
y = b;
z = 0;
cout< < "This Is The Third Type Of The Constructor"< < endl;
cout< < "The Three Values Are"< < x< < ","< < y< < ","< < z< < endl < < endl;
}

data(int a,int b,int c)
{
x = a;
y = b;
z = c;
cout< < "This Is The Fourth Type Of The Constructor"< < endl;
cout< <"The Three Values Are"< < x< <" ,"< < y< < ","< < z< < endl;
}
};

void main()
{
data d1();
data d2 = data(9);
data d3(1,2);
data d4(1,2,4);

getch();
}

/* OUTPUT *

This Is The First Type Of The Constructor
The Three Values Are0,0,0

Is The Second Type Of The Constructor
The Three Values Are9,0,0

This Is The Third Type Of The Constructor
The Three Values Are1,2,0

This Is The Fourth Type Of The Constructor
The Three Values Are1,2,4 */

FUNCTION OVERLOADING in C++

/* C++ Program For the IMPLEMENTATION OF FUNCTION OVERLOADING. The Program Finds the Area OF A Circle, Square and rectangle */

#include< iostream.h>
#include< conio.h>

int area(int);
int area(int,int);
float area(float);

void main()
{
clrscr();
cout< < " Area Of Square: "< < area(4);
cout< < " Area Of Rectangle: "< < area(4,4);
cout< < " Area Of Circle: "< < area(3.2);
getch();
}

int area(int a)
{
return (a*a);
}

int area(int a,int b)
{
return(a*b);
}

float area(float r)
{
return(3.14 * r * r);
}

/*****OUTPUT******
Area Of Square: 16 Area Of Rectangle: 16 Area Of Circle: 10.048 */

Pass Object As An Argument

/*C++ PROGRAM TO PASS OBJECT AS AN ARGUMEMT. The program Adds the two heights given in feet and inches. */

#include< iostream.h>
#include< conio.h>

class height
{
int feet,inches;
public:
void getht(int f,int i)
{
feet=f;
inches=i;
}
void putheight()
{
cout< < "\nHeight is:"< < feet< < "feet\t"< < inches< < "inches"< < endl;
}
void sum(height a,height b)
{
height n;
n.feet = a.feet + b.feet;
n.inches = a.inches + b.inches;
if(n.inches ==12)
{
n.feet++;
n.inches = n.inches -12;
}
cout< < endl< < "Height is "< < n.feet< < " feet and "< < n.inches< < endl;
}
};
void main()
{
height h,d,a;
clrscr();
h.getht(6,5);
a.getht(2,7);
h.putheight();
a.putheight();
d.sum(h,a);
getch();
}

/**********OUTPUT***********

Height is:6feet 5inches

Height is:2feet 7inches

Height is 9 feet and 0

*/

Static Data Functions

/* C++ Program For the IMPLEMENTATION OF STATIC MEMBERS FUNCTIONS */

#include< iostream.h>
#include< conio.h>
class customer
{
private:
static int count;
int accountno;
public:
void setaccountno(void);
void displayaccountno(void);
void static displaycount(void);
};
int customer::count;

void customer::setaccountno(void)
{
count++;
accountno=count;
}

void customer :: displayaccountno(void)
{
cout< < "The Account Number Is:"< < accountno< < endl;
}
void customer :: displaycount(void)
{
cout< < "The Total Numer Of Account Are:"< < count< < endl;
}
void main()
{
customer c1,c2;
clrscr();
c1.setaccountno();
c2.setaccountno();
c1.displayaccountno();
c2.displayaccountno();
c1.displaycount();
getch();
}


/********* OUTPUT *************
The Account Number Is:1
The Account Number Is:2
The Total Numer Of Account Are:2
*/

Static Data members

/* C++ program For the IMPLEMENTATION OF STATIC DATA MEMBERS */

#include< iostream.h>
#include< conio.h>
class book
{
private:
static int count;
int bookid;
public:
addbook(void)
{
count ++;
bookid=count;
}
displaybook()
{
cout< < "The Number Of The Books Are:"< < count;
}
};
int book::count;
void main()
{
book b1,b2;
clrscr();
b1.addbook();
b2.addbook();
b1.displaybook();
cout< < endl;
b2.displaybook();
getch();
}

/* OUTPUT
The Number Of The Books Are:2
The Number Of The Books Are:2
*/

Student Marks

/* C++ program for the Implementation Of Class And Objects. The Programs Take inputs of n number of students, their marks and roll number and then displays the ranker. */

#include< iostream.h>
#include< conio.h>
class student
{
int count;
struct stud
{
int rollno;
char name[10];
float marks;
}s[10];

public:
void getcount(void);
void getdata(void);
void putdata(void);
void findranker(void);
};

void student::getcount(void)
{
cout< < "Enter no of Students:";
cin>>count;
}

void student::getdata(void)
{
int i;
for(i=0;i< count;i++)
{
cout< < "Name: ";
cin>>s[i].name;
cout< < "Roll no:";
cin>>s[i].rollno;
cout< < "marks:";
cin>>s[i].marks;
}
}

void student::putdata(void)
{
int i;
cout< < "_______________________________________"< < endl;
cout< < "Name\t"< < "Roll no.\t"< < "Marks"< < endl;
cout< < "---------------------------------------"< < endl;
for(i=0;i< count;i++)
cout< < s[i].name< < "\t"< < s[i].rollno< < "\t\t"< < s[i].marks< < endl;
cout< < "---------------------------------------";
}

void student::findranker(void)
{
int i,loc=0;
float top;
top=s[0].marks;
for(i=0;i< count;i++)
{
if(s[i].marks>top)
{
top=s[i].marks;
loc=i;
}

}
cout< < "\nThe Ranker Is:";
cout< < s[loc].name;
}
void main()
{
clrscr();
student s;
s.getcount();
s.getdata();
s.putdata();
s.findranker();
getch();
}

/***************** OUTPUT ***************
Enter no of Students:4
Name: Lionel
Roll no:1
marks:90
Name: Cyril
Roll no:45
marks:89
Name: Valerian
Roll no:56
marks:87
Name: Noronha
Roll no:56
marks:80
_______________________________________
Name Roll no. Marks
---------------------------------------
Lionel 1 90
Cyril 45 89
Valerian 78 87
Noronha 56 80
---------------------------------------

The Ranker Is : Lionel*/

Wednesday, January 23, 2008

RSA Algorithm

/* C Program For The Implementation Of RSA*/

#include<>
#include<>

void main()
{
long int p,q,n,fn,e,s=0,d=1,C[100],M;
char m[100];
unsigned long int b=0,c=0;
int i,j,t;
clrscr();
printf("Enter the value of p and q: ");
scanf("%ld%ld",&p,&q);
n=p*q;
fn=(p-1)*(q-1);
printf("\nEnter the Encryption key e: ");
scanf("%ld",&e);
do
{ s=(d*e)%fn;
d++;
}while(s!=1);
d=d-1;
printf("\n\tPublic key (%ld,%ld) ",e,n);
printf("\n\tPrivate key (%ld,%ld) ",d,n);
printf("\n\nEnter message :\n");
scanf("%s",m);
for(j=0;j< strlen(m);j++)
{
t=(int)m[j];
c=1;
for(i=0;i< e;i++)
c=c*t%n;
c=c%n;
printf("%d ",c);
}
printf("\n\nEnter cipher text :\n");
for(i=0;i< strlen(m);i++)
scanf("%ld",&C[i]);
printf("\n\n\tPlaintext :");

for(j=0;j< strlen(m);j++)
{
M=1;
for(i=0;i< d;i++)
M=M*C[j]%n;
M=M%n;
printf("%c",M);
}
getch();
}
/********************* OUTPUT ***********************
Enter the value of p and q: 7 17

Enter the Encryption key e: 5

Public key (5,119)
Private key (77,119)

Enter message :
Lionel_Noronha
111 56 76 94 33 75 23 57 76 88 76 94 83 20

Enter cipher text :
111 56 76 94 33 75 23 57 76 88 76 94 83 20


Plaintext :Lionel_Noronha
****************************************************/

Run Length Encoding

/* C Program For The Implementation Of RUN LENGTH ENCODING */

#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
int i,j,cnt,l,count[50]={0};
char str[50];
clrscr();
printf("Enter the string: ");
scanf("%s",str);
printf("\n\tOriginal String is: %s",str);
printf("\n\n\tEncoded String is: ");
l = strlen(str);
for(i=0;i< l;i*=1)
{
j = 0;
count[i] = 1;
do
{
j++;
if(str[i+j] == str[i])
count[i]++;
}while(str[i+j]==str[i]);
if(count[i]==1)
printf("%c",str[i++]);
else
{
printf("$%d%c",count[i],str[i]);
i += count[i];
}
}
getch();
}
/************************* OUTPUT ************************
Enter the string: sleepzzzzzzzzzzzzzzzzzzz

Original String is: sleepzzzzzzzzzzzzzzzzzzz

Encoded String is: sl$2ep$19z */

RSA Algorithm

/* C program for the Implementation Of RSA Algorithm */

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

int phi,M,n,e,d,C,FLAG;

int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return;
}
FLAG = 0;
}

void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}

void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}

void main()
{
int p,q,s;
clrscr();
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&C);
decrypt();
getch();
}

/*************** OUTPUT *****************

Enter Two Relatively Prime Numbers : 7 17

F(n) = 96

Enter e : 5

Public Key : {5,119}
Private Key : {77,119}

Enter The Plain Text : 19

Encrypted keyword : 66

Enter the Cipher text : 66

Decrypted keyword : 19 */

Shanno - Fano Code

/*
The Shannon-Fano Algorithm

This is a basic information theoretic algorithm. A simple example will be used to illustrate the algorithm:


Symbol A B C D E
----------------------------------
Count 15 7 6 6 5

Encoding for the Shannon-Fano Algorithm:


A top-down approach
1. Sort symbols according to their frequencies/probabilities, e.g., ABCDE.

2. Recursively divide into two parts, each with approx. same number of counts.




Symbol Count log(1/p) Code Subtotal (# of bits)
------ ----- -------- --------- --------------------
A 15 1.38 00 30
B 7 2.48 01 14
C 6 2.70 10 12
D 6 2.70 110 18
E 5 2.96 111 15
TOTAL (# of bits): 89

*/

#include< stdio.h>
#include< conio.h>
#include< string.h>
struct node
{
char sym[10];
float pro;
int arr[20];
int top;
}s[20];

typedef struct node node;

void prints(int l,int h,node s[])
{
int i;
for(i=l;i< =h;i++)
{
printf("\n%s\t%f",s[i].sym,s[i].pro);
}
}

void shannon(int l,int h,node s[])
{
float pack1=0,pack2=0,diff1=0,diff2=0;
int i,d,k,j;
if((l+1)==h || l==h || l>h)
{
if(l==h || l>h)
return;
s[h].arr[++(s[h].top)]=0;
s[l].arr[++(s[l].top)]=1;
return;
}
else
{
for(i=l;i< =h-1;i++)
pack1=pack1+s[i].pro;
pack2=pack2+s[h].pro;
diff1=pack1-pack2;
if(diff1< 0)
diff1=diff1*-1;
j=2;
while(j!=h-l+1)
{
k=h-j;
pack1=pack2=0;
for(i=l;i< =k;i++)
pack1=pack1+s[i].pro;
for(i=h;i>k;i--)
pack2=pack2+s[i].pro;
diff2=pack1-pack2;
if(diff2< 0)
diff2=diff2*-1;
if(diff2>=diff1)
break;
diff1=diff2;
j++;
}
k++;
for(i=l;i< =k;i++)
s[i].arr[++(s[i].top)]=1;
for(i=k+1;i< =h;i++)
s[i].arr[++(s[i].top)]=0;
shannon(l,k,s);
shannon(k+1,h,s);
}
}

void main()
{
int n,i,j;
float x,total=0;
char ch[10];
node temp;
clrscr();
printf("Enter How Many Symbols Do You Want To Enter\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("Enter symbol %d ---> ",i+1);
scanf("%s",ch);
strcpy(s[i].sym,ch);
}
for(i=0;i< n;i++)
{
printf("\n\tEnter probability for %s ---> ",s[i].sym);
scanf("%f",&x);
s[i].pro=x;
total=total+s[i].pro;
if(total>1)
{
printf("\t\tThis probability is not possible.Enter new probability");
total=total-s[i].pro;
i--;
}
}
s[i].pro=1-total;
for(j=1;j< =n-1;j++)
{
for(i=0;i< n-1;i++)
{
if((s[i].pro)>(s[i+1].pro))
{
temp.pro=s[i].pro;
strcpy(temp.sym,s[i].sym);
s[i].pro=s[i+1].pro;
strcpy(s[i].sym,s[i+1].sym);
s[i+1].pro=temp.pro;
strcpy(s[i+1].sym,temp.sym);
}
}
}
for(i=0;i< n;i++)
s[i].top=-1;

shannon(0,n-1,s);
printf("---------------------------------------------------------------");
printf("\n\n\n\tSymbol\tProbability\tCode");
for(i=n-1;i>=0;i--)
{
printf("\n\t%s\t%f\t",s[i].sym,s[i].pro);
for(j=0;j< =s[i].top;j++)
printf("%d",s[i].arr[j]);
}
printf("\n---------------------------------------------------------------");
getch();
}

/********************* OUTPUT **************************

Enter How Many Symbols Do You Want To Enter : 6
Enter symbol 1 ---> a
Enter symbol 2 ---> b
Enter symbol 3 ---> c
Enter symbol 4 ---> d
Enter symbol 5 ---> e
Enter symbol 6 ---> f

Enter probability for a ---> 0.3

Enter probability for b ---> 0.25

Enter probability for c ---> 0.20

Enter probability for d ---> 0.12

Enter probability for e ---> 0.08

Enter probability for f ---> 0.05


---------------------------------------------------------------
Symbol Probability Code
a 0.300000 00
b 0.250000 01
c 0.200000 10
d 0.120000 110
e 0.080000 1110
f 0.050000 1111
---------------------------------------------------------------
*/

Convolutional Codes

/* C Porgram To Find convolutional Codes or Code Vectors */

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

void main()
{
int i,j,k,n,gb,mb,g[10][10],x[10][10],c[10],m[10],t[10];
clrscr();
for(i=0;i< 10;i++)
{
for(j=0;j< 10;j++)
g[i][j]=0;x[i][j]=0;c[i]=0;m[i]=0;t[i]=0;
}
printf("\t\t\t¦ Convolutional Code ¦");
printf("\n\nEnter The No. Of Generator Sequences\t: ");
scanf("%d",&n);
printf("Enter The No. Of Bits In Each Generator Sequence\t: ");
scanf("%d",&gb);
for(i=0;i< n;i++)
{
printf("\n\nEnter The Values Of G%d\t:\n",i);
for(j=0;j< gb;j++)
scanf("%dt",&g[i][j]);
}
printf("\nEnter The No. Of Message Bits\t: ");
scanf("%d",&mb);
printf("Enter The Message Bits\t:\n");
for(i=0;i< mb;i++)
scanf("%d",&c[i]);
for(i=0;i< mb;i++)
{
for(j=0;j< gb;j++)
{
t[j]=m[j];
if(j==0)
m[j]=c[i];
else
m[j]=t[j-1];
}
for(k=0;k< gb;k++)
{
for(j=0;j< gb;j++)
{
if(g[k][j]==1)
x[k][i]=x[k][i]^m[j];
}
}
}
printf("\n\n¦ The Code Vectors Are: ¦\n");
for(i=0;i< mb;i++)
{
printf("¦ \n¦ ");
for(j=0;j< gb;j++)
printf("%d\t",x[j][i]);
}
getch();
}

/*************** OUTPUT ******************
¦ Convolutional Code ¦

Enter The No. Of Generator Sequences : 2
Enter The No. Of Bits In Each Generator Sequence : 3


Enter The Values Of G0 :
1
1
1


Enter The Values Of G1 :
1
0
1


Enter The No. Of Message Bits : 8
Enter The Message Bits :
1
1
0
1
1
0
1
1


¦ The Code Vectors Are: ¦
¦
¦ 1 1 0 ¦
¦ 0 1 0 ¦
¦ 0 1 0 ¦
¦ 0 0 0 ¦
¦ 0 1 0 ¦
¦ 0 1 0 ¦
¦ 0 0 0 ¦
¦ 0 1 0

*/

Cyclic Code

/* C Program For Implementation Of Cyclic Code */

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

void main()
{
int n,k,g[10],C[10],M[10],I,T[10];
int i,j,r;
clrscr();
printf("\t\t\t³ CYCLIC ENCODER ³\n");

printf("\n\nEnter The Value Of n and k\t:\n");
scanf("%d%d",&n,&k);
printf("\nEnter The Generator Polynomial Of Dergee %d",(n-k));
printf(" of the form 1+x+xý+x³+...\n");

for(i=0;i< n-k;i++)
scanf("%d",&g[i]);
printf("\nEnter Msg\n");
for(i=0;i< k;i++)
scanf("%d",&M[i]);
printf("\n\n");

for(i=0;i< n-k;i++)
{
T[i]=0;
}
for(i=0;i< k;i++)
{
I = (M[i] + T[n-k-1])%2;
C[0] = I;

for(j=1;j< n-k;j++)
{
if(g[j]==1)
C[j]=(I + T[j-1])%2;
else
C[j]=T[j-1];

}
for(r=0;r< n-k;r++)
{
T[r]=C[r];
}
}
printf("-----------------------------------------------------");
printf("\n³\tGenerated Cyclic code is\t: ");
for(i=0;i< k;i++)
printf("%d",M[i]);
printf(" ");
for(i=0;i< n-k;i++)
printf("%d",C[i]);
printf("\t\n-------------------------------------------------------");
getch();
}
/********************* OUTPUT *************************
³ CYCLIC ENCODER ³

Enter The Value Of n and k :
7
4

Enter The Generator Polynomial Of Dergee 3 of the form 1+x+xý+x³+...
1
1
0

Enter Msg
1
0
1
0


-------------------------------------------------------
Generated Cyclic code is : 1010 110
-------------------------------------------------------

*/

Entropy Of Given Message

/* C Program to find Entropy Of Given Message */

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

void main()
{
int c,i;
float p,sum=0,it;
clrscr();
printf("**** INFORMATION CONTENT OF FIVE MESSEGES *****\n\n");
printf("Enter No Of Messages : ");
scanf("%d",&c);
printf("\n");
for(i=1;i< =c;i++)
{
printf("\tEnter The %d Probability\t: ",i);
scanf("%f",&p);
it = p* (log(1/p)) / log(2);
sum=sum+it;
}
printf("\nEntropy Of The Given Message : %f",sum);
getch();
}
/******************** OUTPUT *************************

**** INFORMATION CONTENT OF FIVE MESSEGES *****

Enter No Of Messages : 5

Enter The 1 Probability : 0.1
Enter The 2 Probability : 0.2
Enter The 3 Probability : 0.3
Enter The 4 Probability : 0.36
Enter The 5 Probability : 0.04

Entropy Of The Given Message : 2.034038 */

Tuesday, January 22, 2008

Linear Block Code

/* C Program For Implementation Of Linear Block Code.
The User Should Enter the following details :
For A Generator Matrix
1. The No Of Message Bits(n)
2. The No Of Parity Bits(k)
3. And The Message Bits */

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

void main()
{
int i,j,n,k,M[10],C[10],G[10][10],par[10][10];
clrscr();
printf(" For A Generator Matrix Enter :\n");
printf("1. The No Of Message Bits(n)\t: ");
scanf("%d",&n);
printf("2. The No Of Parity Bits(k)\t: ");
scanf("%d",&k);
printf("\nEnter The Elements Of Generator Matrix:\n");
for(i=0;i< k;i++)
for(j=0;j< n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter The Message Bits\n");
for(i=0;i< k;i++)
scanf("%d",&M[i]);
printf("\t\t\tResult\n");
printf("____________________________________________________");
printf("\n\tThe Generator Matrix Is :\n\t");
for(i=0;i< k;i++)
{
for(j=0;j< n;j++)
printf("%d\t",G[i][j]);
printf("\n\t");
}
printf("\n\tThe Parity Matrix Is :\n\t");
for(i=0;i< k;i++)
{
for(j=0;j< n-k;j++)
{
par[i][j]=G[i][j+k];
printf("%d\t",par[i][j]);
}
printf("\n\t");
}
printf("\n\tThe Messege Bits :\n\t");
for(i=0;i< k;i++)
printf("%d ",M[i]);
printf("\n\n\tThe Parity Bits :");
for(i=0;i< n-k;i++)
{
C[i] = 0;
for(j=0;j< k;j++)
C[i]=(C[i] + M[j] * par[j][i])%2;
printf("\n\tC%d = %d",i+1,C[i]);
}
printf("\n\n\tCode Word For Given Messege Bit:\n\t");
for(i=0;i< k;i++)
printf("%d ",M[i]);
for(i=0;i< n-k;i++)
printf("%d ",C[i]);
printf("\n____________________________________________________");
getch();
}
/********************** OUTPUT **************************
For A Generator Matrix Enter :
1. The No Of Message Bits(n) : 6
2. The No Of Parity Bits(k) : 3

Enter The Elements Of Generator Matrix:
1
0
0
1
1
1
0
1
0
1
1
0
0
0
1
1
0
1

Enter The Message Bits
0
0
1
Result
____________________________________________________
The Generator Matrix Is :
1 0 0 1 1 1
0 1 0 1 1 0
0 0 1 1 0 1

The Parity Matrix Is :
1 1 1
1 1 0
1 0 1

The Messege Bits :
0 0 1

The Parity Bits :
C1 = 1
C2 = 0
C3 = 1

Code Word For Given Messege Bit:
0 0 1 1 0 1
____________________________________________________ */

Information Content

/* C Program to find th information Content OF the 5 message entered by the user */

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

void main()
{
int c,i;
float p,sum=0,it;
clrscr();
printf("***** INFORMATION CONTENT OF FIVE MESSEGES *****\n\n");
printf("Enter No Of Messages : ");
scanf("%d",&c);
for(i=1;i< =c;i++)
{
printf("\tEnter The %d Probability\t: ",i);
scanf("%f",&p);
it=(log(1/p)) / log(2);
sum=sum+it;
}
printf("Information Content Of Given Message : %f",sum);
getch();
}
/********************* OUTPUT ***********************

***** INFORMATION CONTENT OF FIVE MESSEGES *****

Enter No Of Messages : 5
Enter The 1 Probability : 0.1
Enter The 2 Probability : 0.2
Enter The 3 Probability : 0.3
Enter The 4 Probability : 0.36
Enter The 5 Probability : 0.04
Information Content Of Given Message : 13.498609 */

Even / Odd Number

/* C Program For Checking whether a number is even or odd */

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

void main()
{
int a;
clrscr();
printf("Enter A number : ");
scanf("%d",&a);
if(a%2 != 0)
{
printf("\nThe Number %d Is Odd",a);
}
else if(a%2 == 0)
{
printf("\nThe Number %d Is Even",a);
}
getch();
}
/******************* OUTPUT ********************

Enter A number : 66
The Number 66 Is Even

Enter A number : 37
The Number 37 Is Odd */

Non - Restoring Division

/* C Program For Implementation Of Non - Restoring Division */

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

int a[5]={0,0,0,0,0},q[4],b[5],b2c[5];

comp()
{
int i=4;
do
{
b2c[i]=b[i];
i--;
}while(b[i+1]!=1);
while(i>=0)
{
b2c[i]=(b[i]+1)%2;
i--;
}
printf("\n\tB's complement:");
for(i=0;i< 5;i++)
printf("%d",b2c[i]);
printf("\n");
}


nonresdiv()
{
shiftleft();
if(a[0]==0)
a_minus_b();
else
a_plus_b();
q[3]=(a[0]+1)%2;
}

shiftleft()
{
int i;
for(i=0;i< 4;i++)
a[i]=a[i+1];
a[4]=q[0];
for(i=0;i< 3;i++)
q[i]=q[i+1];
}

a_minus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b2c[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}

a_plus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}


void main()
{
int i,j,k;
clrscr();
printf("Enter dividend in binary form\t: ");
for(i=0;i< 4;i++)
scanf("%d",&q[i]);
printf("Enter divisor in binary form\t: ");
for(i=0;i< 5;i++)
scanf("%d",&b[i]);
comp();
printf("\n\t[A]\t[M]\n");
for(i=0;i< 4;i++)
{
nonresdiv();
printf("\t");
for(j=0;j< 5;j++)
printf("%d",a[j]);
printf("\t");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n");
}
if(a[0]==1)
a_plus_b();printf("\t");
for(j=0;j< 5;j++)
printf("%d",a[j]);
printf("\t");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n");
printf("\n\tThe Quotient Is\t: ");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n\tThe Remainder Is\t: ");
for(j=0;j< 5;j++)
printf("%d",a[j]);
getch();
}

/********************** OUTPUT ****************
Enter dividend in binary form : 0 1 0 1
Enter divisor in binary form : 0 0 0 1 1

B's complement:11101

[A] [M]
11101 1010
11110 0100
11111 1000
00010 0001
00010 0001

The Quotient Is : 0001
The Remainder Is : 00010

Enter dividend in binary form : 1 1 1 1
Enter divisor in binary form : 0 0 1 1 1

B's complement:11001

[A] [M]
11010 1110
11100 1100
00000 1001
11010 0010
00001 0010

The Quotient Is : 0010
The Remainder Is : 00001


Enter dividend in binary form : 1 1 0 1
Enter divisor in binary form : 0 0 0 1 1

B's complement:11101

[A] [M]
11110 1010
00000 0101
11101 1010
11110 0100
00001 0100

The Quotient Is : 0100
The Remainder Is : 00001
*/
Your Ad Here