Next Fit > Java Program

Computer Organization and Architecture

Next Fit > Java Program


import java.io.*; 
class NFit 

public static void main(String args[]) throws IOException 
{         

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         
int nh,np;         
System.out.println("Enter the number of Holes:");         
nh=Integer.parseInt(br.readLine());         
System.out.println("Enter the number of Processes:");         
np=Integer.parseInt(br.readLine());         
int P[]=new int[np];         
int H[]=new int[nh];         
System.out.println("Enter the size of holes:");         
for(int i=0;i<nh;i++)         
{                 
System.out.print("H["+i+"]=");                 
H[i]=Integer.parseInt(br.readLine());                 
System.out.println();         
}         
System.out.println("Enter the size of Processes:");         
for(int i=0;i<np;i++)         
{
System.out.print("P["+i+"]=");                 
P[i]=Integer.parseInt(br.readLine());                 
System.out.println();         
}
int h=nh;         
int j=0; 
while(h>0)

for(int i=0;i<nh;i++)         
{
if(H[i]>P[j])         
{
System.out.println("Process "+j+"allocated in hole "+i);         
H[i]-=P[j];         
j++;         
h--;         
}          
}
}
 System.out.println("_______________"); 
}

/* Output: 
Enter the number of Holes: 

Enter the number of Processes: 

Enter the size of holes: 
H[0]=20 
H[1]=12 
H[2]=10 
H[3]=30 
Enter the size of Processes: 
P[0]=10 
P[1]=24 
P[2]=8 
P[3]=5 
P[4]=12 
Process 0 allocated in hole 0 
Process 1 allocated in hole 3 
Process 2 allocated in hole 0 
Process 3 allocated in hole 1  */

Comments

Popular posts from this blog

Intermediate Code Generation > C Program