/*

Ex.No:5.d
CPU SCHEDULING ALGORITHMS
SJF SCHEDULING
AIM:
 To write a C program for implementation of SJF scheduling algorithms.
ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer,totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign pid as I and get the value of p[i].btime.
Step 4: Assign p[0] wtime as zero and tot time as btime and inside the loop calculate wait time
and turnaround time.
Step 5: Calculate total wait time and total turnaround time by dividing by total number of
process.
Step 6: Print total wait time and total turnaround time.
Step 7: Stop the program.


*/

#include<stdio.h>

int main() {
	int bt[20], p[20], wt[20], tat[20], i, j, n, total = 0, pos, temp; 
	float avg_wt, avg_tat;
	printf("Enter number of process:");
	scanf("%d",&n);

	printf("\n Enter Burst Time:\n");
	for( i = 0; i < n; i++){
		printf("p%d:",i+1);
		scanf("%d",&bt[i]);
		p[i]=i+1;
	}

	for( i = 0; i < n; i++){ 
		pos = i;
		for( j = i+1; j < n; j++){
			if( bt[j] < bt[pos])
				pos = j;
		}

	temp = bt[i];
	bt[i] = bt[pos];
	bt[pos] = temp;

	temp = p[i];
	p[i] = p[pos];
	p[pos] = temp;
	}

	wt[0] = 0;

	for( i = 1; i < n; i++){ 
		wt[i] = 0;
		for( j = 0; j < i; j++)
			wt[i] += bt[j];

			total += wt[i];
	}

	avg_wt = (float)total / n;

	total = 0;

	printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
	for( i = 0; i < n; i++){ 
		tat[i] = bt[i] + wt[i];
		total += tat[i];
		printf("\n p%d\t\t\t %d\t\t\t %d\t\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
	}

	avg_tat = (float)total / n;
	printf("\n\nAverage Waiting Time=%f",avg_wt); printf("\nAverage TurnaroundTime=%f\n",avg_tat);
}
