Compare Char matrix and vector

Asked

Viewed 33 times

-2

How do I make, in C, a code that compares whether the line responses of an array are equal to the line of an array? Follows the question:

imagem da pergunta

Follow the code I have:

#include <stdio.h>

int main()
{
    char ma[5][10] = {'a','b','c','d','d','c','b','a','b','b','d','a','d','c','b','a','b','b','d','a',
    'd','d','c','b','a','b','b','d','a','d','b','d','a','d','c','b','a','b','b','d','c','d','d','c',
    'b','a','b','b','d','a'}, letter;
    char gab[10] = {'a','b','c','d','a','b','c','d','b','b'};
    int resultado[10], r, l, soma = 0;
    
    //Corrigindo
    for(r = 0; r < 5; r++){
        for(l = 0; l < 10; l++){
            if(ma[r][l] == gab[l]){
                soma++;
            }
            resultado[r] =  soma;
        }
    }
    
    printf("Resultados\n\n");
    for(r = 0; r < 5; r++){
        printf("Aluno %d: %d\n", r+1, resultado[r]);
    }

    return 0;
}
  • This answers your question? Compare a char vector?

  • https://answall.com/questions/247225/como-comparar-vetores-em-c

  • https://answall.com/questions/390538/como-comparar-uma-posi%C3%A7%C3%A3o-da-string-com-um-character-c

  • I believe that in your section "Correcting" you must reset the accumulator soma for each line r otherwise it will not make sense what will store in resultado. for(r = 0; r < 5; r++){ soma = 0; for(l = 0; l < 10; l++){ if(ma[r][l] == gab[l]){ soma++; } resultado[r] = soma; } }

  • It didn’t work. After doing this he reset all the results. I tested tbm before the r FOR and after l. No results.

  • Here https://ideone.com/PDDeaK worked.

Show 1 more comment

2 answers

0

Note that for each letter of 'ma' corresponding to 'Gab' sum you receive 1. Thus, it makes no sense for the result of 4 and 5 to be 13. It follows excerpt of Cod showing matrix and vector, for comparison:

#include <stdio.h>

int main() { char ma[5][10] = {'a','b','c','d','d','c','b','a','b','b','d','a','d','c','b','a','b','b','d','a', 'd','d','c','b','a','b','b','d','a','d','b','d','a','d','c','b','a','b','b','d','c','d','d','c', 'b','a','b','b','d','a'}; char gab[10] = {'a','b','c','d','a','b','c','d','b','b'}; int result[10], r, l, sum = 0;

//imprimindo matriz
for(r = 0; r < 5; r++){
    for(l = 0; l < 10; l++){
        printf("%c  ",ma[r][l]);
    }
    printf("\n");
}
printf("\n\n");
//imprimindo gabarito
for(l = 0; l < 10; l++){
    printf("%c  ",gab[l]);
}
printf("\n\n");
for(r = 0; r < 5; r++){
    for(l = 0; l < 10; l++){
        if(ma[r][l] == gab[l]){
            soma++;
        }
        resultado[r] =  soma;
    }
}

printf("Resultados\n\n");
for(r = 0; r < 5; r++){
    printf("Aluno %d: %d\n", r+1, resultado[r]);
}

return 0;

}

  • That’s not an answer. If you want to add something to your question, just edit it and add whatever you think is necessary to clarify who will answer.

  • Then was to answer the guy from the previous answer. I did so because the code did not fit here.

0

Apparently your code already does what is requested, I ran this code and it worked perfectly.

#include <stdio.h>

int main() {
    char ma[5][10] = {'a','b','c','d','d','c','b','a','b','b','d','a','d','c','b','a','b','b','d','a', 'd','d','c','b','a','b','b','d','a','d','b','d','a','d','c','b','a','b','b','d','c','d','d','c', 'b','a','b','b','d','a'};
    char gab[10] = {'a','b','c','d','a','b','c','d','b','b'};
    int resultado[10], r, l, soma = 0;
    
    for(r = 0; r < 5; r++){
        for(l = 0; l < 10; l++){
            if(ma[r][l] == gab[l]){
                soma++;
            }
            resultado[r] =  soma;
        }
    }

    printf("Resultados\n\n");
    for(r = 0; r < 5; r++){
        printf("Aluno %d: %d\n", r+1, resultado[r]);
    }

    return 0;
}
  • I managed to resolve with the help of a member of another forum, thanks to all who made their time to help me.

Browser other questions tagged

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