Count rows and null columns of an array

0

Please, help me with the following question:

Write a complete C-language program that declares a square-sized array 5x5. Then, your program must populate this array randomly with 0s and 1s (the array should be different for each program run). Finally, your program should inform how many rows and null columns the array has. Here is an example of program output. In that program does not provide input data. Matrix generated:

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 that I can not read the rows and null 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);
}
    
asked by anonymous 14.05.2016 / 06:19

1 answer

2

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

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 working on ideone .

    
14.05.2016 / 12:42