Thursday, February 6, 2020

Manhattan Distance Heuristic

#include<stdio.h>
#include<stdlib.h>
struct node{
 int arr[10][10];
 int i,j;
 int md;

};
int a[10][10];
struct node* head = NULL;
struct node* q[200];
int check(int n);
void insert(int n);

int r=0,c=0,gotit;
void operations(int k,int m,int n);
void display(int n);
main()
{
 int n=3;
 insert(n);
 display(n);

}

void insert(int n)
{
 int i,j,i1,j1;

 struct node *temp;
 temp = (struct node*)malloc(sizeof(struct node));
 printf("Enter 8 puzzle matrix");
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   scanf("%d",&temp->arr[i][j]);
  }
 }
  printf("Enter 8 puzzle matrix goal state");
     for(i=0;i<n;i++)
      {
    for(j=0;j<n;j++)
       {

   scanf("%d",&a[i][j]);
       }
   }
  for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   if(temp->arr[i][j]==0)
   {
 
    temp->i = i;
    temp->j = j;
   }
  }
 }
 int count=0;
for(i1=0;i1<n;i1++)
{
for(j1=0;j1<n;j1++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(temp->arr[i1][j1]==a[i][j])
{
if((i1+j1)-(i+j)>0)
{
count=count+(i1+j1)-(i+j);
}
else
{
count=count+(i+j)-(i1+j1);
}
}
}
}
}
}
temp->md=count;
 int k,m;
 q[r] = temp;
 while(1)
 {


   k = q[r]->i;
  m = q[r]->j;

  if(++k<n && m<n && (k>=0 && m>=0))
  {
   operations(k,m,n);
  }

  k = q[r]->i;
   m = q[r]->j;

  if(--k<n && m<n&& (k>=0 && m>=0))
  {
       operations(k,m,n);
   }

  k = q[r]->i;
   m = q[r]->j;

  if(k<n && --m<n&& (k>=0 && m>=0))
  {
  operations(k,m,n);
  }

  k = q[r]->i;
  m = q[r]->j;

  if(k<n && ++m<n&& (k>=0 && m>=0))
  {
  operations(k,m,n);
  }

  r++;
  struct node* swap;
  // This for loop is sorting acc to h(n)
  for(i=r;i<=c;i++)
  {
   for(j=r;j<=c-1;j++)
   {
    if(q[j]->md>q[j+1]->md)
    {
     swap = q[j];
     q[j] = q[j+1];
     q[j+1] = swap;
    }
   }
  }

   c=r;
  if(check(n)==-4)
 {
  break;
 }


 }
}
//
void operations(int k,int m,int n)
{

 int o,p,count=0;
  struct node* t;
  t = (struct node*)malloc(sizeof(struct node));
 int i,j;
  for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   t->arr[i][j] = q[r]->arr[i][j];
 
  }
 }
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   if(t->arr[i][j]==0)
   {
    o = i;
    p=j;
   }
  }
 }
 int swap = t->arr[o][p];
 t->arr[o][p] = t->arr[k][m];
 t->arr[k][m] = swap;
  for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   if(t->arr[i][j]==0)
   {
     t->i = i;
     t->j = j;
   }
  }
 }
count =0;
int i1,j1;
for(i1=0;i1<n;i1++)
{
for(j1=0;j1<n;j1++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i1][j1]==a[i][j])
{
if((i1+j1)-(i+j)>0)
{
count=count+(i1+j1)-(i+j);
}
else
{
count=count+(i+j)-(i1+j1);
}
}
}
}
}
}
t->md=count;

 c++;
 q[c] = t;

}


int check(int n)

{

 int i,j,k,i1,i3,c1=0,c2,g2=0;

 int count=0;
  // here it is removing the node if duplicate matrix is present
  for(i=0;i<c;i++)

 {
  for(i3=i+1;i3<c;i3++)

  {
  count = 0 ;
for(j=0;j<n;j++)
    {
for(k=0;k<n;k++)

   {
       if(q[i]->arr[j][k]==q[i3]->arr[j][k])

    {

     count++;
     }
    }
 }
  if(count==9)

  {

       for(i1=i3;i1<c;i1++)

       {

        q[i1] = q[i1+1];

     

    }

    c--;

  count = 0;

 }



 count = 0;

 }



}

// here it is checking whether the goal state is finding or not

 c2=0;

for(i1=0;i1<c;i1++)

{
for(j=0;j<n;j++)

  {

   for(k=0;k<n;k++)

   {

 

    if(q[i1]->arr[j][k]==a[j][k])

    {

     c2++;

   

    }

   }

   }

  if(c2==9)

  {

   gotit = i1;

   g2 = -4;

   break;

  }

  else{

   c2=0;

  }

 }
return g2;
}
// Displaying path
void display(int n)
{
 int i,j,k;
 for(i=0;i<r;i++)
 {
  printf("\n");
  for(j=0;j<n;j++)
  {
   for(k=0;k<n;k++)
   {
    printf("%d\t",q[i]->arr[j][k]);
   }
   printf("\n");
  }

 }

}

Wednesday, February 5, 2020

misplaced tiles (heuristic) in c

#include<stdio.h>
#include<stdlib.h>
struct node{
int arr[10][10];
int i,j;
int h;

};
int a[10][10];
struct node* head = NULL;
struct node* q[200];
int check(int n);
void insert(int n);

int r=0,c=0,gotit;
void operations(int k,int m,int n);
void display(int n);
main()
{
int n=3;
insert(n);
display(n);

}

void insert(int n)
{
int i,j;

struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter 8 puzzle matrix");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&temp->arr[i][j]);
}
}
printf("Enter 8 puzzle matrix goal state");
     for(i=0;i<n;i++)
      {
  for(j=0;j<n;j++)
       {

scanf("%d",&a[i][j]);
       }
   }
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(temp->arr[i][j]==0)
{

temp->i = i;
temp->j = j;
}
}
}
int count=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(temp->arr[i][j]!=a[i][j] && a[i][j]!=0)
{
  count++;
}
}
}
temp->h = count;
int k,m;
q[r] = temp;
while(1)
{


k = q[r]->i;
m = q[r]->j;

if(++k<n && m<n && (k>=0 && m>=0))
{
operations(k,m,n);
}

k = q[r]->i;
m = q[r]->j;

if(--k<n && m<n&& (k>=0 && m>=0))
{
      operations(k,m,n);
  }

k = q[r]->i;
m = q[r]->j;

if(k<n && --m<n&& (k>=0 && m>=0))
{
operations(k,m,n);
}

k = q[r]->i;
m = q[r]->j;

if(k<n && ++m<n&& (k>=0 && m>=0))
{
operations(k,m,n);
}

r++;
struct node* swap;
// This for loop is sorting acc to h(n)
for(i=r;i<=c;i++)
{
for(j=r;j<=c-1;j++)
{
if(q[j]->h>q[j+1]->h)
{
swap = q[j];
q[j] = q[j+1];
q[j+1] = swap;
}
}
}

c=r;
if(check(n)==-4)
{
break;
}


}
}
//
void operations(int k,int m,int n)
{

int o,p,count=0;
struct node* t;
t = (struct node*)malloc(sizeof(struct node));
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
t->arr[i][j] = q[r]->arr[i][j];

}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i][j]==0)
{
o = i;
p=j;
}
}
}
int swap = t->arr[o][p];
t->arr[o][p] = t->arr[k][m];
t->arr[k][m] = swap;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i][j]==0)
{
  t->i = i;
  t->j = j;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i][j]!=a[i][j] && a[i][j]!=0)
{
count++;
}
}
}
t->h = count;
c++;
q[c] = t;

}


int check(int n)

{

int i,j,k,i1,i3,c1=0,c2,g2=0;

int count=0;
  // here it is removing the node if duplicate matrix is present
for(i=0;i<c;i++)

{
for(i3=i+1;i3<c;i3++)

{
count = 0 ;
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)

{
if(q[i]->arr[j][k]==q[i3]->arr[j][k])

{

count++;
}
  }
}
if(count==9)

{

      for(i1=i3;i1<c;i1++)

      {

       q[i1] = q[i1+1];

       

  }

  c--;

count = 0;

}



count = 0;

}



}

// here it is checking whether the goal state is finding or not

c2=0;

for(i1=0;i1<c;i1++)

{
for(j=0;j<n;j++)

{

for(k=0;k<n;k++)

{



if(q[i1]->arr[j][k]==a[j][k])

{

c2++;



}

}

}

if(c2==9)

{

gotit = i1;

g2 = -4;

break;

}

else{

c2=0;

}

}
return g2;
}
// Displaying path
void display(int n)
{
int i,j,k;
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
printf("%d\t",q[i]->arr[j][k]);
}
printf("\n");
}

}

}

Wednesday, January 29, 2020

shortest job first in c with gannt chart

#include<stdio.h>

void ganntchart(int p[],int arr2[],int n,int arr[]);

void displaytable(int arr[],int arr1[],int arr2[],int arr3[],int arr4[],int p[],int n);
main()
{

 int n,arr[15],arr1[15],arr2[15]={0},arr3[15],arr4[15],s=0,j1;
int p[15],k=0,sum=0;
float wt=0,tat=0;
int i,j,swap,swap1,swap2;
printf("Enter how many processers are there ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
  p[i] = i+1;
printf("Enter p%d arival time",i+1);
scanf("%d",&arr[i]);

printf("Enter p%d burst time",i+1);
scanf("%d",&arr1[i]);

}

for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;

swap1 = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = swap1;

swap2 = p[j];
p[j]= p[j+1];
p[j+1] = swap2;

}

}
}


for(i=0;i<n;i++)
{


 if( i==0)
 {
   s = arr1[i]+arr[i];
         arr2[i] = s;
   continue;
 }





  for(j1=i;j1<n;j1++)
  {
 
 
  for(j=i;j<n-1;j++)
  {
 
 
 
  if(arr1[j]>arr1[j+1] && arr[j+1]<=s)
  {
 

 swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;

swap1 = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = swap1;

swap2 = p[j];
p[j]= p[j+1];
p[j+1] = swap2;
 
 
  }
 
 }

}
 s = s+arr1[i];
 arr2[i] = s;


}

//tat and wt
for(i=0;i<n;i++)
{
arr3[i] = arr2[i] - arr[i];
arr4[i] =  arr3[i] - arr1[i];
}
for(i=0;i<n;i++)
{
tat = tat+arr3[i];
wt = wt+arr4[i];
}
tat = tat/n;
wt = wt/n;
displaytable(arr,arr1,arr2,arr3,arr4,p,n);

ganntchart(p,arr2,n,arr);

printf("\n\nAverage turn around time is : %f \nAverage waiting time is :%f",tat,wt);

}

//    Display table


void displaytable(int arr[],int arr1[],int arr2[],int arr3[],int arr4[],int p[],int n)
{
 int i;
printf("\n                         printing table");
printf("\n                         --------------\n\n");

printf("p  |  Arival time  |  Burst time  |  complete time  |    TAT  |   WT  |\n");

 for(i=0;i<n;i++)
    {
     printf("p%2d      %2d             %2d               %2d              %2d       %2d  \n",p[i],arr[i],arr1[i],arr2[i],arr3[i],arr4[i]);
 }


}



// Gannt chart
void ganntchart(int p[],int arr2[],int n,int arr[])
{
int i,j,k=1;
printf("\n                         Gannt chart");
printf("\n                         ----------- \n");
printf("\n");

for(i=0;i<n;i++)
{
 printf(" ");
 for(j=0;j<k;j++)
 {


  printf("--");

 }

 k++;

}
printf("\n");

for(i=0;i<n;i++)
{
 printf("|p%d",p[i]);
 k= i*2;
 for(j=0;j<k;j++)
 {
  printf(" ");
 
 }
}



printf("|\n");
k=1;

for(i=0;i<n;i++)
{
 printf(" ");
 for(j=0;j<k;j++)
 {


  printf("--");

 }

 k++;

}
printf("\n");
printf("%d",arr[0]);
k=1;
int m=0;
for(i=0;i<n;i++)
{
  for(j=0;j<k;j++)
 {
  printf("  ");
 }

 k++;
 if(arr2[i]>=10)
 {
  if(m==0)
  {
   printf("%d",arr2[i]);
 
  }
  else
  {
   printf("\b");
   printf("%d",arr2[i]);
  }
  m++;
 }
 else
 {
  printf("%d",arr2[i]);
 }

}

}

Tuesday, January 28, 2020

8puzzle using bfs in c program

#include<stdio.h>
#include<stdlib.h>
struct node{
int arr[10][10];
struct node* link;
int i,j;

};
int a[10][10];
struct node* q[200];


int r=0,c=0,gotit=0;
void operations(int k,int m,int n);
void display(int n);
void insert(int n);
int check(int n);
main()
{
int n=3;
insert(n);
display(n);

}

void insert(int n)
{
int i,j;

struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter 8 puzzle matrix intial state");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&temp->arr[i][j]);
}
}
printf("Enter 8 puzzle matrix goal state");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
temp->link=NULL;
// here finding the zero position and zero index is storing in that particular node
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(temp->arr[i][j]==0)
{

temp->i = i;
temp->j = j;
}
}
}
int k,m;
q[r] = temp;
while(1)
{
if( check(n)==-4)
{
break;
}


k = q[r]->i;
m = q[r]->j;

 //down
if(++k<n && m<n && (k>=0 && m>=0))
{
operations(k,m,n);
}

k = q[r]->i;
m = q[r]->j;
//up
if(--k<n && m<n&& (k>=0 && m>=0))
{
      operations(k,m,n);
  }

k = q[r]->i;
m = q[r]->j;
//left
if(k<n && --m<n&& (k>=0 && m>=0))
{
operations(k,m,n);
}

k = q[r]->i;
m = q[r]->j;
// right
if(k<n && ++m<n&& (k>=0 && m>=0))
{
operations(k,m,n);
}

r++;
if( check(n)==-4)
{
break;
}


}
}
// operations
void operations(int k,int m,int n)
{

int o,p;
struct node* t;
t = (struct node*)malloc(sizeof(struct node));
int i,j;
// copying the parent node into child node
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
t->arr[i][j] = q[r]->arr[i][j];

}
}
// finding the zero positin
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i][j]==0)
{
o = i;
p=j;
}
}
}
// swaping the zero based up on left,right,up,down operations
int swap = t->arr[o][p];
t->arr[o][p] = t->arr[k][m];
t->arr[k][m] = swap;
//finding the zero position of child node
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i][j]==0)
{                                                                                                                                                 
  t->i = i;
  t->j = j;
}
}
}
t->link = q[r];
c++;
q[c] = t;

}
int check(int n)
{
int i,j,k,i1,i3,c1=0,c2,g2=0;
int count=0;

  // here it is removing the node if duplicate matrix is present

for(i=0;i<c;i++)
{


for(i3=i+1;i3<c;i3++)
{
count = 0 ;
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{

if(q[i]->arr[j][k]==q[i3]->arr[j][k])
{
count++;

}
}

}
if(count==9)
{
      for(i1=i3;i1<c;i1++)
      {
       q[i1] = q[i1+1];
       
  }
  c--;
count = 0;
}

count = 0;
}

}
// here it is checking whether the goal state is finding or not
c2=0;
for(i1=0;i1<c;i1++)
{


for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{

if(q[i1]->arr[j][k]==a[j][k])
{
c2++;

}
}

}
if(c2==9)
{
gotit = i1;
g2 = -4;
break;
}
else{
c2=0;
}

}


return g2;

}
//Displaying
void display(int n)
{
printf("printing in reverse order\n");
struct node* t;

t=q[gotit];
int i,j;
while(t!=NULL)
{
for( i=0;i<n;i++)
{
for( j=0;j<n;j++)
{
printf("%d\t",t->arr[i][j]);
}
printf("\n");
}
printf("\n");
t = t->link;
}

}

Representation of 8puzzle

#include<stdio.h>
#include<stdlib.h>
struct node{
int arr[10][10];
int i,j;

};
struct node* head = NULL;
struct node* q[200];
void insert(int n);
void check(int n);
int r=0,c=0;
void operations(int k,int m,int n);
void display(int n);
main()
{
int n=3;
insert(n);
display(n);

}

void insert(int n)
{
int i,j;

struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter 8 puzzle matrix");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&temp->arr[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(temp->arr[i][j]==0)
{

temp->i = i;
temp->j = j;
}
}
}
int k,m;
q[r] = temp;
while(1)
{
check(n);

if(r==100) break; // breaking at random point because it's just a representation of puzzle
k = q[r]->i;
m = q[r]->j;

if(++k<n && m<n && (k>=0 && m>=0))
{
operations(k,m,n);
}

k = q[r]->i;
m = q[r]->j;

if(--k<n && m<n&& (k>=0 && m>=0))
{
      operations(k,m,n);
  }

k = q[r]->i;
m = q[r]->j;

if(k<n && --m<n&& (k>=0 && m>=0))
{
operations(k,m,n);
}

k = q[r]->i;
m = q[r]->j;

if(k<n && ++m<n&& (k>=0 && m>=0))
{
operations(k,m,n);
}

r++;


}
}
//
void operations(int k,int m,int n)
{

int o,p;
struct node* t;
t = (struct node*)malloc(sizeof(struct node));
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
t->arr[i][j] = q[r]->arr[i][j];

}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i][j]==0)
{
o = i;
p=j;
}
}
}
int swap = t->arr[o][p];
t->arr[o][p] = t->arr[k][m];
t->arr[k][m] = swap;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i][j]==0)
{
  t->i = i;
  t->j = j;
}
}
}

c++;
q[c] = t;

}
// checking whether the matrix are repeating are not
void check(int n)
{
int i,j,k,i1,i3;
int count=0;
for(i=0;i<c;i++)
{
for(i3=i+1;i3<c;i3++)
{

for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{

if(q[i]->arr[j][k]==q[i3]->arr[j][k])
{
count++;
}
}

}
if(count==9)
{
      for(i1=i3;i1<c;i1++)
      {
       q[i1] = q[i1+1];
       
  }
  c--;
count = 0;
}

count = 0;
}


}
}
void display(int n)
{
int i,j,k;
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
printf("%d\t",q[i]->arr[j][k]);
}
printf("\n");
}

}

}

Thursday, January 23, 2020

depth first search in c program

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* root = NULL;


void insert();
void dfs();
int main()
{

int size=0;
int choice;
while(1)
{

printf("\nEnter 1-> to insert the data\n");
printf("Enter 2->to display\n");
printf("Enter 3 - > to exit");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1 :  insert();
break;
case 2 : dfs();
break;
case 3 : exit(0);
break;
}

}
}

void insert()
{


struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter data");
scanf("%d",&temp->data);
temp->left = NULL;
temp->right = NULL;
if(root==NULL)
{
root = temp;
}
else
{
struct node* p = root;
struct node* q = NULL;
while(p!=NULL)
{
q = p;
if(temp->data>p->data)
{
p=p->right;
}
else
{
p = p->left;
}
}
if(temp->data>q->data)
{
q->right = temp;


}
else
{
q->left = temp;
}
}

}

void dfs()
{
struct node* store;
struct node* q[100];
    int c= 0;
int s[100];
int i,r=-1;
q[c] = root;
if(q[c]==NULL)
{
printf("Nothing to display");
}
else
   {
   
     while(1)
     {
   
      if(q[c]->left!=NULL && q[c]->right!=NULL)
      {
     
      s[++r] = q[c]->data;
      store = q[c];
     
      q[c] =store->right;
     
      q[++c]=store->left;
}
else if(q[c]->left!=NULL && q[c]->right==NULL)
      {
      s[++r] = q[c]->data;
      q[c] = q[c]->left;
     
     
}
else if(q[c]->left==NULL && q[c]->right!=NULL)
      {
      s[++r] = q[c]->data;
      q[c] = q[c]->right;
     
}
else if(q[c]->left==NULL && q[c]->right==NULL)
      {
      s[++r] = q[c]->data;
     
      c--;
      if(c==-1) break;
}


}

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


}

FCFS Scheduling with gannt chart and table in c program


#include<stdio.h>
void ganntchart(int p[],int arr2[],int n);

void displaytable(int arr[],int arr1[],int arr2[],int arr3[],int arr4[],int p[],int n);
int main()
{
/*
where arr for arival time
arr1 for burst time
arr2 for c.t
arr3 for tat
arr4 for waiting time
*/
int n,arr[15],arr1[15],arr2[15]={0},arr3[15],arr4[15],s=0;
int p[15];
float wt=0,tat=0;
int i,j,swap,swap1;
printf("Enter how many processers are there ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i+1;
printf("Enter p%d arival time",i+1);
scanf("%d",&arr[i]);

printf("Enter p%d burst time",i+1);
scanf("%d",&arr1[i]);

}


for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;

swap1 = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = swap1;

int swap2 = p[j];
p[j]= p[j+1];
p[j+1] = swap2;

}

}
}
//ct
for(i=0;i<n;i++)
{
s = s+arr1[i];
arr2[i] = s;
}
//tat and wt
for(i=0;i<n;i++)
{
arr3[i] = arr2[i] - arr[i];
arr4[i] =  arr3[i] - arr1[i];
}
for(i=0;i<n;i++)
{
tat = tat+arr3[i];
wt = wt+arr4[i];
}
tat = tat/n;
wt = wt/n;
displaytable(arr,arr1,arr2,arr3,arr4,p,n);

ganntchart(p,arr2,n);

printf("\n\nAverage turn around time is : %f \nAverage waiting time is :%f",tat,wt);

}

//    Display table


void displaytable(int arr[],int arr1[],int arr2[],int arr3[],int arr4[],int p[],int n)
{
int i;
printf("\n                         printing table");
printf("\n                         --------------\n\n");

printf("p  |  Arival time  |  Burst time  |  complete time  |    TAT  |   WT  |\n");
 
for(i=0;i<n;i++)
    {
     printf("p%2d      %2d             %2d               %2d              %2d       %2d  \n",p[i],arr[i],arr1[i],arr2[i],arr3[i],arr4[i]);
}


}



// Gannt chart
void ganntchart(int p[],int arr2[],int n)
{
int i,j,k=1;
printf("\n                         Gannt chart");
printf("\n                         ----------- \n");
printf("\n");

for(i=0;i<n;i++)
{
printf(" ");
for(j=0;j<k;j++)
{


printf("--");

}

k++;

}
printf("\n");

for(i=0;i<n;i++)
{
printf("|p%d",p[i]);
k= i*2;
for(j=0;j<k;j++)
{
printf(" ");

}
}



printf("|\n");
k=1;

for(i=0;i<n;i++)
{
printf(" ");
for(j=0;j<k;j++)
{


printf("--");

}

k++;

}
printf("\n");
printf("0");
k=1;
int m=0;
for(i=0;i<n;i++)
{
for(j=0;j<k;j++)
{
printf("  ");
}

k++;
if(arr2[i]>=10)
{
if(m==0)
{
printf("%d",arr2[i]);

}
else
{
printf("\b");
printf("%d",arr2[i]);
}
m++;
}
else
{
printf("%d",arr2[i]);
}

}

}


Tuesday, January 21, 2020

Breadth first search using linked list

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* left;
struct node* right;
};
struct node* root = NULL;
struct node* q[100];
int insert(int);
void bfs();
int main()
{

int size=0;
int choice;
while(1)
{

printf("Enter 1-> to insert the data\n");
printf("Enter 2->to display\n");
printf("Enter 3 - > to exit");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : size= insert(size);
break;
case 2 : bfs(size);
break;
case 3 : exit(0);
break;
}

}
}

int insert(int size)
{

size++;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter data");
scanf("%d",&temp->data);
temp->left = NULL;
temp->right = NULL;
if(root==NULL)
{
root = temp;
}
else
{
struct node* p = root;
struct node* q = NULL;
while(p!=NULL)
{
q = p;
if(temp->data>p->data)
{
p=p->right;
}
else
{
p = p->left;
}
}
if(temp->data>q->data)
{
q->right = temp;


}
else
{
q->left = temp;
}
}
return size;
}



void bfs(int size)
{
int c= 0,r=0;

q[c] = root;
if(q[c]==NULL)
{
printf("Nothing to display");
}
else
   {
    int i;
     while(q[c]!=NULL)
     {
      if(c==size-1)
      {
      break;
}
      if(q[c]->left!=NULL && q[c]->right!=NULL)
      {
      q[++r] = q[c]->left;
      q[++r] = q[c]->right;
      c++;
}
else if(q[c]->left!=NULL && q[c]->right==NULL)
      {
      q[++r] = q[c]->left;
     
      c++;
}
else if(q[c]->left==NULL && q[c]->right!=NULL)
      {
     
      q[++r] = q[c]->right;
      c++;
}
else if(q[c]->left==NULL && q[c]->right==NULL)
      {
     
      c++;
}

}
for(i=0;i<=r;i++)
{
printf("%d\t",q[i]->data);
}
 
   }
   



}


Saturday, November 9, 2019

storing student data using java

package pack1;

import java.util.Scanner;

class hh{
String Name;
int roll;
String branch;
String[] core = new String[10];  
String[] elective = new String[10];
int cor,ele;
public static void main(String args[])
{
    int n2=0;
    hh[] obj = new hh[20];
    while(1!=0) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter 1 -> to add data");
        System.out.println("Enter 2 - > to change his / her elective courses");
        System.out.println("Enter 3 -> to change his/her core subjects");
        System.out.println("Enter 4 -> to print particular");
        System.out.println("Enter 5 -> for particular branch");
        System.out.println("Enter 6-> to exit");
        int n1 = scan.nextInt();
        switch (n1)
        {
            case 1 :for (int i = 0+n2; i < n2+n1; i++) {
                String[] core = new String[5];
                String[] elective = new String[5];

                Scanner s2 = new Scanner(System.in);
                System.out.println("Enter name ,branch, roll no,cor and elective subjects");
                String Name = s2.nextLine();
                String branch = s2.nextLine();
                int roll = s2.nextInt();
                System.out.println("Enter how many core subjects ");
                int cor = s2.nextInt();
                System.out.println("ENter how many elective subjects have");
                int ele = s2.nextInt();
                if(cor+ele>6)
                {
                    System.out.println("!!!Something went wrong!!!\n\t enter again");
                    break;
                }
                System.out.println("ENter his "+cor+" Subjects");
                              for(int j=0;j<cor;j++)
                {
                    Scanner scan1 = new Scanner(System.in);
                    System.out.println("Enter " +(j+1)+" Core subject");
                    core[j] = scan1.nextLine();
                }
                for(int j=0;j<ele;j++)
                {
                    Scanner scan1 = new Scanner(System.in);
                    System.out.println("Enter " +(j+1)+" elective subject");
                    elective[j] = scan1.nextLine();
                }

                obj[i] = new hh(Name, roll,branch,core,elective,cor,ele);

                n2++;
                break;
            }
            break;
            case 2:
                electiv(obj,n2);
                break;
            case 3:co(obj,n2);
            break;
            case 4:method(obj,n2);
                   break;
            case 5: branch(obj,n2);
            break;
            case 6 :
                System.exit(0);
            default: System.out.println("Enter correct one");
        }

    }

}
 // change core subjects     static void co(hh[] obj, int n2) {
Scanner scan = new Scanner(System.in);
    System.out.println("Enter student name");
String name = scan.nextLine();
    for(int i=0;i<n2;i++)
    {
        if(name.equals(obj[i].Name))
        {
            Scanner s2 = new Scanner(System.in);
            System.out.println("Enter how many core subjects ");
            int cor = s2.nextInt();
            if(cor+obj[i].ele>6)
            {
                System.out.println("!!!Something went wrong!!!\n\t enter again");
                break;
            }
            System.out.println("ENter his "+cor+" Subjects");
            for(int j=0;j<cor;j++)
            {
                Scanner scan1 = new Scanner(System.in);
                System.out.println("Enter " +(j+1)+" Core subject");
                obj[i].core[j] = scan1.nextLine();
            }
        }
}

}
// To change elective courses    static void electiv(hh[] obj, int n2) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter student name");
        String name = scan.nextLine();
        for(int i=0;i<n2;i++)
        {
            if(name.equals(obj[i].Name))
            {
                Scanner s2 = new Scanner(System.in);
                System.out.println("Enter how many elective subjects ");
                int ele = s2.nextInt();
                if(ele+obj[i].cor>6)
                {
                    System.out.println("!!!Something went wrong!!!\n\t enter again");
                    break;
                }
                System.out.println("ENter his "+ele+" Subjects");
                for(int j=0;j<ele;j++)
                {
                    Scanner scan1 = new Scanner(System.in);
                    System.out.println("Enter " +(j+1)+" elective subject");
                    obj[i].elective[j] = scan1.nextLine();
                }
            }
        }

    }
//branch wise    static void branch(hh[] obj, int n2) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a branch name ");
    String br = scan.nextLine();
         for(int i=0;i<n2;i++) {
             if(br.equals(obj[i].branch)) {
                 System.out.println("Name:" + obj[i].Name + "\nRoll:" + obj[i].roll+"\nBranch:"+obj[i].branch);
                 System.out.println("core subjects:");
                 for(int j=0;j<obj[i].cor;j++)
                 {
                     System.out.println("\t "+obj[i].core[j]);
                 }
                 System.out.println("elective subjects:");
                 for(int j=0;j<obj[i].ele;j++)
                 {
                     System.out.println("\t "+obj[i].elective[j]);
                 }
             }
             else             {
                 continue;
             }
         }
    }
//costructor    hh(String Name, int roll, String branch,String[] core,String[] elective,int cor,int ele)
{
    this.Name=Name;
    this.roll=roll;
    this.branch=branch;
    this.cor=cor;
    this.ele=ele;
    for (int i=0;i<cor;i++)
    {
        this.core[i]=core[i];
    }
    for (int i=0;i<cor;i++)
    {
        this.elective[i]=elective[i];
    }
}

//Pring name wisestatic void method(hh[] obj,int n2)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter name to search");
    String name = sc.nextLine();
    for(int i=0;i<n2;i++) {
          if(name.equals(obj[i].Name)) {
              System.out.println("Name:" + obj[i].Name + "\nRoll:" + obj[i].roll+"\nBranch:"+obj[i].branch);
              System.out.println("core subjects:");
              for(int j=0;j<obj[i].cor;j++)
              {
                  System.out.println("\t "+obj[i].core[j]);
              }
              System.out.println("elective subjects:");
              for(int j=0;j<obj[i].ele;j++)
              {
                  System.out.println("\t "+obj[i].elective[j]);
              }
          }
          else          {
              continue;

Sunday, January 13, 2019

Write a menu driven program that depicts the working of a library.


Write a menu driven program that depicts the working of a library. The menu options should be:
(a) Add book Information
(b) Display book information
(c) List all books of given author
(d) List the title of specified book
(e) List the count of books in the library
(f) List the books in the order of accession number
(g) Exit

Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not.
PROGRAM



#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void abk();
void dbk();
void aabk();
void tbk();
void nbk();
void lbk();
void ebk();



struct library{
int accession,cost,n;
char tittle[20],author[20],name[20],flag[5];
}b[10];
int i=0;

int main()
{
char choice,g;
while(i>=0)
{

printf("\nif you want to Add book information , Then enter A or a ");
printf("\nif you want to display book information ,then enter B or b");
printf("\nif you want to know Author books,then enter C or c");
printf("\nif you want to list the tittle of the specified book,then enter D or d");
printf("\nif you want to know number of the books,then enter E or e");
printf("\nif you want to List books in the order of accession number ,then enter F or f");
printf("\nIf you want to exit,then enter G or g");
printf("\nIf you want to clear the screen,Then enter H or h");

printf("\nEnter your choice:");
scanf("%c",&choice);

fflush(stdin);
    
  
    switch(choice)
{
case 'a':  abk(); break;
case 'b': dbk(); break; 
case 'c': aabk();break;
case 'd': tbk(); break;
case 'e': nbk(); break;
case 'f': lbk(); break;
case 'A':  abk(); break;
case 'B': dbk(); break; 
case 'C': aabk();break;
case 'D': tbk(); break;
case 'E': nbk(); break;
case 'F': lbk(); break;
   case 'g' :exit(5); break;
      case 'G' :exit(5); break;
  case 'h' :system("cls"); break;   // clear output screen
  case 'H' :system("cls"); break; 
  default :printf("\nThere is no selection for '%c'",choice);
  
     
}
}
}

//ADDING INFORMATON

void abk()
{
printf("\nEnter accession number:");
scanf("%d",&b[i].accession);
fflush(stdin);
//(fflsh(stdin):If entered ouput is a null pointer, the fflush function will flush all entered specific output with unwritten data in the buffer)
printf("Enter Book name:");
gets(b[i].tittle);
fflush(stdin);
printf("Enter author name:");
gets(b[i].author);
fflush(stdin);
printf("Enter book price:");
scanf("%d",&b[i].cost);
fflush(stdin);

printf("BOOK ISSUMENT\t(yes or no):");
gets(b[i].flag);
fflush(stdin);
i++;
}


//DISPLAYING INFORMATION
void dbk()
{
int j;
char str[3]={'y','e','s'};
for(j=0;j<i;j++)
{
printf("\naccession number is %d\n",b[j].accession);
puts(b[j].tittle);
puts(b[j].author);
printf("book price is :%d",b[j].cost);

  if(strcmp(b[j].flag,str)==0)
  {
  printf("\n issued");
  }
  else
  {
  printf("\n not issued");
  }

}
}



//AUTHOR BOOK INFORMATION

void aabk()
{
int j;
char a2[20];
printf("\nEnter author name :");
gets(a2);
fflush(stdin);
for(j=0;j<i;j++)
{
if(strcmp(b[j].author,a2)==0)
{
printf("\naccession number is %d\n",b[j].accession);

puts(b[j].tittle);

puts(b[j].author);
printf("book price is :%d",b[j].cost);
 printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
 }
 else if(strcmp(b[j].author,a2)!=0)
 {
  printf("\nThere is no belongs to  %s books",a2);
 }


}
if(i==0)  printf("\nThere is no belongs to  %s books",a2);
}
//SPECIFIED BOOK

void tbk()
{char book[20];
int j;;
printf("\nEnter a book name");
gets(book);
fflush(stdin);
for(j=0;j<i;j++)
{
if(strcmp(b[j].tittle,book)==0)
{
printf("\naccession number is %d\n",b[j].accession);

puts(b[j].tittle);

puts(b[j].author);
printf("book price is :%d",b[j].cost);
}
else if(strcmp(b[j].tittle,book)!=0)
{
printf("\nThere is no %s  book",book);
}
printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
if(i==0) printf("There is no %s book",book);
}
//NUMBER OF BOOKS IS THERE



void nbk()
{
int j,k=0;
for(j=0;j<i;j++)
{
k++;
}
if(k>1)
printf("\n%d books",k);
else if(k==1)
printf("\n%d book",k);
else
printf("\nThere are no books");
printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}

//ASCENDING ORDER OF ACCESSION NUMBER

void lbk()
{
int j,k,temp;
for(j=0;j<i;j++)
{
for(k=0;k<i-1;k++)
{
if(b[k].accession>b[k+1].accession)
{
temp=b[k].accession;
b[k].accession=b[k+1].accession;
b[k+1].accession=temp;
}
}
   }
for(j=0;j<i;j++)
{
printf("\naccession number is %d\n",b[j].accession);

puts(b[j].tittle);

puts(b[j].author);
printf("book price is :%d",b[j].cost);
   
printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
EXPECTED OUTPUT



Manhattan Distance Heuristic

#include<stdio.h> #include<stdlib.h> struct node{  int arr[10][10];  int i,j;  int md; }; int a[10][10]; struct node*...