Thursday 29 March 2012

tips nd tricks section added soon

Tips nd tricks section added soon. Pls give ur comment nd suggestion to improve this blog.

Tuesday 27 March 2012

Program to implement LAPLACE - EVERETT'S INTERPOLATION FORMULA in C



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

void main()
{
int n;                          // no. of terms.
int i,j;                        // Loop variables
float ax[10];                   // 'X' array limit 9
float ay[10];                   // 'Y' array limit 9
float x;                        // User Query for what value of X
float nr,dr;
float y=0;                      // Calculated value for coressponding X.
float h;                        // Calc. Section
float p,q;                      // Calc. Section
float diff[20][20];             // to store Y
float y1,y2,y3,y4;              // Formulae variables.
float py1,py2,py3,py4;

clrscr();
printf("\t\t !! LAPALCE-EVERETT'S INTERPOLATION FORMULA !! ");
// Input section.
printf("\n\n Enter the no. of terms -> ");
scanf("%d",&n);

// Input Sequel for array X
printf("\n\n Enter the value in the form of x -> ");
// Input loop for X.
for(i=0;i<n;i++)
   {
   printf("\n Enter the value of x%d -> ",i+1);
   scanf("%f",&ax[i]);
   }

// Input sequel for array Y.
printf("\n\n Enter the value in the form of y -> ");
// Input loop for Y.
for(i=0;i<n;i++)
   {
   printf("\n Enter the value of y%d -> ",i+1);
   scanf("%f",&ay[i]);
   }

// Inputting the required value quarry
printf("\n\n Enter the value of x for ");
printf("\n which u want the value of y -> ");
scanf("%f",&x);

// Calculation and processing section.
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
   diff[i][1]=ay[i+1]-ay[i];

for(j=2;j<=4;j++)
   for(i=0;i<n-j;i++)
      diff[i][j]=diff[i+1][j-1]-diff[i][j-1];

i=0;
do
 {
 i++;
 }
while(ax[i]<x);

i--;
p=(x-ax[i])/h;
q=1-p;
y1=q*(ay[i]);
y2=q*(q*q-1)*diff[i-1][2]/4;
y3=q*(q*q-1)*(q*q-4)*diff[i-2][4]/120;
py1=p*(ay[i+1]);
py2=p*(p*p-1)*diff[i][2]/6;
py3=p*(p*p-1)*(p*p-4)*diff[i-1][4]/120;

// Taking sum
y=ay[i]+y1+y2+y3+py1+py2+py3;

// Outut Section
printf("\n When x = %6.2f , y = %6.8f",x,y);

// Invoke user watch halt function
printf("\n\n\n\t\t\t !! PRESS ENTER TO EXIT !! ");
getch();
}

program in to implement Gauss Seidal method



# include <stdio.h>
# include <conio.h>
# include <math.h>
# define MAXIT 50
# define EPS .000001
void main()
{
float a[10][10],b[10],x[10],sum,x0[10];
int i,j,n,count=0;
clrscr();
printf("Enter Number of equation : ");
scanf("%d",&n);
printf("Enter Coefficients row by row \n");
printf("One row in each line\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&a[i][j]);
printf("Enter vector elements\n");
for(i=1;i<=n;i++)
{
x0[i]=x[i]=0;
scanf("%f",&b[i]);
}
printf("N\t");
for(i=1;i<=n;i++)
printf("X%d \t",i);
printf("\n");
printf("-------------------------------------------------------------------\n");
s: printf("%d\t",count+1);
for(i=1;i<=n;i++)
{
sum=0;
for(j=1;j<=n;j++)
{
if(i==j)
continue;
else
sum=sum+(a[i][j]*x[j]);
}
x[i]=(b[i]-sum)/a[i][i];
printf("%f \t",x[i]);
}
count++;
if(fabs(x[n]-x0[n])>EPS && count<MAXIT)
{
for(i=1;i<=n;i++)
x0[i]=x[i];
printf("\n");
goto s;
}
else
{
printf("\n----------------------------------------------\nSolution : \n");
for(i=1;i<=n;i++)
printf("X%d = %3.4f\t",i,x[i]);
}
getch();
}

Newton Divided difference formula



# include <stdio.h>
# include <conio.h>
# define MAX 20
void main()
{
int i,j,n;
float x[MAX],fx[MAX][MAX],xp,u,sum=0;
printf("Newton Divided Difference formula \n");
printf("Enter No. of data points : ");
scanf("%d",&n);
printf("Enter values of X and f(x) set by set each at once \n");
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&fx[i][1]);
printf("Enter value of X at which interpolation is required : ");
scanf("%f",&xp);
for(i=2;i<=n;i++)
for(j=1;j<=n-i+1;j++)
fx[j][i]=(fx[j+1][i-1]-fx[j][i-1])/(x[j+i-1]-x[j]);
printf("-------------------------------------------------------------------\n");
printf("X F(x) ");
for(i=1;i<n;i++)
printf("y%d ",i);
printf("\n-----------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%4.2f\t ",x[i]);
for(j=1;j<=n-i+1;j++)
printf("%4.2f ",fx[i][j]);
printf("\n");
}
printf("\n-----------------------------------------------------------------\n");
sum=fx[1][1];
for(i=2;i<=n;i++)
{
u=1;
for(j=1;j<i;j++)
u=u*(xp-x[j]);
u=u*fx[1][i];
sum=sum+u;
}
printf("For %3.2f Interpolated value = %4.2f",xp,sum);
getch();
}

Monday 26 March 2012

first experience of blogging

This blog is for all computer learners. my goal to make this blog for c beginners and data structure students. i will try my best to update this on daily basis, so plz visit this . and say that you like this or not. and how can i make this better. thanx.

Sunday 25 March 2012

Program to implement BESSELS INTERPOLATION FORMULA in C


/*
  Program to implement BESSELS INTERPOLATION FORMULA in C.
      ------------------------------------
*/

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

void main()
{
int n;                          // no. of terms.
int i,j;                        // Loop variables
float ax[10];                   // 'X' array limit 9
float ay[10];                   // 'Y' array limit 9
float x;                        // User Query for what value of X
float y;                      // Calculated value for coressponding X.
float h;                        // Calc. Section
float p;                        // Calc. Section
float diff[20][20];             // to store Y
float y1,y2,y3,y4;              // Formulae variables.

clrscr();
printf("\t\t !! BESSELS INTERPOLATION FORMULA !! ");
// Input section.
printf("\n\n Enter the no. of terms -> ");
scanf("%d",&n);

// Input Sequel for array X
printf("\n\n Enter the value in the form of x -> ");
// Input loop for X.
for(i=0;i<n;i++)
   {
   printf("\n Enter the value of x%d -> ",i+1);
   scanf("%f",&ax[i]);
   }

// Input sequel for array Y.
printf("\n\n Enter the value in the form of y -> ");
// Input loop for Y.
for(i=0;i<n;i++)
   {
   printf("\n Enter the value of y%d -> ",i+1);
   scanf("%f",&ay[i]);
   }

// Inputting the required value quarry
printf("\n\n Enter the value of x for ");
printf("\n which u want the value of y -> ");
scanf("%f",&x);

// Calculation and processing section.
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
   diff[i][1]=ay[i+1]-ay[i];

for(j=2;j<=4;j++)
   for(i=0;i<n-j;i++)
      diff[i][j]=diff[i+1][j-1]-diff[i][j-1];

i=0;
do
 {
 i++;
 }
while(ax[i]<x);

i--;
p=(x-ax[i])/h;
y1=p*diff[i][1];
y2=p*(p-1)*(diff[i][2]+diff[i-1][2])/4;
y3=p*(p-1)*(p-0.5)*diff[i-1][3]/6;
y4=(p+1)*p*(p-1)*(p-2)*(diff[i-2][4]+diff[i-1][4])/48;

// Taking sum
y=ay[i]+y1+y2+y3+y4;

// Outut Section
printf("\n When x = %6.2f , y = %6.8f",x,y);

// Invoke user watch halt function
printf("\n\n\n\t\t\t !! PRESS ENTER TO EXIT !! ");
getch();
}

program to create two way linked list


program to create two way linked list.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define NULL 0
void main()
{
struct dnode
{
struct dnode *prev;
int data;
struct dnode *next;
}*start,*temp,*k,*p,*pre;
int i,c;
char ch;
clrscr();
start=NULL;
for(i=0;i<=1;i++)
{
if(start==NULL)
{
temp=(struct dnode*)malloc(sizeof(struct dnode));
printf("\nEnter the value for node: -");
scanf("%d",&temp->data);
start= temp;
temp->prev=NULL;
temp->next=NULL;
}
else
{
while(1)
{
printf("\nDo you want to create one more node(Y/N):- ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='y' || ch=='Y')
{
temp->next=(struct dnode*)malloc(sizeof(struct dnode));
printf("\nEnter the value of  node: -");
k=temp->next;
scanf("%d",&k->data);
k->prev=temp;
k->next=NULL;
temp=k;
}
else
{
temp->next=NULL;
break;
}
}
}
}
//For forward Displaying the linked list.
printf("\n\n\nBy forward Displaying: -");
printf("\nNodes of linked list are-\n");
temp=start;
while(temp!=NULL)
{
printf("%4d",temp->data);
temp=temp->next;
}
//For backward Displaying the linked list.
printf("\n\n\nBy backward Displaying:");
printf("\nNodes of linked list are-\n");
temp=k;
while(temp!=NULL)
{
printf("%4d",temp->data);
temp=temp->prev;
}
getch();
}

output:
Enter the value for node: -14
Do you want to create one more node(Y/N):- y
Enter the value of  node: -63
Do you want to create one more node(Y/N):- y
Enter the value of  node: -87
Do you want to create one more node(Y/N):- y
Enter the value of  node: -69
Do you want to create one more node(Y/N):- y
Enter the value of  node: -100
Do you want to create one more node(Y/N):- n
By forward Displaying: -
Nodes of linked list are-
  14  63  87  69 100
By backward Displaying:
Nodes of linked list are-
 100  69  87  63 14

program to delete the element from linked list after the given value


program to delete the element from linked list after the given value.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
 {
  int data;
  struct node *link;
 }*start,*temp,*prev,*n1;
void display();
void main()
 {
  int n,i;
  char ch;
  clrscr();
  start=NULL;
  prev=NULL;
  while(1)
   {
    if(start==NULL)
     {
                temp=(struct node*)malloc(sizeof(struct node));
                printf("\nEnter value for Node:- ");
                scanf("%d",&temp->data);
                start=temp;
                temp->link=NULL;
     }
     else
     {
      temp->link=(struct node*)malloc(sizeof(struct node));
      temp=temp->link;
      printf("\nEnter value for Node:- ");
      scanf("%d",&temp->data);
     }
      printf("\nDo you want to create one more node(Y/N) :- ");
      fflush(stdin);
      scanf("%c",&ch);
      if(ch!='y')
       {
                temp->link=NULL;
                break;
       }
   }
   printf("\n\nThe nodes before Deleting:\n");
   display();
   printf("\n\nEnter node value after which you want to Delete:-");
   scanf("%d",&n);
   temp=start;
   while(temp->data!=n)
    {
     prev=temp;
     temp=temp->link;
    }
   n1=temp->link;
   prev->link=n1;
   temp->link=NULL;
   free(temp);
   printf("\n\nThe nodes after Deleting:\n");
   display();
                getch();
}
void display()
 {
  prev=start;
  while(prev!=NULL)
   {
    printf("%5d",prev->data);
    prev=prev->link;
   }
 }



output:
Enter value for Node:- 45
Do you want to create one more node(Y/N) :- y
Enter value for Node:- 63
Do you want to create one more node(Y/N) :- y
Enter value for Node:- 14
Do you want to create one more node(Y/N) :- y
Enter value for Node:- 95
Do you want to create one more node(Y/N) :- n
The nodes before Deleting:
   45   63   14   95
Enter node value after which you want to Delete:-63
The nodes after Deleting:
   45   14   95

program to delete the element from linked list after the given position


program to delete the element from linked list after the given position.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
 {
  int data;
  struct node *link;
 }*start,*temp,*prev,*n1;
void display();
void main()
 {
  int n,i;
  char ch;
  clrscr();
  start=NULL;
  prev=NULL;
    while(1)
   {
    if(start==NULL)
     {
                temp=(struct node*)malloc(sizeof(struct node));
                printf("\nEnter value for Node :- ");
                scanf("%d",&temp->data);
                start=temp;
                temp->link=NULL;
     }
     else
     {
      temp->link=(struct node*)malloc(sizeof(struct node));
      temp=temp->link;
      printf("\nEnter value for Node :- ");
      scanf("%d",&temp->data);
     }
      printf("\nDo you want to create one more node :- ");
      fflush(stdin);
      scanf("%c",&ch);
      if(ch!='y')
       {
                temp->link=NULL;
                break;
       }
   }
    printf("\nThe nodes before Deleting:\n");
   display();
   printf("\nEnter Place where you want to Delete :- ");
   scanf("%d",&n);
   temp=start;
   for(i=1;i<=n-1;i++)
   {
    prev=temp;
    temp=temp->link;
   }
   n1=temp->link;
   temp->link=NULL;
   prev->link=n1;
   printf("\nthe nodes after Deleting:\n");
   display();
      getch();
}
void display()
 {
  prev=start;
  while(prev!=NULL)
   {
    printf("%5d",prev->data);
    prev=prev->link;
   }
 }


output:
Enter value for Node :- 45
Do you want to create one more node :- y
Enter value for Node :- 65
Do you want to create one more node :- y
Enter value for Node :- 15
Do you want to create one more node :- y
Enter value for Node :- 20
Do you want to create one more node :- n
The nodes before Deleting:
   45   65   15   20
Enter Place where you want to Delete :- 3
the nodes after Deleting:
   45   65   20

program in c to delete the element from linked list of last position element


W.A.P to delete the element from linked list of last position element.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define NULL 0
struct node
{
int data;
struct node *link;
}*start,*temp,*pre;
void display();
void main()
{
int i,q,x;
char ch;
clrscr();
start=NULL;
for(i=0;i<=1;i++)
{
if(start==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter value for node: -");
scanf("%d",&temp->data);
start=temp;
temp->link=NULL;
}
else
{
while(1)
{
printf("\nDo you want create one more node(Y/N):- ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='y' || ch=='Y')
{
temp->link=(struct node *)malloc(sizeof(struct node));
temp=temp->link;
printf("\nEnter value for node: -");
scanf("%d",&temp->data);
}
else
{
temp->link=NULL;
break;
}
}
}
}
printf("\n\n\nThe value of nodes are before delete :\n");
display();
//For delete last node
temp=start;
while(temp->link!=NULL)
{
pre=temp;
temp=temp->link;
}
pre->link=NULL;
free(temp);
printf("\n\nAfter deleting  last node the new nodes are :\n");
display();
getch();
}
void display()
{
temp=start;
while(temp!=NULL)
{
printf("%4d",temp->data);
temp=temp->link;
}
}
output:

Enter value for node: -45
Do you want create one more node(Y/N):- y
Enter value for node: -12
Do you want create one more node(Y/N):- y
Enter value for node: -10
Do you want create one more node(Y/N):- y
Enter value for node: -85
Do you want create one more node(Y/N):- n
The value of nodes are before delete :
  45  12  10  85
After deleting  last node the new nodes are :
  45  12  10

Pogram to delete the element from linked list of first position element


Pogram to delete the element from linked list of first position element.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define null 0
void main()
{
 char c;
 int i,p,j=0;
 struct node
 {
  int data;
  struct node *link;
 }*temp,*start,*n,*prev;
 clrscr();
 start=null;
 for(i=0;i<=1;i++)
 {
  if(start==null)
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("enter data:");
   scanf("%d",&temp->data);
   temp->link=null;
   start=temp;
   j++;
  }
  else
  {
   while(1)
   {
    printf("press n if you not want to add another node");
    fflush(stdin);
    c=getch();
    if(c=='n'||c=='N')
     break;
    else
    {
     temp->link=(struct node*)malloc(sizeof(struct node));
     temp=temp->link;
     printf("\nenter data:");
     scanf("%d",&temp->data);
     temp->link=null;
     j++;
    }
   }
  }
 }
 printf("\ntraversing\n");
 temp=start;
 while(temp->link!=null)
 {
  printf("%d\t",temp->data);
  temp=temp->link;
 }
 printf("%d",temp->data);
 printf("\ndelete link first position:");
 temp=start;
 start=start->link;
 temp->link=null;
 free(temp);
 printf("\ntraversing\n");
 temp=start;
 while(temp->link!=null)
 {
  printf("%d\t",temp->data);
  temp=temp->link;
 }
 printf("%d",temp->data);
 getch();
}
output:
enter data:45
press n if you not want to add another node
enter data:85
press n if you not want to add another node
enter data:56
press n if you not want to add another node n
traversing:
45       85      56
Delete link at from first position
traversing
85           56