Advertisement
luizaspan

Soma elementos coluna/linha matriz

Jun 23rd, 2015
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define n 4
  5. #define m 5
  6.  
  7. int main(void)
  8. {
  9.     int A[m][n],i,j,sl,sc,k;
  10.  
  11.     // A= {{1,2,3},{4,5,6},}
  12.  
  13.     for (i=0;i<n;i++) // definindo os elementos
  14.     {
  15.         for (j=0;j<m;j++)
  16.         {
  17.             A[i][j]=rand() % (m*n);
  18.         }      
  19.     }
  20.  
  21.     for (i=0;i<n;i++) // apresentando a matriz
  22.     {
  23.         for (j=0;j<m;j++)
  24.         {  
  25.  
  26.             printf("%d ",A[i][j]);
  27.  
  28.             if (j==(m-1))
  29.                 {printf("\n");}
  30.         }
  31.     }
  32.  
  33.  
  34.     for (i=0;i<n;i++) // somando elementos de cada linha
  35.     {
  36.         sl=0;
  37.         for (j=0;j<m;j++)
  38.         {
  39.             sl+=A[i][j];
  40.         }
  41.         printf("Soma %d-ésima linha: %d \n",i+1,sl);
  42.     }
  43.  
  44.     for (j=0;j<m;j++) // somando elementos de cada coluna
  45.     {
  46.         sc=0;
  47.         for (i=0;i<n;i++)
  48.         {
  49.             sc+=A[i][j];
  50.         }
  51.         printf("Soma %d-ésima coluna: %d \n",j+1,sc);
  52.     }
  53.  
  54.  
  55.    
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement