Wednesday, 27 December 2017

C PROGRAM DEMONSTRATING ALL TREE TRAVERSALS RECURSIVELY AND NON RECURSIVELY

#include<stdio.h>
#include<stdlib.h>
struct Tnode{
 struct Tnode*pleft;
 struct Tnode*pright;
int ch;
};
struct Tnode *stack[50];
struct Tnode *stack1[50];
int top,top1;
void push(struct Tnode* ch1);
struct Tnode* pop();
void push1(struct Tnode *ch1);
struct Tnode *pop1();
struct Tnode*root;
struct Tnode*create_node(int c);
void create();
void insert(int c);
void non_preorder();
void non_postorder();
void non_inorder();
void inorder(struct Tnode *rot);
void preorder(struct Tnode *rot);
void postorder(struct Tnode *rot);
//main function
int main(){
top=-1;root=NULL;top1=-1;
create();
printf("RECURSIVE INORDER TRAVERSAL IS:\n");
inorder(root);
printf("\n");
printf("RECURSIVE PREORDER TRAVERSAL IS:\n");
preorder(root);
printf("\n");
printf("RECURSIVE POSTORDER TRAVERSAL IS:\n");
postorder(root);
printf("\n");
non_preorder();
printf("\n");
non_postorder();
printf("\n");
non_inorder();
 return 0;}

struct Tnode*create_node(int c){
 struct Tnode*pnew=(struct Tnode*)malloc(sizeof(struct Tnode));
 if(pnew){
 pnew->ch=c;
 pnew->pleft=NULL;
  pnew->pright=NULL;}
 return pnew;}
 void insert(int c){
  struct Tnode *pnew,*ptrav,*pa;
  pnew=create_node(c);
  if(!pnew) printf("no memeory\n");
  else{
   if(!root)
   root=pnew;
   else{
    pa=root;ptrav=root;
    while(ptrav){
     pa=ptrav;
     if(c<(ptrav->ch)){
     ptrav=ptrav->pleft;}
     else{
     ptrav=ptrav->pright;}}
    if(c<(pa->ch)){
    pa->pleft=pnew;}
    else{
    pa->pright=pnew;}}return;}}
 void create(){
 int k;
  int i,n;
  printf("enter number of values\n");scanf("%d",&n);
  for(i=0;i<n;i++) {
  scanf("%d",&k);
  insert(k);
  }}
 void push(struct Tnode *ch1){
  top++;
  stack[top]=ch1;
  return;
 }
struct Tnode *pop(){
  struct Tnode* k;
  if(top<0){
  k=NULL;return k ;}
  else
  {

    k=stack[top];
   top--;
   return k;
  }
 }
   void push1(struct Tnode *ch1){//thi function is for stack1 operations
  top1++;
  stack1[top1]=ch1;
  return;
 }//push1 end

struct Tnode *pop1(){
  struct Tnode* k;
  if(top1<0){
  k=NULL;return k ;}
  else
  {
    k=stack1[top1];
   top1--;
   return k;
  }}
//RECURSIVE FUNCTIONS
 void inorder(struct Tnode *rot){
  if(rot){
 inorder(rot->pleft);
   printf("%d\t",rot->ch);
   inorder(rot->pright);}
  }

   void preorder(struct Tnode *rot){
  if(rot){
  printf("%d\t",rot->ch);
  preorder(rot->pleft);
  preorder(rot->pright);}
  }

   void postorder(struct Tnode *rot){
  if(rot){
  postorder(rot->pleft);
   postorder(rot->pright);
   printf("%d\t",rot->ch);}
  }
//NON RECURSIVE FUNCTIONS
void non_preorder(){
char l; 
int done=0;
struct Tnode *ptemp=NULL,*ptr=root;
push(ptr);
if(!root){printf("empty tree\n");
}else{
 printf("NON RECURSIVE PREORDER IS:\n");
 while(top>=0){
ptr=pop(); 
   printf("%d\t",ptr->ch);
 if((ptr->pright)!=NULL){
 ptemp=ptr->pright;
 push(ptemp);}
 if((ptr->pleft)!=NULL){
 ptemp=ptr->pleft;
 push(ptemp);
}
}
}}

void non_postorder(){
char l; 
int done=0;
struct Tnode *ptemp=NULL,*ptr=root;
push(ptr);
if(!root){printf("empty tree\n");
}else{
 printf("NON RECURSIVE POST ORDER:\n");
 while(top>=0){
          ptr=pop();
  push1(ptr);
 if((ptr->pleft)!=NULL){
 ptemp=ptr->pleft;
 push(ptemp);}
 if((ptr->pright)!=NULL){
 ptemp=ptr->pright;
 push(ptemp);}
}
}
while(top1>=0){
 ptemp=pop1();
 printf("%d\t",ptemp->ch);
}
}

void non_inorder(){
struct Tnode *ptemp=NULL,*ptr=root;
if(!root){printf("empty tree\n");
}else{
push(ptr);
 printf("NON RECURSIVE IN ORDER IS:\n");
 while(top>=0){
  if((ptr->pleft)!=NULL){
 ptr=ptr->pleft;
 push(ptr);
}
 else{
ptemp=pop();
  printf("%d\t",ptemp->ch);
  if(ptemp->pright!=NULL){
  ptr=ptemp->pright;
  push(ptr);
 }}
}
}
}

C PROGRAM FOR BINARY SEARCH USING RECURSION

#include<stdio.h>
void bin(int l,int h,int arr[],int s);
int main()
{
int s,i,k,arr[10]={0};
printf("enter array elements\n");
for(i=0;i<5;i++)
scanf("%d",&arr[i]);
printf("enter element to search\n");
scanf("%d",&s);
bin(0,4,arr,s);//CALL TO RECURSIVE FUN
return 0;
}
void bin(int l,int h,int arr[],int s){
int m;
if(l<=h){
m=(l+h)/2;
if(arr[m]==s){
printf("element found at %d",m+1);
}
else if(s<arr[m]){
 bin( l,m-1,arr,s);//RECURSIVE CALLS
}
else 
 bin( m+1,h,arr,s);}
else
printf("element not found\n");
return;
}

Thursday, 30 November 2017

GAUSS SEIDEL METHOD USING C PROGRAM

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,k,s=1;
printf("enter number of variales");
scanf("%d",&n);
float a[n][n];
float  b[n];
float x[n]={0},var,itr,temp,temp1;
printf("enter approx");
scanf("%f",&itr);
for(i=0;i<n;i++)
{
printf("enter element at b[%d]",i);
scanf("%f",&b[i]);

for(j=0;j<n;j++)
{
printf("enter element at a[%d][%d]",i,j);
scanf("%f",&a[i][j]);
}
}
do
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
var+=a[i][j]*x[j];
}
}
temp1=(1/a[i][i])*(b[i]-var);
var=0;
printf("initial %f\n",x[i]);
temp=fabs(temp1-x[i]);
x[i]=temp1;
temp1=0;
}
}while((temp>itr));

for(i=0;i<n;i++)
{
printf(" x%d=%f\t",i,x[i]);
}

return 0;
}

GAUSS JACOBI METHOD USING C PROGRAM

//gauss jacobi
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,k,s=1;
printf("enter number of variales");
scanf("%d",&n);
float a[n][n];
float  b[n];
float x[n]={0},x1[n]={0},var,itr,temp,temp1;
printf("enter approx");
scanf("%f",&itr);
for(i=0;i<n;i++)
{
printf("enter element at b[%d]",i);
scanf("%f",&b[i]);

for(j=0;j<n;j++)
{
printf("enter element at a[%d][%d]",i,j);
scanf("%f",&a[i][j]);
}
}
do
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
{
var+=a[i][j]*x1[j];
}
}
x[i]=(1/a[i][i])*(b[i]-var);
var=0;
printf("initial %f\n",x[i]);
temp=fabs(x[i]-x1[i]);
}
for(i=0;i<n;i++)
{
x1[i]=x[i];
}
}while((temp>itr));
for(i=0;i<n;i++)
{
printf(" x%d=%f\t",i,x[i]);
}

return 0;
}

EULER MODIFIED METHOD USING C PROGRAM

#include<stdio.h>
#include<math.h>
float fun(float x,float y)
{
float f;
f=x*x+y;
return f;
}
int main()
{
float diff,y0,y,x0,x1,h,xn,y1,y2;
printf("enter x0,y0,h,xn\n");
scanf("%f%f%f%f",&x0,&y0,&h,&xn);
do
{
  y=y0+h*(fun(x0,y0));
  x1=x0+h;
  do
  {
  y2=y;
  y1=y0+(h/2)*(fun(x0,y0)+fun(x1,y));
  y=y1;
  diff=y1-y2;
  //printf("%f\n",y1-y2);
  }while(fabs(diff)>0.000001);
  y0=y1;
  x0=x0+h;
  printf("x=%f \t y=%f\n",x0,y1);
}while(x0<xn);
return 0;

}

RANGE KUTTA METHOD USING C PROGRAM

#include<stdio.h>
#include<math.h>
float fun(float x,float y)
{
float f;
f=x+(y*y);
return f;
}
int main()
{
float x0,y0,xn,h,y,k1,k2,k3,k4,k,x;
printf("enter x0,y0,h,xn\n");
scanf("%f%f%f%f",&x,&y,&h,&xn);
do
{
k1=h*fun(x,y);
k2=h*fun(x+h/2,y+(k1/2));
k3=h*fun(x+h/2,y+(k2/2));
k4=h*fun(x+h,y+k3);
k=(1/6)*(k1+2*(k2+k3)+k4);
y0=y+k;
printf("%f\n",y);
x=x+h;
y=y0;
}while(x<xn);

return 0;

}

C PROGRAM FOR NON RECURSIVE PREORDER TRAVERSAL

#include<stdio.h>
#include<stdlib.h>
struct Tnode{
struct Tnode*pleft;
struct Tnode*pright;char ch;
};
struct Tnode *stack[50];
int top;
void push(struct Tnode* ch1);
struct Tnode* pop();
struct Tnode*root;
struct Tnode*create_node(char c);
void create();
void insert(char c);
void print();
//main function
int main(){
top=-1;root=NULL;
create();
printf("\n");
print();
return 0;}

struct Tnode*create_node(char c){
struct Tnode*pnew=(struct Tnode*)malloc(sizeof(struct Tnode));
if(pnew){
pnew->ch=c;
pnew->pleft=NULL;
pnew->pright=NULL;}
return pnew;}
void insert(char c){
struct Tnode *pnew,*ptrav,*pa;
pnew=create_node(c);
if(!pnew) printf("no memeory\n");
else{
if(!root)
root=pnew;
else{
pa=root;ptrav=root;
while(ptrav){
pa=ptrav;
if(c<(ptrav->ch)){
ptrav=ptrav->pleft;}
else{
ptrav=ptrav->pright;}}
if(c<(pa->ch)){
pa->pleft=pnew;}
else{
pa->pright=pnew;}}return;}}
void create(){
char k,f;
int i,n;
printf("enter number of characters\n");scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%c",&f);
scanf("%c",&k);
insert(k);
}}
void push(struct Tnode *ch1){
top++;
stack[top]=ch1;
return;
}
struct Tnode *pop(){
struct Tnode* k;
if(top<0){
k=NULL;return k ;}
else
{

k=stack[top];
top--;
return k;
}
}
void print(){
char l;
int done=0;
struct Tnode *ptemp=NULL,*ptr=root;
push(ptr);
if(!root){printf("empty tree\n");
}else{
printf("NON Recursive:\n");
while(top>=0){
// ptemp=ptr;
          ptr=pop();
printf("%c",ptr->ch);
 if((ptr->pright)!=NULL){
ptemp=ptr->pright;
// printf("%c",ptr->ch);
push(ptemp);}
if((ptr->pleft)!=NULL){
ptemp=ptr->pleft;
// printf("%c",ptr->ch);
push(ptemp);
}
}
}}

POLYNOMIAL ADDITION USING LINKED LIST IN C

#include <stdio.h>
#include <stdlib.h>
struct Node{
int co,po;
struct Node*plink;
};
struct head{
struct Node*pfirst;
};
struct head *pl1,*pl2,*pl3;
void createlist(struct head*);
struct Node* create();
struct Node* mem(int,int);
void disp(struct head *);
void insert(struct head*,struct Node*);
void Add(struct head*,struct head*,struct head*);
int main() {
pl1=(struct head*)malloc(sizeof(struct head));
pl1->pfirst=NULL;
pl2=(struct head*)malloc(sizeof(struct head));
pl2->pfirst=NULL;
pl3=(struct head*)malloc(sizeof(struct head));
pl3->pfirst=NULL;
if(pl1&&pl2&&pl3){
printf("  enter poly exp1:\n");
createlist(pl1);
printf("  enter poly exp2:\n");
createlist(pl2);
printf("poly 1:\n");
disp(pl1);
printf("\n");
printf("poly 2:\n");
disp(pl2);
printf("\n");
Add(pl1,pl2,pl3);
printf("poly exp after adding is\n");
disp(pl3);

}
return 0;
}
struct Node* create(){
struct Node* ptemp=(struct Node*)malloc(sizeof(struct Node));
if(!ptemp) return NULL;
else{
printf("enter Coefficient,power\n");scanf("%d%d",&(ptemp->co),&(ptemp->po));ptemp->plink=NULL;return ptemp;
}
}
void createlist(struct head*ptr){struct Node* ptemp=NULL;
int i,n=0;
printf("enter highest power of expression\n");
scanf("%d",&n);for(i=0;i<=n;i++)
{
ptemp=create();
insert(ptr,ptemp);
}
return;}
void insert(struct head*ptr1,struct Node*p1)
{
if(!p1){
printf("no memory \n");
return;
}
else{
struct Node *ptemp1=NULL;
if(!(ptr1->pfirst)){
ptr1->pfirst=p1;
}
else
{
ptemp1=ptr1->pfirst;
while(ptemp1->plink)
ptemp1=ptemp1->plink;
ptemp1->plink=p1;
}
return;
}
}
struct Node* mem(int c,int p){
struct Node* ptr=(struct Node*)malloc(sizeof(struct Node));
if(!ptr)
return NULL;
else{
ptr->co=c;
ptr->po=p;ptr->plink=NULL;return ptr;
}}
void disp(struct head*ptr2){
struct Node*ptemp=NULL;
if(!ptr2->pfirst){
printf("empty polynomial\n");return;
}
else{
int i=0;
ptemp=ptr2->pfirst;
do{
if(((ptemp->co)>0)&&i!=0){
printf("+");
}
printf("%dx^%d",ptemp->co,ptemp->po);
ptemp=ptemp->plink;
i++;
}while(ptemp);
}
return;
}
void Add(struct head*pt1,struct head*pt2,struct head*pt3){
struct Node *ptl1=NULL,*ptl2=NULL,*ptl3=NULL;
ptl1=pt1->pfirst; ptl2=pt2->pfirst;
int a;
while((ptl1)&&(ptl2)){
if((ptl1->po)>(ptl2->po)){
ptl3=mem(ptl1->po,ptl1->co);
ptl1=ptl1->plink;
insert(pt3,ptl3);
}
else if((ptl2->po)>(ptl1->po)){
ptl3=mem(ptl2->po,ptl2->co);
ptl2=ptl2->plink;
insert(pt3,ptl3);
}
else{
a=(ptl1->co)+(ptl2->co);
ptl3=mem(a,ptl1->po);
ptl1=ptl1->plink; ptl2=ptl2->plink;insert(pt3,ptl3);
}
}
while(ptl1){
ptl3=mem(ptl1->po,ptl1->co);
ptl1=ptl1->plink;
insert(pt3,ptl3);
}
while(ptl2){
ptl3=mem(ptl2->po,ptl2->co);
ptl2=ptl2->plink;
insert(pt3,ptl3);
}
return;
} 🔜


HEAP SORT USING C PROGRAM

# include<stdio.h>
#define Max 50
void heapup(int arr[],int,int);
void heap(int arr[],int n);
int main(){
int Heap[Max],n,i,j;
printf("enter number of elements in array\n");
scanf("%d",&n);
printf("enter  elements of array\n");
for(i=1;i<=n;i++)
{
scanf("%d",&Heap[i]);
heapup(Heap,i,n);
}

heap(Heap,n);
for(i=1;i<=n;i++){
printf("%d\t",Heap[i]);
}
return 0;
}
void heapup(int arr[],int i,int n){
int temp=arr[i],t,j=2*i;
while(j<=n){
if((j<n)&&(arr[j]<arr[j+1]))
j=j+1;
if(temp>arr[j])
break;
arr[j/2]=arr[j];
j=2*j;
}
arr[j/2]=temp;
}
void heap(int arr[],int n){
int t,k=n;
do{
t=arr[k];
arr[k]=arr[1];
arr[1]=t;
k--;
heapup(arr,1,k);
}while(k>0);
}

NEWTON BACKWARD INTERPOLATION USING C PROGRAM

#include<stdio.h>
int main()
{
int n;
printf("enter size");
scanf("%d",&n);
float ax[n];
float ay[n];
int i,j;
for(i=0;i<n;i++)
{
printf("enter ax[%d]\n",i);
scanf("%f",&ax[i]);
printf("enter ay[%d]\n",i);
scanf("%f",&ay[i]);
}
float d;
printf("enter intermediate value of x");
scanf("%f",&d);
float y=1,p;
p=(d-ax[n-1])/(ax[1]-ax[0]);
float a[n][n];
for(i=0;i<n;i++)
{
a[i][0]=ay[i];
}
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
{
a[j][i]=a[j+1][i-1]-a[j][i-1];
}
}
float g=ay[n-1];
for(i=1;i<n;i++)
{
y=(y*(p+i-1))/i;
g=g+(y*a[n-i-1][i]);
}
printf("value of y=%f ",g);
return 0;
}

C PROGRAM FOR NON RECURSIVE POST ORDER TRAVERSAL

#include<stdio.h>
#include<stdlib.h>
struct Tnode{
struct Tnode*pleft;
struct Tnode*pright;char ch;
};
struct Tnode *stack[50];
struct Tnode *stack1[50];
int top,top1;
void push(struct Tnode* ch1);
struct Tnode* pop();
struct Tnode*root;
struct Tnode*create_node(char c);
void create();
void insert(char c);
void print();
void push1(struct Tnode *ch1);
struct Tnode *pop1();
//main function
int main(){
top=-1;root=NULL;top1=-1;
create();
printf("\n");
print();
return 0;}//main end

struct Tnode*create_node(char c){
struct Tnode*pnew=(struct Tnode*)malloc(sizeof(struct Tnode));
if(pnew){
pnew->ch=c;
pnew->pleft=NULL;
pnew->pright=NULL;}
return pnew;}
void insert(char c){
struct Tnode *pnew,*ptrav,*pa;
pnew=create_node(c);
if(!pnew) printf("no memeory\n");
else{
if(!root)
root=pnew;
else{
pa=root;ptrav=root;
while(ptrav){
pa=ptrav;
if(c<(ptrav->ch)){
ptrav=ptrav->pleft;}
else{
ptrav=ptrav->pright;}}
if(c<(pa->ch)){
pa->pleft=pnew;}
else{
pa->pright=pnew;}}return;}}//inserrt end

void create(){
char k,f;
int i,n;
printf("enter number of characters\n");scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%c",&f);
scanf("%c",&k);
insert(k);
}}//creaate end

void push(struct Tnode *ch1){
top++;
stack[top]=ch1;
return;
}//push end

struct Tnode *pop(){
struct Tnode* k;
if(top<0){
k=NULL;return k ;}
else
{

k=stack[top];
top--;
return k;
}
}//pop end

void push1(struct Tnode *ch1){//thi function is for stack1 operations
top1++;
stack1[top1]=ch1;
return;
}//push1 end

struct Tnode *pop1(){
struct Tnode* k;
if(top1<0){
k=NULL;return k ;}
else
{

k=stack1[top1];
top1--;
return k;
}
}//pop1 end

//printing non recursively using print function
void print(){
char l;
int done=0;
struct Tnode *ptemp=NULL,*ptr=root;
push(ptr);
if(!root){printf("empty tree\n");
}else{
printf("NON Recursive:\n");
while(top>=0){
          ptr=pop();
push1(ptr);
if((ptr->pleft)!=NULL){
ptemp=ptr->pleft;
push(ptemp);}
 if((ptr->pright)!=NULL){
ptemp=ptr->pright;
push(ptemp);}
}
}
while(top1>=0){
ptemp=pop1();
printf("%c",ptemp->ch);
}
}//end print

Tuesday, 26 September 2017

C PROGRAM TO PERFORM CONCATENATION OF TWO SINGLY LINKED LISTS

#include<stdio.h>
#include<stdlib.h>
struct num
{
int a;
struct num*plink;
};
struct list
{
int n;
struct num*pfirst;
};
struct list*pl1,*pl2;
struct num* create();
void insert(struct list*,struct num*);
void listcon(struct list*,struct list*);
void print(struct list*);
void createlist(struct list*);

int main()
{
pl1=(struct list*)malloc(sizeof(struct list));
pl2=(struct list*)malloc(sizeof(struct list));
if((!pl1)||(!pl2))
{
printf("no memory\n");
return 0;
}
else
{
pl1->n=0;
pl1->pfirst=NULL;
pl2->n=0;
pl2->pfirst=NULL;
printf("creating list for list 1\n");
printf("\n");
createlist(pl1);
printf("creating list for list 2\n");
printf("\n");
createlist(pl2);
listcon(pl1,pl2);
print(pl1);
printf("\n");
return 0;
}
}
struct num* create()
{
struct num *ptemp=NULL;
ptemp=(struct num*)malloc(sizeof(struct num));
if(!ptemp)
return NULL;
else
{
printf("enter integer\n");
scanf("%d",&(ptemp->a));
ptemp->plink=NULL;
return ptemp;
}
}
void insert(struct list*pl,struct num*pn)
{
struct num*ptemp=NULL;
if(!(pl->pfirst))
{
pl->pfirst=pn;
}
else
{
ptemp=pl->pfirst;
while(ptemp->plink)
{
ptemp=ptemp->plink;
}
ptemp->plink=pn;
}
return;
}
void createlist(struct list*ptr)
{
struct num*pnew=NULL;
int i,n=0;
printf("enter number of elements to be inserted\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pnew=create();
insert(ptr,pnew);
}
return;
}
void listcon(struct list*l1,struct list*l2)
{
if((!l1)||(!l2))
{
printf("concatention cannot be done\n");
return;
}
else
{
struct num*ptemp=NULL,*ptrav=NULL,*plast=NULL;
plast=l1->pfirst;
ptrav=l2->pfirst;
while(plast->plink)
{
plast=plast->plink;
}
do
{
ptemp=(struct num*)malloc(sizeof(struct num));
if(!ptemp)
{
printf("concatention cannot be done\n");
return;
}
else
{
ptemp->a=ptrav->a;
ptemp->plink=NULL;
plast->plink=ptemp;
plast=plast->plink;
ptrav=ptrav->plink;
}
}while(ptrav);
}
}
void print(struct list*plk)
{
struct num*ptemp=NULL;
if(plk->pfirst==NULL)
{
printf("EMPTY LIST\n");
return;
}
else
{
ptemp=plk->pfirst;
while(ptemp!=NULL)
{
printf("%d\t",ptemp->a);
ptemp=ptemp->plink;
}
return;
}
}


C PROGRAM TO FIND 2'S COMPLEMENT

#include<string.h>
#include<stdio.h>
int main()
{
  int n,k,c=0,g='1',i,l=0;
printf("enter size");
scanf("%d",&k);
getchar();
char s[k];
char s1[k];
printf("enter string\n");
gets(s);
n=strlen(s);
for(i=0;i<n;i++)
{
if(s[i]=='0' || s[i]=='1')
        c++;
else
l++;
}
if(c!=n)
printf("no binary digits");
else
{
for(i=0;i<n;i++)
{
if(s[i]=='0')
       s1[i]='1';
else
s1[i]='0';
}
//puts(s1);
for(i=n-1;i>=0;i--)
{
          if(s1[i]=='1'&& g=='1')
{
      s[i]='0';
}
else if(s1[i]=='0'&& g=='1')
{
s[i]='1';
g='0';
}
else
{
s[i]=s1[i];
}
}
s[n]='\0';
printf("2's complement is:");
puts(s);
}
return 0;
}
OUTPUT:

Monday, 25 September 2017

C PROGRAM TO PERFORM MERGE SORT

#include<stdio.h>
int a[100];
int t[100];
void merge(int low,int high);
void sort(int l,int h);
int main()
{
int i,n;
printf("enter size");
scanf("%d",&n);
printf("enter elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
merge(0,n-1);
printf("sorted array is ");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}
void merge(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge(low,mid);
merge(mid+1,high);
sort(low,high);
}
return;
}
void sort(int l,int h)
{
int mid=(l+h)/2;
int l1=l,l2=mid+1,i=l;
for(;l1<=mid&&l2<=h;i++)
{
if(a[l1]<a[l2])
{
t[i]=a[l1];
l1++;
}
else
{
t[i]=a[l2];
l2++;
}

}
while(l1<=mid)
{
t[i]=a[l1];
l1++;
i++;
}
while(l2<=h)
{
t[i]=a[l2];
l2++;
i++;
}
for(i=l;i<=h;i++)
{
a[i]=t[i];
}
return;

}
OUT PUT:



Friday, 24 February 2017

C PROGRAM GAUSS FORWARD

GAUSS FORWRD INTERPOLATION
#include<stdio.h>
int main()
{
int n;
printf("enter size");
scanf("%d",&n);
float ax[n];
float ay[n];
int i,j;
for(i=0;i<n;i++)
{
printf("enter ax[%d]\n",i);
scanf("%f",&ax[i]);
printf("enter ay[%d]\n",i);
scanf("%f",&ay[i]);
}
float d,m,q;
int r=0;
printf("enter intermediate value of x");
scanf("%f",&d);
r=0;
do
{
r++;
}while(ax[r]<d);
r--;
m=ax [r];
float y=1,p;
p=(d-m)/(ax[1]-ax[0]);
printf("%f\n",p);
float a[n][n];
for(i=0;i<n;i++)
{
a[i][0]=ay[i];
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
a[j][i]=a[j+1][i-1]-a[j][i-1];
}
}
for(i=0;i<n;i++)
{
printf(" %f ",ax[i]);
for(j=0;j<n-i;j++)
{
printf(" %f ",a[i][j]);
}
printf("\n");
}
float g=ay[r];
int c=1,h=1;
for(i=1;i<n;i++)
{
     if(i==1)
{
y=(y*(p-i+1))/i;
g=g+(y*a[r-i][i]);
printf("%f\n",g);
}
else if(i%2==0)
{
y=(y*(p+i-1))/i;
g=g+(y*a[r-h][i]);
printf("%f\n",g);
}
else
{
y=(y*(p-i-1))/i;
g=g+(y*a[r-h][i]);
h++;
printf("%f\n",g);
}
}
printf("value of y=%f ",g);
return 0;
}

FERMATS LITTLE THEOREM

import java.math.*; import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) {    Sca...