Problem with the final results

Asked

Viewed 35 times

0

Good evening, my code is giving a problem that I can not find, his idea is the following: I add the number of questions and the number of students in the class, and then add a feedback and the questions of the students, after that I check the feedback and give the grade of each student, but the results aren’t adding up. Follow the code below:

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

char** alocamatriz(int q, int a)
{
    int i;
    char** matriz = NULL;

    matriz = (char**) malloc(a * sizeof(char*));

    for(i = 0; i < q ; i++)
    {
        matriz[i] = (char*) malloc( q * sizeof(char));
    }

    return matriz;
}

void adicionavalores(char** matriz, int q, int a)
{
    int i, j;

    for(i = 0; i < a; i++)
    {
        for(j = 0; j < q ; j++)
        {
            scanf("%c", &matriz[i][j]);
        }
    }

}

void contagemdevalores(char** matriz, char* gabarito, int q, int a)
{
    int i, j, certo = 0;
    float media = 0;

    for(i = 0; i < a; i++)
    {
        for(j = 0; j < q; j++)
        {
            if(gabarito[j] == matriz[i][j])
                certo++;
        }

        media = 10 * (certo/q);
        printf("%.2f\n", media);

    }
}

char* adicionagabarito(int q)
{
    int i;
    char* gabarito = NULL;

    gabarito = (char*) malloc(q * sizeof(char));

    for(i = 0; i < q ; i++)
    {
        scanf("%c", &gabarito[i]);  
    }

    return gabarito;
}

int main (void)

{
    int alunos = 0,questoes = 0, i;
    char** matriz = NULL;
    char* gabarito = NULL;

    scanf("%d", &questoes);
    scanf("%d", &alunos);

    gabarito = adicionagabarito(questoes);

    matriz = alocamatriz(questoes, alunos);

    adicionavalores(matriz,questoes,alunos);

    contagemdevalores(matriz, gabarito, questoes, alunos);

    return 0;
}

1 answer

1

In alocamatriz() the cycle shall be a and not to q

char **alocamatriz(int q, int a)
{
    int i;
    char **matriz;
    matriz = malloc(a * sizeof *matriz);
    for(i = 0; i < a ; i++)
    //             ^ a elementos
    {
        matriz[i] = malloc(q * sizeof *matriz[i]);
    }
    return matriz;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.