Search code examples
cmatrixsimulationdynamic-memory-allocation

Read matrix element and respective row and column indexes from text file from text file in C


I am trying to do a predator-prey simulation in C, but I am quite new at programming in C. I can't seem to fill the board as expected.

I need to read the input parameters for the simulation from a text file given as standard input to the program. The input file has the following data:

2 4 3 6 5 5 9

ROCK 0 0

RABBIT 0 2

FOX 0 4

FOX 1 0

FOX 1 4

ROCK 2 4

RABBIT 3 0

RABBIT 4 0

FOX 4 4

Initially, I read the first line, which contains a set of parameters needed for the simulation. I can read these just fine and use the values afterwards.

Then, I need to read the following 9 lines with are the objects and the respective x and y positions in the matrix that represents my board. I was supposed to get something like this as the initial state:

enter image description here

where the * represents the rocks, R the rabbits and F the foxes. Here is what I did until now:

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

// Declare functions
void *fillMatrix(int nobj, int r, int c);
void printMatrix(char *_matrix, int r, int col);

int main(int argc, char *argv[]) {
    
    int GEN_PROC_RABBITS, // number of generations until a rabbit can procreate
    GEN_PROC_FOXES, //  number of generations until a fox can procreate
    GEN_FOOD_FOXES, // number of generations for a fox to die of starvation
    N_GEN, // total number of generations for the simulation
    ROWS,
    COLS,
    N; // inicial number of objects in the ecosystem

if (!scanf("%d %d %d %d %d %d %d", &GEN_PROC_RABBITS, &GEN_PROC_FOXES, &GEN_FOOD_FOXES, &N_GEN, &ROWS, &COLS, &N)) {
    fprintf(stderr, "Error while reading input.\nAborting...\n");
    exit(-1);
    }

char *matrix;
    matrix=fillMatrix(N,ROWS,COLS);
    printMatrix(matrix, ROWS, COLS);

void *fillMatrix(int nobj, int r, int c){
    char (*matrix)[c] = (char (*)[c]) malloc(r * c * sizeof(char));
    char *character[6];
    int m;
    for (m=0; m < nobj; m++) {
        scanf("%s %d %d", &character, &r, &c);
        printf("%s %d %d\n", character, r, c);
        matrix[r][c] = character[0];   
    }
    return (char *) matrix;
}

void printMatrix(char *_matrix, int r, int c){
    char (*matrix)[c] = (char (*)[c]) _matrix;
    int i, j;
    printf("Gen 0\n");
    printf ("--------\n");
    for(i=0; i < r; i++) {
        for (j=0; j < c-1; j++) {
            printf("%c ", matrix[i][j]);
        }
        printf("%c\n", matrix[i][j]);
    }
    printf ("--------\n");
    fflush(stdout);
}

What I get is something like this:

enter image description here

Also, I still need to replace R from ROCKS by "*" and when I tried to do this, everything got even more messed up. It seems there is something wrong with memory allocation or maybe I am missing something else.

Can somebody help about what I am doing wrong here? Thanks a lot. Also, any suggestions of better ways to do this are very welcome.


Solution

  • You could try declaring your matrix like this instead :

    void *fillMatrix(int nobj, int r, int c)
    {
        char (*matrix)[r][c] = malloc(sizeof(*matrix));
    
        // Fill the whole matrix with empty space characters
        // When declared this way, 'matrix' must be dereferenced each time it's used
        memset(*matrix, ' ', sizeof(*matrix));
    
        char *character[6];
        int m;
        for (m=0; m < nobj; m++) {
            scanf("%s %d %d", &character, &r, &c);
            printf("%s %d %d\n", character, r, c);
            (*matrix)[r][c] = character[0]; //<== (*matrix)   
        }
        return (char *) matrix;
    }
    
    void printMatrix(char *_matrix, int r, int c)
    {
        // Idem here
        char (*matrix)[r][c] = (char (*)[r][c]) _matrix;
        int i, j;
        printf("Gen 0\n");
        printf ("--------\n");
        for(i=0; i < r; i++) {
            for (j=0; j < c-1; j++) {
                printf("%c ", (*matrix)[i][j]); //<== (*matrix)
            }
            printf("%c\n", (*matrix)[i][j]); //<== (*matrix)
        }
        printf ("--------\n");
        fflush(stdout);
    }
            printf ("--------\n");
            fflush(stdout);
        }