Problems in reading and organizing a C matrix

Asked

Viewed 36 times

2

I have two files .txt, and each has a 2x8 matrix.

I need to read these files and compare them, row by row, and when the line of both is equal, add to a counter.

However, the problem I’m facing is this: after reading both matrices, as I said, I need to compare them, however, the algorithm compares all values, rather than comparing only line with line.

Follow the code I made:

#include <stdio.h>

#include<stdlib.h>

#define Lin 8

#define Col 2

int main(){

        int i, j, cont_pontos_regra_um=0, resultado[Lin][Col], partic1[Lin][Col], cont=0;
        FILE *arq;

        arq=fopen("resultado.txt", "r");//abertura e scaneiamento do resultado.txt
        for(i=0;i<Lin;i++){
            for(j=0;j<Col;j++){
                fscanf(arq,"%d ", &resultado[i][j]);
                printf("%d ", resultado[i][j]);
        }
        }

        fclose(arq); //fechar arquivo

        arq=fopen("partic1.txt", "r");//abertura e scaneiamento do resultado.txt
        for(i=0;i<Lin;i++){
            for(j=0;j<Col;j++){
                fscanf(arq,"%d ", &partic1[i][j]);      
        }
        }
        fclose(arq); //fechar arquivo

            for(i=0;i<Lin;i++){
                for(j=0;j<Col;j++){
                    if(partic1[i][j]==resultado[i][j]){
                        cont_pontos_regra_um++;
            }   
        }
        }

        //printf("%d", cont_pontos_regra_um);

        return 0;
}

1 answer

0

In this passage:

for(i=0;i<Lin;i++){
    for(j=0;j<Col;j++){
        if(partic1[i][j]==resultado[i][j]){
            cont_pontos_regra_um++;

Do so:

for(i=0;i<Lin;i++){
    int linhas_iguais = 1; // a principio sao iguais
    for(j=0;j<Col;j++){
        if(partic1[i][j]!=resultado[i][j]){
            linhas_iguais = 0; // sao diferentes!
            break;
        }
    }
    if(linhas_iguais)
        cont_pontos_regra_um++;
}

Browser other questions tagged

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