next fit program in java

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("_______________");
}
}
/*

next fit program Output:
Enter the number of Holes:
4
Enter the number of Processes:
5
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
*/

Facing difficulties in understanding the program ?

Ask Your Queries in Comment Box

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *