#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int nol=0,not=0,nos=0,noc=0;
clrscr();
fp=fopen("pr1.c","r");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
noc++;
if(ch==' ')
nos++;
if(ch=='\n')
nol++;
if(ch=='\t')
not++;
}
fclose(fp);
printf("\n Number of characters = %d",noc);
printf("\n Number of blanks = %d",nos);
printf("\n Number of tabs = %d",not);
printf("\n Number of lines = %d",nol);
getch();
}
Friday, November 12, 2010
C Progarm to input numbers into a file and separate the odd and even numbers into two different files
/*
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,i;
clrscr();
printf(" \nContents of DATA file \n\n");
f1=fopen("DATA","w");
for(i=1;i<=30;i++)
{
scanf("%d",&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen("DATA","r");
f3=fopen("ODD","w");
f2=fopen("EVEN","w");
/* Read from DATA file */
while((number=getw(f1))!=EOF)
{
if(number%2==0)
putw(number,f2);
else
putw(number,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\n\n Contents of ODD FILE \n\n");
while((number=getw(f2))!=EOF)
printf("%4d",number);
printf("\n\n Contents of EVEN FILE \n\n");
while((number=getw(f3))!=EOF)
printf("%4d",number);
fclose(f2);
fclose(f3);
getch();
}
// Use -1 to stop inserting numbers.................................................
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,i;
clrscr();
printf(" \nContents of DATA file \n\n");
f1=fopen("DATA","w");
for(i=1;i<=30;i++)
{
scanf("%d",&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen("DATA","r");
f3=fopen("ODD","w");
f2=fopen("EVEN","w");
/* Read from DATA file */
while((number=getw(f1))!=EOF)
{
if(number%2==0)
putw(number,f2);
else
putw(number,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\n\n Contents of ODD FILE \n\n");
while((number=getw(f2))!=EOF)
printf("%4d",number);
printf("\n\n Contents of EVEN FILE \n\n");
while((number=getw(f3))!=EOF)
printf("%4d",number);
fclose(f2);
fclose(f3);
getch();
}
// Use -1 to stop inserting numbers.................................................
Thursday, November 11, 2010
C Program to demonstrate Heap Sort
#include<stdio.h>
#include<conio.h>
void creatheap(int [],int);
void sort(int[],int);
void main()
{
int xarr[10]={11,2,9,13,57,25,17,1,90,3};
int i;
printf(" \n Heap Sort ");
creatheap(xarr,10);
printf("\n Before Sorting \n");
for(i=0;i<=9;i++)
printf("%d\t",xarr[i]);
sort(xarr,10);
printf("\n Array Sorting :\n");
for(i=0;i<=9;i++)
printf("%d\t",xarr[i]);
getch();
}
void creatheap(int x[],int n)
{
int i,val,s,f;
for(i=1;i<n;i++)
{
val=x[i];
s=i;
f=(s-1)/2;
while(s>0 && x[f] < val)
{
x[s]=x[f];
s=f;
f=(s-1)/2;
}
x[s]=val;
}
}
void sort(int x[],int n)
{
int i,s,f,ivalue;
for(i=n-1;i>=1;i--)
{
ivalue=x[i];
x[i]=x[0];
f=0;
if(i==1)
s=-1;
else
s=1;
if(i>2 && x[2] > x[1])
s=2;
while(s >= 0 && ivalue<x[s])
{
x[f]=x[s];
f=s;
s=2*f +1;
if((s+1)<=(i-1) && x[s]<x[s+1])
s++;
if(s>i-1)
s=-1;
}
x[f]=ivalue;
}
}
#include<conio.h>
void creatheap(int [],int);
void sort(int[],int);
void main()
{
int xarr[10]={11,2,9,13,57,25,17,1,90,3};
int i;
printf(" \n Heap Sort ");
creatheap(xarr,10);
printf("\n Before Sorting \n");
for(i=0;i<=9;i++)
printf("%d\t",xarr[i]);
sort(xarr,10);
printf("\n Array Sorting :\n");
for(i=0;i<=9;i++)
printf("%d\t",xarr[i]);
getch();
}
void creatheap(int x[],int n)
{
int i,val,s,f;
for(i=1;i<n;i++)
{
val=x[i];
s=i;
f=(s-1)/2;
while(s>0 && x[f] < val)
{
x[s]=x[f];
s=f;
f=(s-1)/2;
}
x[s]=val;
}
}
void sort(int x[],int n)
{
int i,s,f,ivalue;
for(i=n-1;i>=1;i--)
{
ivalue=x[i];
x[i]=x[0];
f=0;
if(i==1)
s=-1;
else
s=1;
if(i>2 && x[2] > x[1])
s=2;
while(s >= 0 && ivalue<x[s])
{
x[f]=x[s];
f=s;
s=2*f +1;
if((s+1)<=(i-1) && x[s]<x[s+1])
s++;
if(s>i-1)
s=-1;
}
x[f]=ivalue;
}
}
C Program to demonstrate BinarY Tree Sort
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct BinaryTree
{
struct BinaryTree *LeftChild;
int data;
struct BinaryTree *RightChild;
};
void insert(struct BinaryTree **,int);
void inorder(struct BinaryTree *);
void main()
{
struct BinaryTree *BTree;
int Data[10]={11,2,9,13,57,25,17,1,90,3};
int i;
BTree=NULL;
clrscr();
printf(" Binary Tree Sort .\n");
printf("\nArray :\n");
for(i=0;i<10;i++)
printf("%d\t",Data[i]);
for(i=0;i<=9;i++)
insert(&BTree,Data[i]);
printf("\n In-order traversal of Binary Tree : \n");
inorder(BTree);
getch();
}
void insert(struct BinaryTree **sr,int num)
{
if(*sr == NULL)
{
(*sr)=malloc(sizeof(struct BinaryTree));
(*sr)->LeftChild=NULL;
(*sr)->data=num;
(*sr)->RightChild=NULL;
}
else
{
if(num < (*sr)->data)
insert(&((*sr)->LeftChild),num);
else
insert(&((*sr)->RightChild),num);
}
}
void inorder(struct BinaryTree *sr)
{
if(sr!=NULL)
{
inorder(sr -> LeftChild);
printf("%d\t",sr->data);
inorder(sr -> RightChild);
}
}
#include<conio.h>
#include<alloc.h>
struct BinaryTree
{
struct BinaryTree *LeftChild;
int data;
struct BinaryTree *RightChild;
};
void insert(struct BinaryTree **,int);
void inorder(struct BinaryTree *);
void main()
{
struct BinaryTree *BTree;
int Data[10]={11,2,9,13,57,25,17,1,90,3};
int i;
BTree=NULL;
clrscr();
printf(" Binary Tree Sort .\n");
printf("\nArray :\n");
for(i=0;i<10;i++)
printf("%d\t",Data[i]);
for(i=0;i<=9;i++)
insert(&BTree,Data[i]);
printf("\n In-order traversal of Binary Tree : \n");
inorder(BTree);
getch();
}
void insert(struct BinaryTree **sr,int num)
{
if(*sr == NULL)
{
(*sr)=malloc(sizeof(struct BinaryTree));
(*sr)->LeftChild=NULL;
(*sr)->data=num;
(*sr)->RightChild=NULL;
}
else
{
if(num < (*sr)->data)
insert(&((*sr)->LeftChild),num);
else
insert(&((*sr)->RightChild),num);
}
}
void inorder(struct BinaryTree *sr)
{
if(sr!=NULL)
{
inorder(sr -> LeftChild);
printf("%d\t",sr->data);
inorder(sr -> RightChild);
}
}
Saturday, November 6, 2010
C Program to recontruct a Binary Search Tree
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define MAX 101
struct node
{
struct node *left;
int data;
struct node *right;
};
void insert(struct node **,int);
void preorder(struct node *);
void postorder(struct node *);
void inorder(struct node *);
struct node * recons(int*, int *,int);
void deltree(struct node *);
int in[MAX],pre[MAX],x;
void main()
{
struct node *t,*p,*q;
int req,i,num;
t=NULL; /* Empty Tree */
clrscr();
printf(" Specify the number of items to be inserted : ");
while(1)
{
scanf(" %d",&req);
if(req >= MAX || req <= 0)
printf("\n Enter number between 1 to 100. \n");
else
break;
}
for(i=0;i<req;i++)
{
printf("\n Enter the data : ");
scanf("%d",&num);
insert(&t,num);
}
printf("\n In-order Traversal :\n");
x=0;
inorder(t);
printf("\n Pre-order Traversal :\n");
x=0;
preorder(t);
printf("\n Post-order Traversal :\n");
x=0;
postorder(t);
deltree(t);
t=NULL;
t=recons(in,pre,req);
printf("\n\n After reconstruction of the binary tree.\n");
x=0;
printf("\n In-order Traversal :\n");
inorder(t);
x=0;
printf("\n Pre-order Traversal :\n");
preorder(t);
x=0;
printf("\n Post-order Traversal :\n");
postorder(t);
deltree(t);
getch();
}
/* Inserts a new node in a binary search tree */
void insert(struct node **sr,int num)
{
if(*sr == NULL)
{
*sr=(struct node *)malloc(sizeof(struct node));
(*sr)->left=NULL;
(*sr)->data=num;
(*sr)->right=NULL;
return;
}
else /* Search the node to which new node will be attached */
{
/* If new data is less , traverse to left */
if(num < (*sr)->data)
insert(&((*sr)->left),num);
else
/* Else traverse to right */
insert(&((*sr)->right),num);
}
}
void preorder(struct node *t)
{
if(t != NULL)
{
printf("%d\t",pre[x++]=t->data);
preorder(t->left);
preorder(t->right);
}
}
void postorder(struct node *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%d\t",t->data);
}
}
void inorder(struct node *t)
{
if(t != NULL)
{
inorder(t->left);
printf("%d\t",in[x++]=t->data);
inorder(t->right);
}
}
struct node * recons(int *inorder,int *preorder,int noofnodes)
{
struct node *temp,*left,*right;
int tempin[100],temppre[100],i,j;
if(noofnodes == 0)
return NULL;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=preorder[0];
temp->left=NULL;
temp->right=NULL;
if(noofnodes == 1)
return temp;
for(i=0;inorder[i]!=preorder[0];)
i++;
if(i>0)
{
for(j=0;j<=i;j++)
tempin[j]=inorder[j];
for(j=0;j<i;j++)
temppre[j]=preorder[j+1];
}
left=recons(tempin,temppre,i);
temp->left=left;
if(i<noofnodes -1)
{
for(j=i;j<noofnodes;j++)
{
tempin[j-i]=inorder[j+1];
temppre[j-i]=preorder[j+1];
}
}
right=recons(tempin,temppre,noofnodes-i-1);
temp->right=right;
return temp;
}
void deltree(struct node *t)
{
if(t!=NULL)
{
deltree(t->left);
deltree(t->right);
}
free(t);
}
#include<conio.h>
#include<alloc.h>
#define MAX 101
struct node
{
struct node *left;
int data;
struct node *right;
};
void insert(struct node **,int);
void preorder(struct node *);
void postorder(struct node *);
void inorder(struct node *);
struct node * recons(int*, int *,int);
void deltree(struct node *);
int in[MAX],pre[MAX],x;
void main()
{
struct node *t,*p,*q;
int req,i,num;
t=NULL; /* Empty Tree */
clrscr();
printf(" Specify the number of items to be inserted : ");
while(1)
{
scanf(" %d",&req);
if(req >= MAX || req <= 0)
printf("\n Enter number between 1 to 100. \n");
else
break;
}
for(i=0;i<req;i++)
{
printf("\n Enter the data : ");
scanf("%d",&num);
insert(&t,num);
}
printf("\n In-order Traversal :\n");
x=0;
inorder(t);
printf("\n Pre-order Traversal :\n");
x=0;
preorder(t);
printf("\n Post-order Traversal :\n");
x=0;
postorder(t);
deltree(t);
t=NULL;
t=recons(in,pre,req);
printf("\n\n After reconstruction of the binary tree.\n");
x=0;
printf("\n In-order Traversal :\n");
inorder(t);
x=0;
printf("\n Pre-order Traversal :\n");
preorder(t);
x=0;
printf("\n Post-order Traversal :\n");
postorder(t);
deltree(t);
getch();
}
/* Inserts a new node in a binary search tree */
void insert(struct node **sr,int num)
{
if(*sr == NULL)
{
*sr=(struct node *)malloc(sizeof(struct node));
(*sr)->left=NULL;
(*sr)->data=num;
(*sr)->right=NULL;
return;
}
else /* Search the node to which new node will be attached */
{
/* If new data is less , traverse to left */
if(num < (*sr)->data)
insert(&((*sr)->left),num);
else
/* Else traverse to right */
insert(&((*sr)->right),num);
}
}
void preorder(struct node *t)
{
if(t != NULL)
{
printf("%d\t",pre[x++]=t->data);
preorder(t->left);
preorder(t->right);
}
}
void postorder(struct node *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%d\t",t->data);
}
}
void inorder(struct node *t)
{
if(t != NULL)
{
inorder(t->left);
printf("%d\t",in[x++]=t->data);
inorder(t->right);
}
}
struct node * recons(int *inorder,int *preorder,int noofnodes)
{
struct node *temp,*left,*right;
int tempin[100],temppre[100],i,j;
if(noofnodes == 0)
return NULL;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=preorder[0];
temp->left=NULL;
temp->right=NULL;
if(noofnodes == 1)
return temp;
for(i=0;inorder[i]!=preorder[0];)
i++;
if(i>0)
{
for(j=0;j<=i;j++)
tempin[j]=inorder[j];
for(j=0;j<i;j++)
temppre[j]=preorder[j+1];
}
left=recons(tempin,temppre,i);
temp->left=left;
if(i<noofnodes -1)
{
for(j=i;j<noofnodes;j++)
{
tempin[j-i]=inorder[j+1];
temppre[j-i]=preorder[j+1];
}
}
right=recons(tempin,temppre,noofnodes-i-1);
temp->right=right;
return temp;
}
void deltree(struct node *t)
{
if(t!=NULL)
{
deltree(t->left);
deltree(t->right);
}
free(t);
}
Subscribe to:
Posts (Atom)