OK call it a pet peeve if you want to but your program design was really bothering me. Hopefully it will help you to learn how to pass variables in your functions, and localization. So you can use your functions to do the work for you to make the program run smoother/faster/more efficient. pick one or all the above.
Code:
#include <stdio.h>
#include <stdlib.h>
struct processStruct
{
int arrivalTime;
int burstTime;
int waitingTime;
int turnaroundTime;
};
void fcfs(struct processStruct processes[], int n)
{
int avwt=0,avtat=0,i,j,pos,max,temp,sum=0;
processes[0].waitingTime = 0;
// Calculating Waiting and Response Time
for(i = 1; i < n; i++)
{
sum = sum + processes[i - 1].burstTime;
processes[i].waitingTime = sum - processes[i].arrivalTime;
if(processes[i].waitingTime < 0)
processes[i].waitingTime = 0;
}
printf("\nProcess\t\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");
// Calculating Turn Around Time and Printing the Table
// for(i = 0; i < 10; i++)
for(i = 0; i < n; i++)
{
processes[i].turnaroundTime = processes[i].burstTime + processes[i].waitingTime;
avwt += processes[i].waitingTime;
avtat += processes[i].turnaroundTime;
printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d",i, processes[i].arrivalTime,processes[i].burstTime,processes[i].waitingTime,processes[i].turnaroundTime);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d\n\n",avtat);
}
void rr()
{
}
int file(struct processStruct processes[])
{
int i = 0;
int x = 0;
FILE *fp;
fp = fopen("zante_test_file", "r");
if(fp == NULL)
{
perror("Error opening the file\n");
exit(EXIT_FAILURE);
}
fscanf(fp, "%d", &x);
while(fscanf(fp, "%d\t%d", &processes[i].arrivalTime, &processes[i].burstTime) != EOF)
{
printf("Process ID: [%d] \t Arrival Time: %d \t Burst Time: %d\n", i, processes[i].arrivalTime, processes[i].burstTime);
i++;
}
fclose(fp);
return i;
}
int main()
{
struct processStruct processes[10];
int input;
printf("Please Select which scheduling Algorithm you would like to use\n");
printf("1. First Come First Serve\n");
printf("2. Round Robin\n");
printf("3. Exit\n");
scanf("%d", &input);
//after selection open file and read it
int amount_of_data = file(processes);
switch(input)
{
case 1: // call to file() can be added here also if you want to eliminate the use of one int.
// or if you only want to open that file on selection # 1
// fcfs(processes,file(processes));
fcfs(processes,amount_of_data );
break;
case 2:
//function
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Please choose a number 1 to 3\n");
break;
}
getchar();
return 0;
}
also if your functions are written out above main you do not have to add the prototypes above main.