Error in a matrix character counter

Asked

Viewed 68 times

-2

I need to do a counter of occurrences of a matrix, by Visual Studio, and I’m doing well, but when I try to run the program, an error occurs and I do not know how to solve.

The error that occurs is as follows: Exception generated in 0x00981C28 in Matrix.exe: 0xC0000005: access violation while reading local 0x040FD8F8.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    char matriz[3][3], c;
    int linhas, colunas;

    int matrizAux[100], ascii[255], r;
   
    printf("Digite a quantidade de linhas da matriz: "); 
    scanf_s("%d", &linhas);
    while ((c = getchar()) != '\n' && c != EOF) {}

    printf("Digite a quantidade de colunas da matriz: "); 
    scanf_s("%d", &colunas);
    while ((c = getchar()) != '\n' && c != EOF) {}

    printf("\n\n");

    printf("Digite um valor para os elementos da matriz\n\n");

    for (int i = 0; i < linhas; i++)
    {
        for (int j = 0; j < colunas; j++)
        {
            printf("Elemento[%d][%d] = ", i, j);
            scanf_s("%c", &matriz[i][j]);
            while ((c = getchar()) != '\n' && c != EOF) {}
        }
    }

    printf("\n\n");

    for (int i = 0; i < linhas; i++)
    {
        for (int j = 0; j < colunas; j++)
        {
            printf("%c\t", matriz[i][j]); 
        }
        printf("\n");
    }
    
    printf("\n\n");

    //Zerando Tabela ASCII
    for (r = 0; r < 255; r++) {
        ascii[r] = 0;
    }

    //Transformando caracteres em inteiros (ASCII)
    for (r = 0; r < linhas && r < colunas; r++) {
        matrizAux[r] = int(matriz[r]);
    }

    //Contar quantidade de vezes
    for (r = 0; r < linhas && r < colunas; r++) {                       // Roda por todos os espaços 
        if (matrizAux[r] != 32) {                            // Retirar espaços da contagem
            ascii[matrizAux[r]]++;   //ERRO OCORRE AQUI
        }                             // Guardando quantidade de vezes que os caracteres se repetem
    }

    //Imprimindo repetidos
    for (r = 0; r < 255; r++) {
        if (ascii[r] > 0) {      //Verifica se é maior quer 0 
            printf("O caractere %c apareceu %d vez(es) na matriz\n", r, ascii[r]);
        }
    }

    printf("\n");

    system("pause");
    return(0);
}

Código do erro da matriz

  • Wow, how weird... How come your code makes exceptions if you’re programming in C? I’ve never seen this.

  • You declared matrix as a two-dimensional array but here: matrizAux[r] = int(matriz[r]); informs a single index, it seems wrong to me. Another thing is that you declare a 3x3 array but do not make any consistency when reading the variables linhas and colunas, it would not be better to declare the array after reading those variables?

1 answer

0

n use VS(then n can reproduce exactly the error of this code), but by "read access violation" and seeing the code I deduce q is:

1 - matriz[3][3] and then in the code

    for (int i = 0; i < linhas; i++)
        for (int j = 0; j < colunas; j++)
            scanf_s("%c", &matriz[i][j]);

vc declares the 3x3 matrix but uses it as rows x columns, it may occur that rows or columns are larger than the 3 size that has been declared (invalid access)

ideal would be either use fixed matrix size, or dynamic memory allocation

char **matriz = malloc (linhas * sizeof (char*)) ;
for (i=0; i < linhas; i++)
   matriz[i] = malloc (colunas * sizeof (char)) ;

2 - matrizAux[r] = int(matriz[r]); is a matrix and Voce is only going through a dimension, in C you have to go through each of all positions of the matrix

Browser other questions tagged

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