Guest User

Untitled

a guest
May 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. int main(){
  6. int x, y, i, j;
  7. srand(time(NULL));
  8. printf("Digite o tamanho do vetor\n");
  9. scanf("%d %d", &x, &y);
  10. int vet[x][y], somaLinha[x], somaColuna[y], somaDiagonal = 0;
  11.  
  12. for(i = 0; i < x; i++){
  13. somaLinha[i] = 0;
  14. somaColuna[i] = 0;
  15. for (j = 0; j < y; j++)
  16. vet[i][j] = rand() % 100;
  17. }
  18. printf("Soma das linhas\nMatriz:\n");
  19. for(i = 0; i < x; i++){
  20. for (j = 0; j < y; j++){
  21. printf(" %d ", vet[i][j]);
  22. somaLinha[i] = vet[i][j] + somaLinha[i];
  23. }
  24. printf(" = %d", somaLinha[i]);
  25. printf("\n");
  26. }
  27. printf("\n\nSoma das colunas\nMatriz:\n");
  28. for(i = 0; i < x; i++){
  29. for (j = 0; j < y; j++){
  30. printf(" %d ", vet[i][j]);
  31. somaColuna[j] = somaColuna[j] + vet[i][j];
  32. }
  33. printf("\n");
  34. }
  35.  
  36. printf("\n");
  37.  
  38. for (j = 0; j < 3; j++){
  39. printf(" %d ", somaColuna[j]);
  40. }
  41. int aux = 0;
  42. if (x == y){
  43. for (i = 0; i < x; i++){
  44. for (j = 0; j < y; j++){
  45. if (aux == 0)
  46. somaDiagonal += vet[i][j];
  47.  
  48. aux++;
  49. }
  50. if (aux % (i + x) == 0)
  51. somaDiagonal += vet[i][j];
  52. //somaDiagonal += vet[i][aux];
  53. }
  54. }else{
  55. printf("Nao existe diagonal de funcao que nao seja de linhas e colunas iguais");
  56. }
  57.  
  58. printf("\nSoma da diagonal principal: %d", somaDiagonal -1);
  59. return 0;
  60.  
  61. }
Add Comment
Please, Sign In to add comment