/* Simulate the following cpu scheduling algorithms. '
B. Short Job First(sjf) based on arrival time(non-preemptive). */
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y)
{
int *temp;
*temp=*x;
*x=*y;
*y=*temp;
}
void main()
{
int i,j,bt[10],bt1[10],n,twt=0,temp1,z[20],p[10];
int wt[10],sum=0,sum1=0,at[10],k,l;
float avgt=0.00;
clrscr();
printf("\n Enter no. of Processes ::");
scanf("%d",&n);
printf("\n Enter the %d burst times::",n);
for(i=0;i<n;i++)
{
printf("\n\n Enter burst and arrival times for process%d::",i+1);
scanf("%d%d",&bt[i],&at[i]);
p[i]=i+1;
}
i=j=0;
while(at[j+1]<bt[i])
j++;
for(k=0;k<j;k++)
bt1[k]=bt[k+1];
/*for(i=0;i<j;i++)
printf("%6d",bt1[i]);*/
for(i=1;i<j-1;i++)
{
for(k=i+1;k<j;k++)
{
if(bt[i]>bt[k])
swap(&bt[i],&bt[k]);
}
}
/* for(i=0;i<j;i++)
printf("\n%4d",bt1[i]); */
for(i=0;i<n;i++)
{
if(i==0)
{
wt[i]=0;
sum=sum+bt[i];
sum1=sum1+bt[i];
}
else
{
sum=sum+bt[i];
wt[i]=sum-at[i];
}
sum1=sum1+bt[i];
z[i]=sum1;
}
/*for(i=0;i<n;i++)
printf("%d",p[i]); */
printf("\n\n----------------------------------------------\n");
printf("\n\tPNo\tbt\tat\twt\n");
printf("\n----------------------------------------------\n");
for(i=0;i<n;i++)
{
twt+=wt[i];
printf("\n\n\tP%d\t%d\t%d\t%d\n",p[i],bt[i],at[i],wt[i]);
}
printf("\n----------------------------------------------\n");
avgt=(float)twt/(float)n;
printf("\n\nGannt Chart ::\n\n");
printf("\t0");
for(i=1;i<n;i++)
printf("%4d",z[i]);
printf("\n\n Total waiting time is ::%d",twt);
printf("\n\n Average waiting time is ::%f",avgt);
getch();
}
/* Input and Output :-
Enter no. of Processes ::3
Enter the 3 burst times::
Enter time for process1::24
Enter time for process2::3
Enter time for process3::3
231
----------------------------------------------
PNo bt wt
----------------------------------------------
P2 3 0
P3 3 3
P1 24 6
----------------------------------------------
Gannt Chart ::
0 3 6
Total waiting time is ::9
Average waiting time is ::3.000000 */
Sunday, November 25, 2012
ListIterator program in Collection
import java.util.*;
class TestListIterator
{
public static void main(String[] args)
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
ListIterator it=al.listIterator();
while(it.hasNext())
{
System.out.println(it.next());
}
while(it.hasPrevious())
{
System.out.println(it.previous());
}
}
}
class TestListIterator
{
public static void main(String[] args)
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
ListIterator it=al.listIterator();
while(it.hasNext())
{
System.out.println(it.next());
}
while(it.hasPrevious())
{
System.out.println(it.previous());
}
}
}
HashSet Example program in Collections
import java.util.*;
class TestHashSet
{
public static void main(String[] args)
{
HashSet<String> hs=new HashSet<String>();
hs.add("Ram");
hs.clear();
hs.add("sree");
hs.add("sri");
hs.add("Rama");
hs.remove("Ram");
//System.out.println(hs.contains("sita"));
System.out.println(hs.isEmpty());
System.out.println(hs.size());
System.out.println
(hs);
}
}
class TestHashSet
{
public static void main(String[] args)
{
HashSet<String> hs=new HashSet<String>();
hs.add("Ram");
hs.clear();
hs.add("sree");
hs.add("sri");
hs.add("Rama");
hs.remove("Ram");
//System.out.println(hs.contains("sita"));
System.out.println(hs.isEmpty());
System.out.println(hs.size());
System.out.println
(hs);
}
}
Demo on ForEach loop in collections
import java.util.*;
class TestForEach
{
public static void main(String[] args)
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
Iterator it=new Iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class TestForEach
{
public static void main(String[] args)
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
Iterator it=new Iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Saturday, November 24, 2012
Demo Program on Enumeration
import java.util.*;
class TestEnumeration
{
public static void main(String[] args)
{
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
Enumeration e=Collections.enumeration(al);
while(e.hasMoreElements())
{
System.out.println(e.nextElement());
}
/*while(it.hasPrevious())
{
System.out.println(it.previous());
}*/
}
}
/*
boolean hasMoreElements()
element nextElement():
*/
Subscribe to:
Posts (Atom)