Count rows and null columns of a matrix

Asked

Viewed 1,856 times

0

Guys, help me on the following question:

Write a complete program in C language that declares a square matrix of size 5x5. Then your program must fill this matrix randomly with 0s and 1s (the matrix generated must be different with each execution of the program). Finally, your program must inform how many null rows and columns the matrix has. Follow an example of program output. In this program the user does not provide input data. Generated matrix:

1 1 0 1 0
0 0 0 0 0
0 1 0 0 0
1 0 0 1 0
1 1 0 1 0

A matriz possui 1 linha e 2 colunas nulas!

The code I made cannot read the null rows and columns:

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

int main(int argc, char**argv){

    int matriz[5][5];
    int linha, coluna, x, count_linha, count_coluna;

    srand(time(NULL));

    printf("Matriz Gerada: \n");

    for(linha=0; linha<5; linha++){
        printf("\n");
            for(coluna=0; coluna<5; coluna++){
                x= rand() %2 + 0;
                printf("%3d",x);
                matriz[linha][coluna];
                if(linha==0){
                    count_linha++;
                }
                if(coluna==0){
                    count_coluna++;
                }
            }
    }
    printf("\n");
    printf("A matriz tem %d linhas nulas, e %d colunas nulas",count_linha, count_coluna);
}
  • I suggest separating your program into pieces (functions, if you already know what this is).

  • Only what your program does right is generate random numbers.

  • Pablo’s suggestion is good, but I don’t think Stack Overflow is the place to ask people to teach you how to do your job, but to ask specific questions about how to solve problems in it.

1 answer

2

As indicated in a comment by Pablo Almeida, try to separate the code into differentiated pieces (functions), each doing one thing.

For example, like this:

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

int main(int argc, char **argv) {    
    int matriz[5][5];
    int count_linha, count_coluna;

    // Primeiro: inicializar gerador de numeros aleatorios
    srand(time(NULL));

    // Segundo: preencher matriz
    for (int linha = 0; linha < 5; linha++) {
        for (int coluna = 0; coluna < 5; coluna++) {
            // int x = rand() % 2; // substitui para preencher com mais zeros
            int x = rand() % 4 - 2; if (x < 0) x = 0;
            matriz[linha][coluna] = x;
        }
    }

    // Terceiro: imprimir matriz
    printf("Matriz Gerada:\n");
    for (int linha = 0; linha < 5; linha++) {
        for (int coluna = 0; coluna < 5; coluna++) {
            printf("%3d", matriz[linha][coluna]);
        }
        printf("\n");
    }

    // Quarto: contar linhas nulas
    count_linha = 0;
    for (int linha = 0; linha < 5; linha++) {
        int zeros = 0;
        for (int coluna = 0; coluna < 5; coluna++) {
            if (matriz[linha][coluna] == 0) zeros++;
        }
        if (zeros == 5) count_linha++;
    }

    // Quinto: contar colunas nulas
    count_coluna = 0;
    for (int coluna = 0; coluna < 5; coluna++) {
        int zeros = 0;
        for (int linha = 0; linha < 5; linha++) {
            if (matriz[linha][coluna] == 0) zeros++;
        }
        if (zeros == 5) count_coluna++;
    }

    // Sexto: imprimir resultado final
    printf("A matriz tem %d linhas nulas e %d colunas nulas\n",
           count_linha, count_coluna);
    return 0;
}

You can see operating on the ideone.

  • @Joaosarabis (proponent of editing): use the definition of variables within the cycle for serves to make note better that the parts in question are isolated. C99 is almost 20 years old! If there are still compilers who do not accept it, for these compilers it would also be necessary to change the comments to /* ,,, */ instead of // ...

Browser other questions tagged

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