Advertisement
gpsgiraldi

2024_ava10_completo

May 26th, 2024 (edited)
634
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | Source Code | 0 0
  1. /******************************************************************************
  2. ESCREVA UM ALGORITMO QUE LEIA UMA MATRIZ DE ENTRADAS INFORMADAS PELO
  3. USUÁRIO E DEFINA SE A MATRIZ É IDENTIDADE, NULA OU NENHUMA DAS OPÇÕES
  4. *******************************************************************************/
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.  
  10.     int n,aux,aux2;
  11.    
  12. //definição da ordem da matriz
  13.     printf("Insira uma ordem para a matriz: ");
  14.     scanf("%i\n",&n);
  15.     int mat[n][n];
  16.    
  17. //definição das entradas da matriz
  18.     for(int i=0;i<n;i++){
  19.         for(int j=0;j<n;j++){
  20.             scanf("%i",&mat[i][j]);
  21.         }
  22.     }
  23.  
  24. //cálculo do tipo da matriz
  25.     for(int i=0;i<n;i++){
  26.         for(int j=0;j<n;j++){
  27.             if(i==j){
  28.                 switch(mat[i][j]){
  29.                     case 0:
  30.                         aux=0;
  31.                         break;
  32.                     case 1:
  33.                         aux=1;
  34.                         break;
  35.                 }
  36.                 if(mat[i][j]!=0&&mat[i][j]!=1){
  37.                     aux=2;
  38.                 }
  39.             }
  40.             else{
  41.                 if(mat[i][j]==0){
  42.                     aux2=aux;
  43.                 }
  44.                 else{
  45.                     aux2=2;
  46.                 }
  47.             }
  48.         }
  49.     }
  50.     if(aux==aux2){
  51.         aux=aux;
  52.     }
  53.     else{
  54.         aux=2;
  55.     }
  56. //detalhamento da matriz
  57.     for(int i=0;i<n;i++){
  58.         for(int j=0;j<n;j++){
  59.             printf("%d ",mat[i][j]);
  60.         }
  61.     printf("\n");
  62.     }
  63.     switch(aux){
  64.         case 0:
  65.         printf("matriz zero");
  66.         break;
  67.         case 1:
  68.         printf("matriz identidade");
  69.         break;
  70.         case 2:
  71.         printf("nenhuma das opcoes");
  72.         break;
  73.     }
  74.     return 0;
  75. }
  76.  
Advertisement
Comments
  • gpsgiraldi
    17 days
    # C 0.58 KB | 0 0
    1. O cálculo é composto de dois testes, o primeiro testa a diagonal principal(m=n ou i=j). O segundo testa os demais valores(m=/=n ou i=/=j).
    2.  
    3. Para a matriz ser nula, o primeiro e o segundo testes devem encontrar tudo zeros. Define-se uma variável auxiliar como '0' para saída posterior.
    4.  
    5. No caso da matriz identidade, o primeiro teste deve encontrar tudo '1' mas o segundo teste, por sua vez, deve encontrar tudo '0'(igual o teste da matriz nula).
    6.  
    7. Para o caso de não ser nenhuma das opções, atribui-se um condicional composto de 'e'(&&)(não é '0' e não é '1')(nem '0' nem '1').
Add Comment
Please, Sign In to add comment
Advertisement