Home > Software design >  Not printing grades as a 2d array
Not printing grades as a 2d array

Time:09-20

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void getGrades(FILE *ifp, int assigns, int stus, int grades[assigns][stus]);
void printGrades(int assigns, int stus, int grades[assigns][stus]);

int main(int argc, char *argv[])
{

    int assigns = 0;
    int stus = 0;
    int grades[assigns][stus];

    // command line arg. error checking
    if (argc != 2)
    {
        printf("Syntax Error: ./<exec> <file>\n");
        exit(1);
    }
    // file handle = open("infile.txt", read)
    FILE *ifp = fopen(argv[1], "r");
    if (ifp == NULL){
        printf("Could not open %s for reading!\n", argv[1]);
        exit(1);
    }
    fscanf(ifp, "%d%d", &assigns, &stus); // it does scan in 8 and 5 correctly
    // print debugging check
    // printf("assigns == %d\nstus == %d", assigns, stus);

    getGrades(ifp, assigns, stus, grades);
    printGrades(assigns, stus, grades);
    return 0;
}

void getGrades(FILE *ifp, int assigns, int stus, int grades[assigns][stus])
{
    int i = 0, j = 0;
    // iterate through all of the rows & columns
    for (i = 0; i < assigns;   i){
        for (j = 0; i < stus;   j){
           fscanf(ifp, "%d", &grades[i][j]);
        }
    }
}

void printGrades(int assigns, int stus, int grades[assigns][stus])
{
    int i = 0, j = 0;
    for (i = 0; i < assigns;   i){
        for (j = 0; j < stus;   j){
            printf("d", grades[i][j]);
        }
        printf("\n");
    }
}

input file looks like this:

8 5 100 92 84 76 68 99 91 83 75 67 98 90 82 74 66 97 89 81 73 65 96 88 80 72 64 95 87 79 71 63 94 86 78 70 62 93 85 77 69 0

and I simply want the output to look like this

   100        92        84        76        68
    99        91        83        75        67
    98        90        82        74        66
    97        89        81        73        65
    96        88        80        72        64
    95        87        79        71        63
    94        86        78        70        62
    93        85        77        69         0

I don't know where I'm making an error in the code for my program to not print that output.

The size is dynamic since the rows and columns can change in the input file. Right now the row in the input file is 8 and the column is 5.

CodePudding user response:

The problem with your code is:

    int assigns = 0;
    int stus = 0;
    int grades[assigns][stus];

What is the size of this array? Later you change the values of assigns and stus but the array has already been created by then.

You should only create the array AFTER you have values for assigns and stus. If you move it down to after the fscanf that reads those values it works: https://onlinegdb.com/UIWe4ZZh5

CodePudding user response:

Here are the issues with your code:

  1. typo in getGrades() cause the function to not terminate. i < stus should be j < stus.
  2. undefined behavior to allocate int grades[assigns][stus] with assigns == 0 and stus == 0. It triggers a segfault once you fix the above for me. Move the definition till after assigns and stus have been read it. Also, VLAs blow up badly if too large so consider using malloc() instead where you can at least catch a failed memory allocation.
  3. fscanf() may fail in which case your variables are not set.
  4. fclose() your file handle.
  5. (minor) reduce scope of variables
  6. (minor) It's a bit odd that you use both return and exit from main. I changed it to return.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define NAME_LENGTH 10

void getGrades(FILE *ifp, int assigns, int stus, int grades[assigns][stus]) {
    // iterate through all of the rows & columns
    for (int i = 0; i < assigns;   i){
        for (int j = 0; j < stus;   j){
            if(fscanf(ifp, "%d", &grades[i][j]) != 1) {
                printf("fscanf failed\n");
                return;
            }
        }
    }
}

void printGrades(int assigns, int stus, int grades[assigns][stus]) {
    for (int i = 0; i < assigns;   i){
        for (int j = 0; j < stus;   j){
            printf("d", grades[i][j]);
        }
        printf("\n");
    }
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Syntax Error: ./<exec> <file>\n");
        return 1;
    }
    FILE *ifp = fopen(argv[1], "r");
    if (ifp == NULL) {
        printf("Could not open %s for reading!\n", argv[1]);
        return 1;
    }
    int assigns = 0;
    int stus = 0;
    if(fscanf(ifp, "%d%d", &assigns, &stus) != 2) {
        printf("fscanf of assigns and stus failed\n");
        fclose(ifp);
        return 1;
    }
    int grades[assigns][stus];
    getGrades(ifp, assigns, stus, grades);
    fclose(ifp);
    printGrades(assigns, stus, grades);
    return 0;
}

and it now outputs:

       100        92        84        76        68
        99        91        83        75        67
        98        90        82        74        66
        97        89        81        73        65
        96        88        80        72        64
        95        87        79        71        63
        94        86        78        70        62
        93        85        77        69         0
  •  Tags:  
  • c
  • Related