Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not printing grades as a 2d array

#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("%10d", 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading