Generate specific numbers in the matrix

Asked

Viewed 37 times

0

So the question is a 4x4 matrix with 0s and 1s, but I was wondering if there’s any way I could get the matrix to print 0s and 1s in random orders, I just don’t know how and I was wondering if someone could help me

inserir a descrição da imagem aqui

below I made the code but with a numbers 1 and 0 that I set myself

#include "stdio.h" 

int main(void) {

    int contador1=0,contador0=0,cont;
    float MatOriginal[4][4]={1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0};
    //entrada de dados 
      printf(" \n A matriz informada eh = \n");
      for(int i=0; i<4; i++){
        printf(" \n ");
        for(int j=0; j<4; j++){
        printf(" %4.1f ", MatOriginal[i][j]);
        if ( MatOriginal[i][j]==1 ) {
          contador1+=1;
        }
            else{
              if(MatOriginal[i][j]==0){
                contador0+=1;
        }
            }
        }
      }
      printf("\n                                                       \n");
    //escolha do usuario
    printf("\n Escolha 1 para contar os 1s ou 0 para contar os 0s: \n");
    scanf("%i",&cont);
      if (cont==1){
        printf("\n o número de 1s na matriz é= %i\n ",contador1);
      }
        else{
          if (cont==0){
            printf("\n o número de 0s na matriz é= %i \n",contador0);
          }
      }
    return 0;
}
  • 2

    Have you ever thought of using srand(); for(i=0; i<4; i++) for(j=0; j<4; j++) MatOriginal[i][j] = rand() % 2;?

No answers

Browser other questions tagged

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