FEOF while loop to a for loop in c

Viewed 23

I need some help to rewrite the feof while loop to a for loop in c.

I don't understand how to make the while loop to a for loop where it is commented feeding the matrix

I have two variables n and m. Variable n is the size of the array / 1023 in rows and m is columns that are 1023 in size.

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

int main()
{
    int n, m;
    int **A;

    FILE *inputFile;
    inputFile = fopen("Input.dat", "r");
    if (inputFile == NULL){
        printf("An error occured while reading the file");
        exit(1);
    }

    fscanf(inputFile, "%d%d", &n, &m);

    A = (int **)malloc(n * sizeof(int *));
    if (A == NULL){
        printf("An error occured while allocating");
        exit(1);
    }
    // allocating memory to the matrix.
    for (int i = 0; i < n; i++){
        A[i] = (int *)malloc(m * sizeof(int));
    }

    // feeding the matrix.

    for (int i = 0; i < n; i++){
        if (feof(inputFile)){
            break;
        }
        for (int y = 0; y < m; y++){
            if (!feof(inputFile)){
                fscanf(inputFile, "%d", &A[i][y]);
            }
            else{
                break;
                }
        }
    }

    // print out the 2D array
    int Max = A[0][0];
    for (int i = 0; i < n; i++){
        printf("\n");
        for (int j = 0; j < m; j++){
            printf("%d\t", A[i][j]);
            if (Max < A[i][j]){
                Max = A[i][j];
            }
        }
    }
    printf("\nThe biggest number in the matrix is: %d", Max);

    // freeing memory and closing file
    free(A);
    fclose(inputFile);
    return 0;
}
0 Answers
Related