#Printf does not display my output

5 messages · Page 1 of 1 (latest)

novel cosmos
#

Hello I kinda need guidance on why my code is not working, ive spent a couple of hours tracing for errors but nothing seems out of the ordinary. The topic is all about reading a text file and extracting those values to perform round robin scheduling algorithm.

#include <stdlib.h>

struct Process {
    int id;
    int arrTime;
    int burTime;
    int waitTime;
    int tat;
};

int main()
{
    struct Process p[64];
    int i, num_processes = 0, time_slice = 0, count = 0, wt = 0, tat = 0, sum = 0, j, y;
    float avg_wt, avg_tat;
    int temp[10];
    FILE *input_file = fopen("data.txt", "r");
    if (input_file == NULL)
    {
        printf("Error opening file\n");
        return 1;
    }

    fscanf(input_file, "%d", &num_processes);
    y = num_processes;
    fscanf(input_file, "%d", &time_slice);
    printf("%d\n%d\n", num_processes, time_slice);

    for(i= 0; i < num_processes; i++)
    {
        fscanf(input_file, "%d %d %d", &p[i].id, &p[i].arrTime, &p[i].burTime);
        temp[i] = p[i].burTime;
        printf("%d %d %d\n", p[i].id, p[i].arrTime, p[i].burTime);
    }

keen roverBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

novel cosmos
#
    for(sum = 0; i = 0; num_processes!=0)
    {
        if(temp[i] <= time_slice && temp[i] > 0)
        {
            sum = sum + temp[i];
            temp[i] = 0;
            count = 1;
        }

        else if (temp[i] > 0)
        {
            temp[i] = temp[i] - time_slice;
            sum = sum + time_slice;
        }

        if(temp[i] == 0 && count == 1)
        {
            y--;
            printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, p[i].burTime, sum - p[i].arrTime, sum - p[i].arrTime - p[i].burTime);
            wt = wt + sum - p[i].arrTime - p[i].burTime;
            tat = tat + sum - p[i].arrTime;
            count = 0;
        }

        if (i == num_processes - 1)
        {
            i = 0;
        }

        else if(p[i + 1].arrTime <= sum)
        {
            i++;
        }

        else
        {
            i = 0;
        }
    }

    avg_wt = wt * 1.0 / num_processes;
    avg_tat = tat * 1.0 / num_processes;
    printf("\n Average Turn Around Time: \t%f", avg_wt);
    printf("\n Average Waiting Time: \t%f", avg_tat);

    return 0;
}
``` here is the continuation btw
#

and the text file for reference

keen roverBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.