Why, within the function, the program is not comparing correctly?

1

The program receives the number of questions, the number of students, creates a vector of type char for the template and compares with the result of the other students, in an array, but within the function of contagemvalores , apparently the comparison is not being done correctly. What could be the problem?

The output should be when I use the input:

5 3
ABCDE
AAABB
CBEDB
ABDDE

should give:

2.00
4.00
8.00

And he's giving:

0.00
2.00
2.00

And when I put the variable hit that should be 1, 2, and 4, it appears as 0, 1, and 1.

The following is the code below:

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

char** alocamatriz(int q, int a) //Declaracao da funcao de alocacao da matriz(notas dos alunos)
{
    int i;
    char** matriz = NULL;

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

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

    return matriz;
}

void adicionavalores(char** matriz, int q, int a) //Declaracao da funcao que adiciona valores(questoes)
{
    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) //Funcao que verifica igualdade com as questoes do aluno e com o gabarito
{
    int i, j, certo = 0;
    float media = 0;

    for(i = 0; i < a; i++)
    {
        certo = 0;

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

        media =(certo/q) * 10.0; //Media dos alunos
        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); //Chamada de funcao

    matriz = alocamatriz(questoes, alunos); //Chamada de funcao

    adicionavalores(matriz,questoes,alunos); //Chamada de funcao

    contagemdevalores(matriz, gabarito, questoes, alunos); //Chamada de funcao

    free(gabarito);
    free(matriz);

    return 0;
}
    
asked by anonymous 07.06.2015 / 03:03

1 answer

1
media =(certo/q) * 10;

certo and q are integers, so division is done in integers: 1/3 == 0 .

(Yes, I know that you have declared media as float, but the compiler does not care - it calculates first the right side, and then the left side. of whole arithmetic.)

There are several ways to fix this:

media = 10.0f * certo / q
media = (certo / (float) q) * 10
media = ((float) certo / q) * 10

(I personally prefer to declare certo as float, so you do not have to put the constants in float or insert conversion in the middle of the code)

Notice that

media = certo / q * 10.0f

does not work - multiplication and division associate from left to right, so this is equivalent to

media = (certo / q) * 10.0f

And when the program arrives at 10 times it's too late.

Your program also has another problem. When you read scanf("%c", …) , you read the next character in the entry. The formats %s , %d and %f discard blank characters until they find the next word or number, but %c not - if the next character is ' ' or '\n' , scanf reads blank space. You can verify that this is the case by making the following change:

 gabarito = adicionagabarito(questoes);
 printf("# %d\n", (int) gabarito[0]);  // imprime 10

This is easy to solve: it's just you always remember to use " %c" instead of "%c" .

    
07.06.2015 / 03:20