Monday, 9 September 2013

SJF-Preemetive---Only gantt chart

Following code produces Gantt chart for the SJF-preemtive schedulign.....works for 4 process.
In a Array  process one is represented by 0 and so on

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct process
{
       int at;
       int bt;
       int rt;
       int  c;
       int wt;
       int trt;
       };
      
      
       int min(int *a,int j)
       {int min;
        int i=0;
        int flag=0;
        int pos;
        for(i=0;i<=j;i++)
        {
        if(a[i]==0)
        continue;
        min=a[i];
        pos=i;
        flag=1;
        break;
        }
        if(flag==0)
        return -1;
        else
        for(i=0;i<=j;i++)
        {
        if(a[i]==0)
        continue;
        if(a[i]<min)
        {
        min=a[i];
        pos=i;
        }
       }
       return pos;
       }
void display(struct process *p)
{  
    int i=1;
    while(1)
    {
        printf("Arrival time%d  ",p[i].at);
        printf("Burst Time%d  ",p[i].bt);
        printf("Response Time%d  ",p[i].rt);
        printf("comlete Time%d  ",p[i].c);
        printf("waiting Time%d  ",(p[i].rt-p[i].at));
        printf("\n\n");
        i++;
        if(i==5)
        break;
    }
}    
main()
{
      struct process p[100];
      int b[100];
      int a[4];//since number of process taken are four
      puts("Enetr the process arival time and burst time");
      int i=0;
      int j=0;
      while(1)
      {
      i++;
      scanf("%d",&(p[i].at));
      scanf("%d",&(p[i].bt));
      a[j]=p[i].bt;
      j++;
      if(i==4)
      break;
      }
      int x=0;
      int y=0;
       j=0;
      while(x!=-1)
      {
        a[x]=a[x]-1;
        b[y]=x;
        y++;
        if(j!=3)
        j++;
        x=min(a,j);
     
      }
      printf("\n\n");
      for(i=0;i<y;i++)
      printf("%d\n",b[i]);
      getch();
      }

Sunday, 8 September 2013

FCFS ALGORITHM

First we tried to calculate the response time and complete time
for the first process response time is equal to arrival time and complete time is equal to the sum of response time and burst time.
For  next process 
                            if arrival time is equal to the complete time of the previous process then there will be  no idle time and response time is equal to the complete time of the previous process
                           if arrival time is greater than the complete time of the previous process then there will be the idle time and the response time will equal to the arival time of the porcess
                           if arrival time is less than the complete time of the previous process then there will again no idle time and response time is equal to to the complete time of the previous time

For all the above case 
complete time =response+burst time
waiting time=response time-arrival time


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

struct process
{
       int at; //arival time
       int bt; //burst time
       int rt; //response time
       int  c; //completed time
       int wt; //waiting time
       int trt;
       };  //structure to hold all the attributes associated with process

void display(struct process *p)//Function to display all the attributes of the process
{  
    int i=1;
    while(1)
    {
        printf("Arrival time%d  ",p[i].at);
        printf("Burst Time%d  ",p[i].bt);
        printf("Response Time%d  ",p[i].rt);
        printf("comlete Time%d  ",p[i].c);
        printf("waiting Time%d  ",(p[i].rt-p[i].at));
        printf("\n\n");
        i++;
        if(i==4)
        break;
    }
}    
void fcfs(struct process *p)//first position of the array is ignored and counting has started from index value one
{
     if((p[1].at)>0)
      printf("Idle   0-%d\n",p[1].at);
      p[1].rt=p[1].at;
      p[1].wt=0;
      p[1].c=(p[1].at+p[1].bt);
      int i=2;
      while(1)
      {
     
             if(p[i].at==p[i-1].c)
             p[i].rt=p[i-1].c;
            
             if((p[i].at)>(p[i-1].c))
             {
             printf("Idle   %d-%d\n",p[i-1].c,p[i].at);
             p[i].rt=p[i].at;
             p[i].c=p[i].rt+p[i].bt;
              }
             
              if(p[i].at<p[i-1].c)
              {
              p[i].rt=p[i-1].c;
              p[i].c=p[i].rt+p[i].bt;
              }
             
              i++;
              if(i==4)
              break;
        }
 }
main()
{
      struct process p[100];
      puts("Enetr the process arival time and burst time");
      int i=0;
      while(1)
      {
      i++;
      scanf("%d",&(p[i].at));
      scanf("%d",&(p[i].bt));
      if(i==3)
      break;
      }
      fcfs(p);
      display(p);
      }

Wednesday, 4 September 2013

Cookie handling through Javascript

Following Code will work only in mozilla...for chrome browser need to be handled explicitly

<html>
<head>

</head>

<body>
<button type "button"  onclick="z()">A</button>
<button type "button"  onclick="x()">B</button>
<p id="m">A 0</p>
<p id="g">B 0</p>

<script>
var b=document.cookie
document.write(b)
if(b=="")
{
var i=0
var j=0;
}
else
{
var a=document.cookie
var i=parseInt(a.charAt(0))
var j=parseInt(a.charAt(2))
}
function z()
{
i=i+1;
document.getElementById("m").innerHTML="A: "+i
document.cookie=i+" "+(j+0)
}
function x()
{
j=j+1;
document.getElementById("g").innerHTML="B: "+j
document.cookie=i+" "+(j+0)
}
</script>

</body>
</html>

Friday, 9 August 2013

Its just an example of multiplying any number by 2^n-1 or 2^n+1 without using any multiplication symbol.


//multiplication of the number by 2^n+1 2^n-1
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>//will not work in linux
main()
{
      int n=3;
      int m=n<<3;
      printf("%d\n",(m-n));//multiplying 3 by 7
      printf("%d",(m+n));//multiplying 3 by 9
      getch();
      }

Saturday, 3 August 2013

Find highest length substring such that there are equal number of 0’s and 1’sin array of 1’s and 0’s only

I saw this question on some site stating it was asked in some campus placement....so here is the solution..

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

  main()
{
    char ch[100];
    int l;
    int num0=0;
    int num1=0;
    int even=0;

    puts("Enetr the string\n");
    gets(ch);//need not to put & since ch is itself a pointer
    l=strlen(ch);
    int t=l;
    if(l%2==0)
    even=1;
    printf("%d\n",l);
    for(int i=0;i<l;i++)
    {
            if(ch[i]=='0')
            num0++;
            else
            num1++;
            }
            if((num0==num1))
            {
                                 puts("Length of max substring having equal number of zeros and one\n");
                                 printf("%d",l);
                                 }
                                 else{
                                      if(even==0)
                                      t=t+1;
                                      int i=0;
                                      num0=0;
                                      num1=0;
                                      int j=0;
                                 while(t!=0)
                                 {          int w=0;
                                            t=t-2;
                                            j=t;
                                            while(j!=(l+1))
                                            {
                                            for( i=w;i<j;i++)
                                            {
                                                 if(ch[i]=='0')
                                                 num0++;
                                                 else
                                                 num1++;
                                                 
                                                    }
                                                    if(num0==num1)
                                                    {
                                                                  printf("Maximum substring having equal number of 0's and 1's is %d between %d",w,j-1);
                                                                  getch();
                                                                  }
                                                    else{
                                                         num0=0;
                                                         num1=0;          
                                                    w=w+1;
                                                    j=j+1;
                                                    }
                                                    }
                                         
                                            }
                                            }
         
    getch();
 }

Thursday, 1 August 2013

Traversing link-list in Reverse Order(Recursion)

Here we traversing the singly-link list in reverse order without saving the contents in another strucutre


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


struct link
{
    int data;
  struct link *next;
 
    };
   
    void add(struct link **p,int d)
    {struct link *r,*q;
    q=*p;
    if(*p==NULL)
    {
    r=(struct link *)malloc(sizeof(struct link));
    r->data=d;
    r->next=NULL;
    *p=r;
   
    }
    else
    {
    while(q->next!=NULL)
    {q=q->next;
  }
     r=(struct link *)malloc(sizeof(struct link));
      r->data=d;
    r->next=NULL;
    q->next=r;
   
  }
   
  }
  void display(struct link **p)
  {
  struct link *q=*p;
  while(q!=NULL)
  {
  printf("%d\n",q->data);
q=q->next;
 }
  }
  void reversetraverse(struct link *p)//using recursion we traversing 
  {
struct link *q=p;
if(q->next==NULL)
{
     printf("%d\n",q->data);
return;
}
reversetraverse(q->next);
printf("%d\n",q->data);
 
 
}
 
   
   
    main()
    {
  struct link *p;
  p=NULL;
 
  add(&p,5);
add(&p,5);
 add(&p,6);
display(&p);
reversetraverse(p);
  getch();
  }

Recursion-Reversing a String(JAVA)

public class Reverse
{
String s;
static String r="";
 
 public Reverse()
{
}
public void reverse(String s,int i)//earlier we tried using only string as an argument in that case i would be either static or class variable and in both case for all the stack elements value if i would be 5 hence output will "yyyyy"
{

if(i==5)
{
r=r+s.charAt(i);
return;

}
reverse(s,i+1);
r=r+s.charAt(i);//everything that comes after the function call is call in the reverse method we have used that concept
}
public static void main (String args[])
{
Reverse obj=new Reverse();
obj.reverse("Tanmay",0);
System.out.println(obj.r);
}
}