Home > other >  Two dimensional Array no output on C
Two dimensional Array no output on C

Time:12-07

So, I've tried modifying a code from GFG about a SJF algorithm. On the website, the code needs to read input for the user. Since I don't really need it, I removed the input part and replaced it with a two-dimensional array with elements already inside it. I tried to run it, but it doesn't output anything.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int A[5][5] = {
        {8, 3, 7, 5, 2},
        {0, 0, 0, 0, 0}
    }; 
    
    int i, j, n, total = 0, index, temp;
    float avg_wt, avg_tat;

    for (i = 0; i < n; i  ) {
        index = i;
        for (j = i   1; j < n; j  )
            if (A[j][1] < A[index][1])
                index = j;
        temp = A[i][1];
        A[i][1] = A[index][1];
        A[index][1] = temp;
 
        temp = A[i][0];
        A[i][0] = A[index][0];
        A[index][0] = temp;
    }
    A[0][2] = 0;
    for (i = 1; i < n; i  ) {
        A[i][2] = 0;
        for (j = 0; j < i; j  )
            A[i][2]  = A[j][1];
        total  = A[i][2];
    }
    avg_wt = (float)total / n;
    total = 0;
    printf("P     BT     WT     TAT\n");
    for (i = 0; i < n; i  ) {
        A[i][3] = A[i][1]   A[i][2];
        total  = A[i][3];
        printf("P%d     %d     %d      %d\n", A[i][0],
               A[i][1], A[i][2], A[i][3]);
    }
    avg_tat = (float)total / n;
    printf("Average Waiting Time= %f", avg_wt);
    printf("\nAverage Turnaround Time= %f", avg_tat);
}

CodePudding user response:

The main issue:

  1. You declare A[5][5] but only initialize the first two rows. I assume you only want 2 rows so I eliminated the first 5 below to let the compiler derive the right size.
  2. Initialize n.

Additional advise

  1. Minimize the scope or your variables.
  2. Create a swap() function.
  3. Print newline in the end.
  4. Don't reuse variables (i, j, total) for different thngs but instead prefer separate variables. It's ok to use the same name when their scope don't overlap. I renamed the 2nd use for total to total2 instead. This in turn allows you to eliminate two variables that are only used once.
  5. Don't declare a bunch of variables on one line. It's harder to read, easier to miss an initialization, and when you use version control you usually see the whole line being changed when it may just have been one variable.
  6. Prefer an unsigned variable like size_t for indexes that are non-negative. It communicates intend better, and if you ever need it, the maximum positive value is about twice as big as the signed int.
#include <stdio.h>
#include <stdlib.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int A[][5] = {
        {8, 3, 7, 5, 2},
        {0, 0, 0, 0, 0}
    };
    int total = 0;
    size_t n = sizeof A / sizeof *A;
    for (size_t i = 0; i < n; i  ) {
        int index = i;
        for (size_t j = i   1; j < n; j  )
            if (A[j][1] < A[index][1])
                index = j;
        swap(&A[i][0], &A[index][0]);
        swap(&A[i][1], &A[index][1]);
    }
    A[0][2] = 0;
    for (size_t i = 1; i < n; i  ) {
        A[i][2] = 0;
        for (size_t j = 0; j < i; j  )
            A[i][2]  = A[j][1];
        total  = A[i][2];
    }
    int total2 = 0;
    printf("P     BT     WT     TAT\n");
    for (int i = 0; i < n; i  ) {
        A[i][3] = A[i][1]   A[i][2];
        total2  = A[i][3];
        printf("P%d     %d     %d      %d\n", A[i][0],
            A[i][1], A[i][2], A[i][3]);
    }
    printf(
        "Average Waiting Time= %f\n"
        "Average Turnaround Time= %f\n",
        (float) total / n,
        (float) total2 / n);
}

and example output:

P     BT     WT     TAT
P0     0     0      0
P8     3     0      3
Average Waiting Time= 0.000000
Average Turnaround Time= 1.500000
  • Related