Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to get this program to run correctly (write a file called output.txt

ID: 3697317 • Letter: I

Question

I'm trying to get this program to run correctly (write a file called output.txt, etc.) with the appropriate input.txt as follows...

The input.txt file will be formatted like below (without the huge spacing in between the rows), and RR stand for Round Robin (algorithm), 4 is for the amount of processes, and 3 is time quantum, after that the columns (seperated by spaces), are for - Process number           Arrival Time           CPU burst time            Priority - respectively.

input.txt

RR 4

3

1 0 24 1

2 0 3 1

3 0 3 1

The output.txt should look something like this (again without the huge spacing in the rows), RR is same as before, and the numbers are the wait time followed by the process number. Finally displaying the average wait time. This is about as thourough as I can get, so if you are confused, don't bother answering.

output.txt

RR

0 1

4 2

7 3

10 1

14 1

18 1

22 1

26 1

AVG Waiting Time: 5.67

Here is the code:

#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum,index = 0;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
char type[5];
char str[50];
char *ptr;
FILE *fp = fopen("input.txt", "r");
fgets(str, 100, fp); // reading line
ptr = strtok(str, " "); // splitting by space
int i=0;
while(ptr != NULL)
{
if(index == 0){
//The first token in the ptr will be assigned to the first position in the character array, type.
type[0] = *ptr; // error: incompatible types when assigning to type 'char[5]' from type 'char *'
index++;
}
else if(index == 1){
//Here n is an integer, and ptr is a pointer. Especially, its a character pointer. So,
//If you want to assign the next token to n, then, you should convert that string value to
//which ptr is pointing to int, and then assign.
n = atoi(ptr); // warning: assignment makes integer from pointer without a cast
remain = n;
index++;
}
else{
//strtol is expecting a character pointer as the first parameter. ptr is a pointer, but ptr[someIndex is not].
at[i] = (int) strtol(ptr + 1, (char **)NULL, 10);
bt[i] = (int) strtol(ptr +2, (char **)NULL, 10);
rt[i] = bt[i];
i++;
}
ptr = strtok(NULL, " "); // and keep splitting
}
fclose(fp);

char c[1000];
FILE *fptr;
fptr=fopen("output.txt","w");
fprintf(fptr,"%s"," Process |Turnaround Time|Waiting Time ");

for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
fprintf(fptr,"P[%d] | %d | %d ",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
fprintf(fptr," Average Waiting Time= %f ",wait_time*1.0/n);
fprintf(fptr,"Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}

Explanation / Answer

Hey heres the code, I tried hard to simplify and correct your code, you will get the exact output as you asked, if you get any doubts just post comment.

#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum,index = 0;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10],i;
char type[5];
char str[50];
char *ptr;
FILE *fp = fopen("input.txt", "r");
fgets(str, 100, fp); // reading line
for(int k=0;str[k]!='';k++)
{
if(isdigit(str[k]))
{
  time_quantum=int(str[k])-48;
  
}
} // splitting by space

while(fscanf(fp,"%d",&i)!=EOF)
{
  if(index==0)
  {
n=i; /* Assuming n as number of processes*/
  
index=1;
    }
  
     
     j=0;
    while(j<=n)
    {
      fscanf(fp,"%d",&i);
         j=i;
      
    /*Assuming j as process numbers*/
      fscanf(fp,"%d",&i);
      at[j]=i;
     
      fscanf(fp,"%d",&i);
      bt[j]=i;
   
      rt[j]=bt[j];
      fscanf(fp,"%d",&i); /*This last one for saving priority numbers*/
      j++;
  }

break;
   
}

fclose(fp);

char c[1000];
FILE *fptr;
fptr=fopen("output1.txt","w");
fprintf(fptr,"%s ","RR");

remain=n;
if(at[1]==0)
fprintf(fptr,"%d 1 ",at[1]);
for(time=0,count=1;remain!=0;)
{
    if(rt[count]<=time_quantum && rt[count]>0)
    {
      time+=rt[count];
      fprintf(fptr,"%d %d ",time,count);
      rt[count]=0;
    
     
      flag=1;
    }
    else if(rt[count]>0)
    {
      rt[count]-=time_quantum;
     
      time+=time_quantum;
      fprintf(fptr,"%d %d ",time,count);
    }
    if(rt[count]==0 && flag==1)
    {
      remain--;
     
      wait_time+=time-at[count]-bt[count];
      
      flag=0;
    }
    if(count==n)
      count=1;
    else if(at[count+1]<=time)
      count++;
    else
      count=1;
}
fprintf(fptr," Average Waiting Time= %f",wait_time*1.0/n);
  

return 0;
}