Advertisement
weldisalves

Lista 05 - exercício 17

Jun 28th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. /* 17. Preencha, com números randômicos, uma matriz de 8 linhas por 20 colunas. A faixa dos
  5. números aleatórios deverá ser solicitada ao usuário. Calcule a quantidade de números pares e
  6. impares em cada linha da matriz. Calcule também o percentual destes em relação a quantidade
  7. total de números da matriz.
  8. Exiba a matriz num formato tabular indicando as quantidades e percentuais calculados. */
  9.  
  10. int main()
  11. {
  12.     srand(time(NULL));
  13.     int i,j,quantLinhas,quantColunas,total=0,limiteInferior,limiteSuperior;
  14.  
  15.     printf("\n Digite a quantidade de linhas e colunas da matriz: ");
  16.     scanf("%d %d",&quantLinhas,&quantColunas);
  17.  
  18.     printf("\n Digite o limite inferior e superior do sorteio: ");
  19.     scanf("%d %d",&limiteInferior,&limiteSuperior);
  20.  
  21.     int matriz[quantLinhas][quantColunas];
  22.     float estatistica[quantLinhas][4];
  23.  
  24.     for(i=0;i<quantLinhas;i++)
  25.     {
  26.         for(j=0;j<quantColunas;j++)
  27.         {
  28.             estatistica[i][j]=0;
  29.         }
  30.         printf("\n");
  31.     }
  32.  
  33.     printf("\n Matriz:\n ");
  34.     for(i=0;i<quantLinhas;i++)
  35.     {
  36.         for(j=0;j<quantColunas;j++)
  37.         {
  38.             matriz[i][j]=(rand()%(limiteSuperior-limiteInferior+1)+limiteInferior);
  39.             printf("%2d ",matriz[i][j]);
  40.             total++;
  41.  
  42.             if(matriz[i][j]%2==0)
  43.             {
  44.                 estatistica[i][0]++;
  45.             }else{
  46.                 estatistica[i][1]++;
  47.                 }
  48.         }
  49.         printf("\n");
  50.     }
  51.  
  52.     printf("\n Estatistica:\n");
  53.     printf("\n  P   I    Pp   Ip");
  54.     printf("\n");
  55.     for(i=0;i<quantLinhas;i++)
  56.     {
  57.         estatistica[i][2]=((estatistica[i][0]*100)/total);
  58.         estatistica[i][3]=((estatistica[i][1]*100)/total);
  59.  
  60.         for(j=0;j<4;j++)
  61.         {
  62.             printf("%.2f ",estatistica[i][j]);
  63.         }
  64.         printf("\n");
  65.     }
  66.  
  67.     getchar();
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement