Saturday, 14 September 2013

reverse doubly linked list

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

typedef struct node node;

struct node
{
       int info;
       node *pre;
       node *next;
};

node *head=NULL;

node *reverse(node *ptr)
{
   
                   if(ptr->next->next==NULL)
                   {
                                   head=ptr->next;
                                   ptr->next->next=ptr;        
                   }
                   else
                   {
                                   reverse(ptr->next);
                                   ptr->next->next=ptr;
                   }
                   return ptr;
}

main()
{
      FILE *f=fopen("hinput.txt","r");
      char c[5];
      node *ptr,*temp;
      while(fscanf(f,"%s",c)!=EOF)
      {
               ptr=(node*)malloc(sizeof(node));
               ptr->info=atoi(c);    
                                 
               if(head==NULL)
               {
                            head=ptr;
                            ptr->pre=NULL;
                            ptr->next=NULL;
                            temp=head;
               }                  
               else
               {
                            ptr->pre=temp;
                            ptr->next=NULL;
                            temp->next=ptr;
                            temp=temp->next;    
               }
      }
      temp=head;
      while(temp!=NULL)
      {
                       printf("%d\n",temp->info);
                       temp=temp->next;
      }
     
      temp=reverse(head);
      temp->next=NULL;
      temp=head;
      while(temp!=NULL)
      {
                       printf("%d\n",temp->info);
                       temp=temp->next;
      }
     
      getch();
}

Sunday, 23 June 2013

Merge Sort C Code

Merge Sort

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

int c[10],k;

void merge(int *a,int u,int l,int n)
{int i;
     k=u;
     int st=u;
     while(u<l && l<=n)
     {
                      if(a[u]<=a[l])
                      {
                          c[k]=a[u];        
                          u++;
                          k++;
                      }
                      else
                      {
                          c[k]=a[l];
                          k++;
                          l++;
     }
}
     while(u<l)
     {
                c[k]=a[u];        
                u++;
                k++;
     }
     while(l<=n)
     {
                c[k]=a[l];
                k++;
                l++;
     }
    i=st;
     while(i<=n)
     {
                a[i]=c[i];
                i++;
     }
}

void mergesort(int *a,int u,int l)
{
     if(u<l)
     {
     int i=(u+l)/2;
   
     mergesort(a,u,i-1);
     mergesort(a,i,l);
     merge(a,u,i+1,l);
     }
}

 int main()
{
      int a[50];
     
      FILE *f=fopen("hinput.txt","r");
      char c[5];
      int i=0;
      while(fscanf(f,"%s",c)!=EOF)
                                  a[i++]=atoi(c);
      int n=i-1;
      for(;i>=0;i--)
      printf("a[%d]=%d\n",i,a[i]);

      mergesort(a,0,n);


      for(i=0;i<=n;i++)
      printf("c[%d]=%d\n",i,c[i]);
   
         
      fclose(f);
      getch();
      return 0;
}

Quick Sort C Code

Quick Sort

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

void swap(int *a,int *b)
{
     int temp;
     temp=*a;
     *a=*b;
     *b=temp;
}

int par(int *a,int p,int r)
{
    int i=p-1,j=p,pivot;
    pivot=r;
   
    while(j<pivot)
    {
                   if(a[j]<=a[pivot])
                   {
                                    swap(&a[j],&a[i+1]);
                                    i++;            
                                    j++;                      
                   }      
                   else
                   {
                                    j++;
                   }  
    }
    swap(&a[i+1],&a[pivot]);
   
    return i+1;
}

void quicksort(int *a,int p,int r)
{
     int q;
     if(p<r)
     {
           q=par(a,p,r);
           quicksort(a,p,q-1);
           quicksort(a,q+1,r);
     }
}

main()
{
      FILE *f=fopen("hinput.txt","r");
      char c[10];
      int a[10],i=0;
   
       while(fscanf(f,"%s",c)!=EOF)
      {
              a[i++]=atoi(c);              
      }
      int n=i-1;
   
     
       for(i-=1;i>=0;i--)
                        printf("a[%d]=%d\n",n-i,a[n-i]);
      quicksort(a,0,n);
      printf("\n#######\n Quick Sort\n#########\n");
      for(i=0;i<=n;i++)
                        printf("a[%d]=%d\n",i,a[i]);
     
      fclose(f);
      getch();
}

Heap Sort

Heap Sort


#include<stdio.h>
#include<conio.h>
#define MAX 50
int n;
void swap(int *a,int *b)
{
     int temp;
     temp=*a;
     *a=*b;
     *b=temp;
}

void max_heapify(int *a,int x,int n)
{
     int largest=x;
     if(2*x<=n)
              if(a[x]<a[2*x])
                             largest=2*x;
     if((2*x+1)<=n)
               if(a[largest]<a[2*x+1])
                             largest=2*x+1;
     if(x!=largest)
     {
          swap(&a[x],&a[largest]);
          max_heapify(a,largest,n);
     }
}

void build(int *a,int n)
{
     int i;
     for(i=n/2;i>=0;i--)
                        max_heapify(a,i,n);
}
 
void heap_sort(int *a,int n)
{
     int i;

     build(a,n);
     for(i=n;i>=2;i--)
     {
                      swap(&a[n],&a[0]);
                      n--;
                      max_heapify(a,0,n);
     }
}

main()
{
      int a[MAX];
      FILE *f=fopen("hinput.txt","r");
     
      char c[5];
      int i=0;
      printf("Input\n");
      while(fscanf(f,"%s",c)!=EOF)
      {
              a[i++]=atoi(c);              
      }
      n=i-1;

      for(i-=1;i>=0;i--)
                        printf("a[%d]=%d\n",n-i,a[n-i]);
      heap_sort(a,n);
      printf("\n#######\n Heap Sort\n#########\n");
      for(i=0;i<=n;i++)
                        printf("a[%d]=%d\n",i,a[i]);
     
      fclose(f);
      getch();
}

Friday, 19 April 2013

Download Turbo C link


DCS research paper implementation


C- Compiler full project


Software engineering Project

Firefly Algorithm


//============================================================================
// Name        : Firefly.cpp
// Authors     : Dr. Iztok Fister and Iztok Fister Jr.
// Version     : v1.0
// Created on  : Jan 23, 2012
//============================================================================

/* Classic Firefly algorithm coded using C/C++ programming language */

/* Reference Paper*/

/*I. Fister Jr.,  X.-S. Yang,  I. Fister, J. Brest, Memetic firefly algorithm for combinatorial optimization,
in Bioinspired Optimization Methods and their Applications (BIOMA 2012), B. Filipic and J.Silc, Eds.
Jozef Stefan Institute, Ljubljana, Slovenia, 2012 */

/*Contact:
Iztok Fister (iztok.fister@uni-mb.si)
*/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <memory.h>

#define DUMP 1
#define MAX_FFA 1000
#define MAX_D 1000

using namespace std;

int D = 1000; // dimension of the problem
int n = 20; // number of fireflies
int MaxGeneration; // number of iterations
int NumEval; // number of evaluations
int Index[MAX_FFA]; // sort of fireflies according to fitness values

double ffa[MAX_FFA][MAX_D]; // firefly agents
double ffa_tmp[MAX_FFA][MAX_D]; // intermediate population
double f[MAX_FFA]; // fitness values
double I[MAX_FFA]; // light intensity
double nbest[MAX_FFA];          // the best solution found so far
double lb[MAX_D]; // upper bound
double ub[MAX_D]; // lower bound

double alpha = 0.5; // alpha parameter
double betamin = 0.2;           // beta parameter
double gama = 1.0; // gamma parameter

double fbest; // the best objective function

typedef double (*FunctionCallback)(double sol[MAX_D]);

/*benchmark functions */
double cost(double sol[MAX_D]);
double sphere(double sol[MAX_D]);

/*Write your own objective function */
FunctionCallback function = &cost;

// optionally recalculate the new alpha value
double alpha_new(double alpha, int NGen)
{
double delta; // delta parameter
delta = 1.0-pow((pow(10.0, -4.0)/0.9), 1.0/(double) NGen);
return (1-delta)*alpha;
}

// initialize the firefly population
void init_ffa()
{
int i, j;
double r;

// initialize upper and lower bounds
for (i=0;i<D;i++)
{
lb[i] = 0.0;
ub[i] = 2.0;
}

for (i=0;i<n;i++)
{
for (j=0;j<D;j++)
{
r = (   (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
ffa[i][j]=r*(ub[i]-lb[i])+lb[i];
}
f[i] = 1.0; // initialize attractiveness
I[i] = f[i];
}
}

// implementation of bubble sort
void sort_ffa()
{
int i, j;

// initialization of indexes
for(i=0;i<n;i++)
Index[i] = i;

// Bubble sort
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(I[i] > I[j])
{
double z = I[i]; // exchange attractiveness
I[i] = I[j];
I[j] = z;
z = f[i]; // exchange fitness
f[i] = f[j];
f[j] = z;
int k = Index[i]; // exchange indexes
Index[i] = Index[j];
Index[j] = k;
}
}
}
}

// replace the old population according the new Index values
void replace_ffa()
{
int i, j;

// copy original population to temporary area
for(i=0;i<n;i++)
{
for(j=0;j<D;j++)
{
ffa_tmp[i][j] = ffa[i][j];
}
}

// generational selection in sense of EA
for(i=0;i<n;i++)
{
for(j=0;j<D;j++)
{
ffa[i][j] = ffa_tmp[Index[i]][j];
}
}
}

void findlimits(int k)
{
int i;

for(i=0;i<D;i++)
{
if(ffa[k][i] < lb[i])
ffa[k][i] = lb[i];
if(ffa[k][i] > ub[i])
ffa[k][i] = ub[i];
}
}

void move_ffa()
{
int i, j, k;
double scale;
double r, beta;

for(i=0;i<n;i++)
{
scale = abs(ub[i]-lb[i]);
for(j=0;j<n;j++)
{
r = 0.0;
for(k=0;k<D;k++)
{
r += (ffa[i][k]-ffa[j][k])*(ffa[i][k]-ffa[j][k]);
}
r = sqrt(r);
if(I[i] > I[j]) // brighter and more attractive
{
double beta0 = 1.0;
beta = (beta0-betamin)*exp(-gama*pow(r, 2.0))+betamin;
for(k=0;k<D;k++)
{
r = (   (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
double tmpf = alpha*(r-0.5)*scale;
ffa[i][k] = ffa[i][k]*(1.0-beta)+ffa_tmp[j][k]*beta+tmpf;
}
}
}
findlimits(i);
}
}

void dump_ffa(int gen)
{
cout << "Dump at gen= " << gen << " best= " << fbest << endl;
}

/* display syntax messages */
void help()
{
cout << "Syntax:" << endl;
cout << "  Firefly [-h|-?] [-l] [-p] [-c] [-k] [-s] [-t]" << endl;
cout << "    Parameters: -h|-? = command syntax" << endl;
cout << " -n = number of fireflies" << endl;
cout << " -d = problem dimension" << endl;
cout << " -g = number of generations" << endl;
cout << " -a = alpha parameter" << endl;
cout << " -b = beta0 parameter" << endl;
cout << " -c = gamma parameter" << endl;
}

int main(int argc, char* argv[])
{
        int i;
        int t = 1; // generation  counter

         // interactive parameters handling
         for(int i=1;i<argc;i++)
         {
            if((strncmp(argv[i], "-h", 2) == 0) || (strncmp(argv[i], "-?", 2) == 0))
            {
    help();
    return 0;
            }
            else if(strncmp(argv[i], "-n", 2) == 0)         // number of fireflies
            {
    n = atoi(&argv[i][2]);
            }
            else if(strncmp(argv[i], "-d", 2) == 0) // problem dimension
            {
    D = atoi(&argv[i][2]);
            }
            else if(strncmp(argv[i], "-g", 2) == 0) // number of generations
            {
    MaxGeneration = atoi(&argv[i][2]);
            }
            else if(strncmp(argv[i], "-a", 2) == 0) // alpha parameter
            {
    alpha = atof(&argv[i][2]);
            }
            else if(strncmp(argv[i], "-b", 2) == 0) // beta parameter
            {
    betamin = atof(&argv[i][2]);
            }
            else if(strncmp(argv[i], "-c", 2) == 0) // gamma parameter
            {
    gama = atof(&argv[i][2]);
            }
            else
            {
    cerr << "Fatal error: invalid parameter: " << argv[i] << endl;
    return -1;
            }
        }

        // firefly algorithm optimization loop
        // determine the starting point of random generator
srand(1);

// generating the initial locations of n fireflies
init_ffa();
#ifdef DUMP
dump_ffa(t);
#endif

while(t <= MaxGeneration)
{
// this line of reducing alpha is optional
alpha = alpha_new(alpha, MaxGeneration);

// evaluate new solutions
for(i=0;i<n;i++)
{
                        f[i] = function(ffa[i]);                        // obtain fitness of solution
I[i] = f[i]; // initialize attractiveness
}

// ranking fireflies by their light intensity
sort_ffa();
// replace old population
replace_ffa();

// find the current best
for(i=0;i<D;i++)
nbest[i] = ffa[0][i];
fbest = I[0];

// move all fireflies to the better locations
move_ffa();
#ifdef DUMP
dump_ffa(t);
#endif
t++;
}

cout << "End of optimization: fbest = " << fbest << endl;

return 0;
}

// FF test function
double cost(double* sol)
{
double sum = 0.0;

for(int i=0;i<D;i++)
sum += (sol[i]-1)*(sol[i]-1);

return sum;
}

double sphere(double* sol) {
int j;
double top = 0;
for (j = 0; j < D; j++) {
top = top + sol[j] * sol[j];
}
return top;
}

Thursday, 21 March 2013

rajnikanth amithabh bachchan common letters


#include<stdio.h>
#include<conio.h>
#define MAX 100
main()
{
      char a[]="AMITABH BACHCHAN";
      char b[]="RAJNIKANTH";
      char c[100];
   
     
      int i,j,k=0,flag=0;
     
      for(i=0;i<sizeof(a)-1;i++)
      {
             flag=0;
             for(j=0;j<k;j++)
             {
                         if(a[i]==c[j])
                                       flag=1;  
             }                
             if(flag==1)
                        continue;
             else
             {
                 for(j=0;j<sizeof(b)-1;j++)
                 {
                         if(a[i]==b[j])
                         {
                                       c[k]=a[i];
                                       k++;break;
                         }  
                 }                  
             }
      }
     
      printf("%s",c);
      getch();
         
}