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

Asked

Viewed 74 times

1

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

The exit should be when I use the entrance:

5 3
ABCDE
AAABB
CBEDB
ABDDE

should give:

2.00
4.00
8.00

And this giving:

0.00
2.00
2.00

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

Follow 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;
}
  • When you paste a code block into a question, you can select the block and type Ctrl-K to format it as code.

1 answer

1


media =(certo/q) * 10;

certo and q are whole, so division is made into the whole: 1/3 == 0.

(Yes, I know you stated media like float, but the compiler doesn’t care - he calculates first the right side, and then the left. On the right side, it only has integer arithmetic operations.)

There are several ways to fix this:

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

(the one I prefer, personally, is to declare certo like float, then you don’t have to put the constants in float or put conversion in the middle of the code)

Note 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 by the time the program gets multiplied by 10 it’s too late.


Your program also has another problem. When you read scanf("%c", …), you read the next character of the input. The formats %s, %d and %f discard blank characters until they find the next word or number, but %c no - if the next character is ' ' or '\n', the scanf reads the white 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: just remember to always use " %c" in place of "%c".

  • Perfect, this I managed to solve, but you could help me in why the comparison between the values are not beating??

  • Edited question, well, I gave the place space before but didn’t solve, I should just do it myself??

  • Yes, it really worked, the only problem is that instead of 2.0, 4.0, 8.0, it’s giving 2.0, 2.0, 4.0

  • Are you sure you are running your program with the right input file? Your code works perfectly here.

  • This updated, but I tested the program here and it ran perfectly, finished the problems, thank you very much

  • Edit your question back as it was (no spaces before the "%c" and without the (float)), for the next people who find this issue do not get lost looking for the problem of your code.

Show 1 more comment

Browser other questions tagged

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