The Programming Project: C code
Showing posts with label C code. Show all posts
Showing posts with label C code. Show all posts

Friday, October 10, 2014

Insertion Sort : Python Code & Linked List implementation

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
Sorting is typically done in-place, by iterating up the array, growing the sorted list behind it. At each array-position, it checks the value there against the largest value in the sorted list (which happens to be next to it, in the previous array-position checked). If larger, it leaves the element in place and moves to the next. If smaller, it finds the correct position within the sorted list, shifts all the larger values up to make a space, and inserts into that correct position.


Python Code

import random
def InsertionSort():
    N = input("Enter the number of elements in the list:")
    unsortedList = [0 for i in range(N)]
    for i in range(N):
        """ user input can be taken here"""
        unsortedList[i] = int(random.random()*N)
        i = i + 1
    print 'Unsorted list is:',unsortedList
    i = 0
    sortedList = [0 for i in range(N)]
    sortedList[0] = unsortedList[0]
    """ counter holds the number of elements inserted in the sorted list"""
    counter = 1
    for i in range (N-1):
        i = i + 1
        temp = counter
        while unsortedList[i] < sortedList[counter-1] and counter > 0:
            sortedList[counter] = sortedList[counter-1]
            counter = counter - 1
        sortedList[counter] = unsortedList[i]
        temp = temp+1
        counter = temp
    print 'Sorted list is:',sortedList           
InsertionSort()   

Friday, September 20, 2013

Permutation in Lexicographical Order

This C program prints the permutation of n distinct objects (0 < n <= 26 )  in Lexicographical Order.



#include<stdio.h>
#include<malloc.h>
void sort(char *permutation,int n,int pos);
void replace_min (char *permutation,int n,int pos);
int main()
{
char alpha[]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
          'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int n,i,flag=1,pos;
long int tot=0;
char *permutation;
printf("\n Enter the numbr of objects (max values is 26):");
scanf("%d",&n);
permutation =(char *)malloc(n*sizeof(char));
for(i=0;i<n;i++)
    permutation[i]=alpha[i];
printf("\n The permutations are:\n");
printf("\t\t      ");
for(i=0;i<n;i++)
    printf("%c",permutation[i]);
    tot=1;
while(flag)
    {
    pos=-1;
    for(i=n-1;i>=1;i--)
        {
        if(permutation[i-1]<permutation[i])
             {
             pos=i-1;
             flag=1;
             break;
             }
        }
        if(pos==-1)
            {
            flag=0;
            break;
            }
    replace_min(permutation,n,pos);
    sort(permutation,n,pos);
   printf("\n");
    printf("\t\t      ");
    for(i=0;i<n;i++)
        printf("%c",permutation[i]);
    //tot++;
    //getche();
    }
//printf("\n Total number of permutation is: %ld",tot);
return 0;
}
void sort(char *permutation,int n,int pos)
{
    int i,j;
   char temp='A';
    for(i=pos+1;i<n-1;i++)
        {
        for(j=pos+1;j<n-1;j++)
            {
            if(permutation[j]>permutation[j+1])
                {
                temp=permutation[j+1];
                permutation[j+1]=permutation[j];
                permutation[j]=temp;
                }
            }
        }
  return;
}
void replace_min(char *permutation,int n,int pos)
{
  char min='Z',temp;
  int k,min_pos=0;
  for(k=pos+1;k<n;k++)
    {
        if(permutation[k]>permutation[pos])
            {
            if(permutation[k]<min)
                {
                min=permutation[k];
                min_pos=k;
                }
            }
    }
    temp=permutation[min_pos];
    permutation[min_pos]=permutation[pos];
    permutation[pos]=temp;
    return;
}

Wednesday, September 4, 2013

Newton's Forward Interpolation : Numerical Analysis C Program

Newton's Forward Interpolation : Numerical Analysis C Program

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void differenceTable(double **A,double **L,int ROW);
double result(double **A,double **L,double x,int ROW);
double factorial(int n);
FILE *fp;
int main(int argcchar *argv[]) {
double **A,*b,*xn,*xnplus1,*p,temp,x,**L,**U,lambda,error,t1=0.0;
int ROW,i,j,k,t=1,m=1,bo=1;
char ch;
if(argc==1) { 
    printf("\n Enter the number of values:");
    scanf("%d",&ROW);
    printf("\n Enter the value at which value has to be aprroximated:");
    scanf("%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    L=(double **)malloc((ROW+1)*sizeof(double*));
    U=(double **)malloc((ROW+1)*sizeof(double*));
    for(i=0;i<=ROW;i++) {
        A[i]=(double *)malloc((ROW+1)*sizeof(double));
        L[i]=(double *)malloc((ROW+1)*sizeof(double));
        U[i]=(double *)malloc((ROW+1)*sizeof(double));
    }
 b=(double *)malloc((ROW+1)*sizeof(double));
 p=(double *)malloc((ROW+1)*sizeof(double));
 xn=(double *)malloc((ROW+1)*sizeof(double));
 xnplus1=(double *)malloc((ROW+1)*sizeof(double));
 for(i=1;i<=ROW;i++)  {
    printf("\n Enter the value of X[%d]:",i);
    scanf("%lf",&A[i][1]);
 }
 for(i=1;i<=ROW;i++) {
    printf("\n Enter the value of Y[%d]:",i);
    scanf("%lf",&A[i][2]);
 }
  
}
else if (argc==2) {
    fp=fopen(argv[1],"r");
    if(fp==NULL) {
        printf("\n File not found, program will terminate:");
        exit(0);
    }
    fscanf(fp,"%d",&ROW);
    fscanf(fp,"%lf",&x);
    A=(double **)malloc((ROW+1)*sizeof(double*));
    L=(double **)malloc((ROW+1)*sizeof(double*));
    U=(double **)malloc((ROW+1)*sizeof(double*));
    for(i=0;i<=ROW;i++) {
        A[i]=(double *)malloc((ROW+1)*sizeof(double));
        L[i]=(double *)malloc((ROW+1)*sizeof(double));
        U[i]=(double *)malloc((ROW+1)*sizeof(double));
    }
    b=(double *)malloc((ROW+1)*sizeof(double));
    p=(double *)malloc((ROW+1)*sizeof(double));
    xn=(double *)malloc((ROW+1)*sizeof(double));
    xnplus1=(double *)malloc((ROW+1)*sizeof(double));
    while(!feof(fp)) {
    for(i=1;i<=ROW;i++) {
        for(j=1;j<=2;j++) {
            fscanf(fp,"%lf ",&A[i][j]);
            }
        }
    }  
 fclose(fp);
 }
 else {
    printf("\n Invalid Arguments,program will terminate:\n");
    exit(0);
}
printf("\n         X          Y");
printf("\n------------------------------------------------------------------\n");
for(i=1;i<=ROW;i++) {
    for(j=1;j<=2;j++) {
        U[i][j]=A[i][j];
        printf("   %+lf",U[i][j]);
        }
    printf("\n");
}
for(i=1;i<=ROW-1;i++) {
    for(j=ROW-i,t=1;j>=1;j--,t++)  {
        L[t][i]=U[t+1][2]-U[t][2];
        }
 for(t=1;t<=ROW-i;t++)
    U[t][2]=L[t][i];
differenceTable(A,L,ROW);
printf("\n Value at %+lf by Newton's Forward Interpolation formula is %+lf\n",x,result(A,L,x,ROW));
return 0;
}
double result(double **A,double **L,double x,int ROW) {
int i,j;
double h,value=0,p,tmp=1.0;
h=A[2][1]-A[1][1];
p=(x-A[1][1])/h;
for(i=0;i<ROW;i++){
    if(i==0)
    value +=A[1][2];
    else {
        for(j=1;j<=i;j++)
        tmp=tmp*(p-j+1);
        tmp = tmp/factorial(i);
        tmp = tmp*L[1][i];
        value +=tmp;
    }
    if(i==0)
        printf("%2.2lf ",A[1][2]);
    else
        printf("%2.2lf ",L[1][i]);
    tmp=1.0;
    }
    return value;
}
double factorial(int n) {
    if(n<=1)
        return 1;
    else
        return n*factorial(n-1);
}
void differenceTable(double **A,double **L,int ROW) {
    int **position,*ap,j,m,i;
    position=(int **)malloc((ROW+1)*sizeof(int*));
    ap=(int *)malloc((ROW+1)*sizeof(int));
    for(i=0;i<=ROW;i++)
        position[i]=(int *)malloc((ROW+1)*sizeof(int));
    int an,tmp; 
    tmp=ROW;
    for(i=1;i<=ROW;i++) {
        an=1+(i-1)*2;
        ap[i]=an;
        for(j=1;j<=tmp;j++)  {
            position[i][j]=an+(j-1)*4;
            }
        tmp--;
    }
    tmp=ROW;
    tmp=ROW+(ROW-1)*3;
    int *match,tmp1,l,*pos,flag,z,k;
    match=(int *)malloc((ROW+1)*sizeof(int));
    pos=(int *)malloc((ROW+1)*sizeof(int));
    for(i=0;i<=ROW;i++) {
        match[i]=0;
        pos[i]=0;
        }
printf("\n-----------------------------Difference Table-----------------------------\n");
printf("\n X       Y");
for(i=1;i<=ROW-1;i++)
    printf("      D^%d",i);
printf("\n--------------------------------------------------------------------------\n");
for(i=1;i<=tmp;i++) {
    tmp1=ROW;
     for(l=1;l<=ROW;l++) {
        flag=0;
        for(m=1;m<=tmp1;m++)  {
            if(i==position[l][m])   {
                flag=1;
                match[l]=1;
                pos[m]=position[l][m];
                break;
                }
            } //inner for
        if(flag==1)        {
            for(k=1;k<=ROW;k++)  {
                if(match[k]==0) {
                    printf("");
                }
        else  {
            if(k==1)
            //printf("(%d,%d)+(%d,%d)|%d",(position[l][m]/4)+1,k,(position[l][m]/4)+1,k+1,i);
                printf("%+2.2lf\t%+2.2lf",A[(position[l][m]/4)+1][k],A[(position[l][m]/4)+1][k+1]);
            else   {
                z=(position[l][m]-ap[k])/4+1;
                //printf("\t\t(%d,%d)|%d",z,k-1,i);
                printf("\t\t%+2.2lf",L[z][k-1]);
                }
            }
        } //end of k-loop     
    } //end of flag==1
    else
        {printf(" ");}
    tmp1--;
   for(k=0;k<=ROW;k++) {
        pos[k]=0;
        match[k]=0;
        }
    }
    printf("\n");
    }
printf("\n--------------------------------------------------------------------------\n");
}