Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Sunday, April 6, 2014

Java code to search an element using Linear Search or Binary Search

/*
Program to search for a number in an array entered by the user using
either Linear Search or Binary Search.
*/
import java.io.*;
class searchArray
{
int a[];
int n;
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
public searchArray(int nn) // Constructor
{
a = new int[nn];
n = nn;
}
public static void main(String args[]) throws IOException
{
System.out.print("
Enter the size of the array : ");
int nn = Integer.parseInt(br.readLine());
searchArray call = new searchArray(nn);
System.out.println("
Enter " +nn +" elements :");
call.readArray();
// Ask for the search technique
System.out.println("Choose Search Technique :
");
System.out.println("1 : Linear Search");
System.out.println("2 : Binary Search (the array should be
sorted in ascending order)");
System.out.print("
Your Choice : ");
int choice = Integer.parseInt(br.readLine());
int v;
switch(choice)
{
case 1:
System.out.print("
Enter the number to be searched : ");
v = Integer.parseInt(br.readLine());
call.linearSearch(v);
break;
case 2:
System.out.print("
Enter the number to be searched : ");
v = Integer.parseInt(br.readLine());
call.binarySearch(v);
break;
default :
System.out.println("
Invalid Choice !");
break;
}
}
public void readArray() throws IOException
{
for(int i=0;i<n;i++)
a[i] = Integer.parseInt(br.readLine());
}
public void linearSearch(int v)
{
int f=-1;
for(int i=0;i<n;i++)
{
if(a[i]==v)
{
f=i;
break;
}
}
if(f==-1)
System.out.println("
" +v +" NOT found !");
else
System.out.println("
" +v +" is in location " +f);
}
public void binarySearch(int v)
{
int f=-1;
int l=0,m=0,u=n-1;
while(l<=u && f==-1)
{
m = (l+u)/2;
if(a[m]==v)
f = m;
else
if(a[m]>v)
u = m-1;
else
l = m+1;
}
if(f==-1)
7System.out.println("
" +v +" NOT found !");
else
System.out.println("
" +v +" is in location " +m);
}
}
/**
* Algorithm for Linear Search :
* ---------------------------
* 1. Start
* 2. Construct a single-dimensional array of appropriate size.
* 3. Fill the array with data entries.
* 4. Ask the user for the number to be searched
* 5. Use linear search technique.
* 6. Print a message whether the number is present in the array or
not.
* 7. End
*
* Algorithm for Binary Search :
* ---------------------------
* 1. Start
* 2. Construct a single-dimensional array of appropriate size.
* 3. Fill the array with data entries in ascending order.
* 4. Ask the user for the number to be searched.
* 5. Use binary search technique.
* 6. Print a message whether the number is present in the array or
not.
* 7. End
*/
/*
Enter the size of the array : 5
Enter 5 elements :
46
324
846
54
541
Choose Search Technique :
1 : Linear Search
2 : Binary Search (the array should be sorted in ascending order)
Your Choice : 1
Enter the number to be searched : 54
54 is in location 3
*/


Read More..

Wednesday, April 2, 2014

C code for Round Robin Scheduling

Round Robin: 
A straightforward way to reduce the penalty that short jobs suffer with FCFS is to use preemption based on a clock. The simplest such policy is round robin. A clock interrupt is generated at periodic intervals. When the interrupt occurs, the currently running process is placed in the ready queue, and the next ready job is selected on a FCFS basis. This technique is also known as time slicing, because each process is given a slice of time before being preempted.

/* C program to implement Round Robin Scheduling */

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

int initialise();
void sort();
void print();
void GC();
void WT(int,int,char);
int RR();
float averagewaitingtime();
float averageturnaroundtime();

int p[15],a[15],b[15],br[15],gc[30],n,time,k;
static int wt[15];
void main()
  {
    int k;
    clrscr();
    printf("Enter the time slice:");
     fflush(stdin);
    scanf("%d",&time);
     n=initialise();
     sort();
     k=RR();
     print();
     printf("

Average Wating time = %.2fmsec. & Average Turnaround time= %.2fmsec.

",averagewaitingtime()/n,averageturnaroundtime()/n);
     GC(k);
     getch();
  }

int RR()
  {
   int i,flag,left,k=0;
    do
      {
    flag=1;
    for(i=0;i<n;i++)
     {
       if ( br[i]==0  || (a[i] > (k*time)))
         continue;
       if(br[i] <= time)
        {
          left=br[i];
          gc[k++]=p[i];
          br[i]=0;
          WT(i,left,l);
        }
       else
        {
          gc[k++]=p[i];
          br[i]-=time;
          WT(i,time,g);
        }
     }
      for(i=0;i<n;i++)
    if(br[i]!=0)
      flag=0;
      }while(!flag);
   return k;
  }


 void WT(int i,int add,char ch)
  {
    int j;
    if ( ch == l )
     {
       for(j=0;j<n;j++)
       if( j!=i && br[j]!=0 )
          wt[j]+=add;
     }
    if ( ch == g )
     {
       for(j=0;j<n;j++)
    if ( j!=i && br[j]!=0 )
     wt[j]+=add;
     }
  }



 void print()
    {
      int i;
       printf("

    | Process | Arrival time | Burst_Time | Waiting_Time | T_arnd_Time |
");
       printf("    -------------------------------------------------------------------
");
    for(i=0;i<n;i++)
     printf("    |%4d     |%8d      |%6d      |%8d      |%6d       |

",p[i],a[i],b[i],wt[i],b[i]+wt[i]);
       printf("    --------------------------------------------------------------------
");
    }

  void GC(int g)
   {
     int i;
     printf("


AND THE G.C IS :::

");
      for(i=0;i<g;i++)
       printf("------");
      printf("
");
       for(i=0;i<g;i++)
          {
    printf("|  %d  ",gc[i]);
     if(i==g-1)
      printf("|");
    }
     printf("
");
     for(i=0;i<g;i++)
      printf("------");
      printf("
");
   }

void sort()
 {
   int i,n,t,j;
   for(i=0;i<n;i++)
    {
    for(j=i+1;j<n;j++)
     {
      if(a[i] > a[j])
       {
     t=p[i];
     p[i]=p[j];
     p[j]=t;

     t=a[i];
     a[i]=a[j];
     a[j]=t;

     t=b[i];
     b[i]=b[j];
     b[j]=t;

     t=br[i];
     br[i]=br[j];
     br[j]=t;
       }
    }
  }
}

 int initialise()
  {
    FILE *fp;
    int i=0,n1,n2,n3;
    fp=fopen("sjf.txt","r");
    while(fscanf(fp,"%d%d%d",&n1,&n2,&n3)!=EOF)
      {
     p[i]=n1;
     a[i]=n2;
     br[i]=b[i++]=n3;
      }
    fclose(fp);
    return i;
  }
 float averagewaitingtime()
  {
    float sum=0;
    int i;
     for(i=0;i<n;i++)
    sum+=wt[i];
      return sum;
  }

  float averageturnaroundtime()
  {
    float sum=0;
    int i;
     for(i=0;i<n;i++)
    sum+=(wt[i]+b[i]);
      return sum;
  }


Input File:

1 0 3
2 1 3
3 2 2

//Output Of the above program:-

C code for Round Robin Scheduling

C code for Round Robin Scheduling


Read More..