fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. // define tipo matriz com campos linha, coluna e ponteiro
  4. typedef struct{
  5. int m;
  6. int n;
  7. double *p_data;
  8. }matriz_t;
  9. // define tipo vetor, uma struct com campos coluna e ponteiro
  10. typedef struct{
  11. int n;
  12. double *p_data;
  13. }vetor_t;
  14. //prototipo das fucncoes
  15. matriz_t le_matriz();
  16. vetor_t le_vetor();
  17. matriz_t prod(matriz_t A,matriz_t B);
  18. vetor_t solve(matriz_t C,vetor_t a);
  19. void mostra_vetor(vetor_t b);
  20.  
  21. int main(){
  22. matriz_t A,B,C;
  23. vetor_t a,b;
  24. A = le_matriz();
  25. B = le_matriz();
  26. a = le_vetor();
  27. C = prod(A,B);
  28. b = solve(C,a);
  29. mostra_vetor(b);
  30. return 0;
  31. }
  32.  
  33. matriz_t le_matriz(){
  34. matriz_t A;
  35. int i,j;
  36. printf("tam M1");
  37. // armazena n de linhas e de colunas da matriz
  38. scanf("%d",&A.m);
  39. scanf("%d",&A.n);
  40. printf("\n");
  41.  
  42. A.p_data = malloc(A.m*A.n*sizeof(double));
  43. printf("elem m1:\n");
  44. // armazena elementos matriz
  45. for(i=0;i<A.m;i++){
  46. for(j=0;j<A.n;j++){
  47. scanf("%lf", &A.p_data[i*A.n+j]);
  48. }
  49. }
  50. printf("\n");
  51. return A;
  52. }
  53.  
  54. vetor_t le_vetor(){
  55. vetor_t a;
  56. int i;
  57. printf("tam v1\n");
  58. // armazena tamanho vetor
  59. scanf("%d",&a.n);
  60.  
  61. a.p_data = malloc(a.n*sizeof(double));
  62. printf("\n");
  63. printf("elemv1:\n");
  64. // armazena elementos do vetor
  65. for(i=0;i<a.n;i++){
  66. scanf("%lf",&a.p_data[i*a.n]);
  67. }
  68. printf("\n");
  69. return a;
  70. }
  71.  
  72. matriz_t prod(matriz_t A, matriz_t B){
  73. matriz_t C;
  74. int i,j,k;
  75. C.p_data = malloc(A.m*B.n*sizeof(double));
  76. // condicao para o produto matriz matriz
  77. if(A.n == B.m){
  78. // percorre coluna matriz A
  79. for(i=0;i<A.n;i++){
  80. // percorre linha matriz B
  81. for(j=0;j<B.m;j++){
  82. // inicializa terceira matriz com 0
  83. C.p_data[i*A.n+j] =0;
  84. // calcula e armazena o resultado numa 3 matriz
  85. for(k=0;k<B.n;k++){
  86. C.p_data[i*A.n+j] = C.p_data[i*A.n+j]+(A.p_data[i*A.m+k]*B.p_data[k*B.n+j]);
  87. }
  88. }
  89. }
  90. return C;
  91. }
  92. }
  93.  
  94. vetor_t solve(matriz_t C, vetor_t a){
  95. vetor_t b;
  96. int i,j,k;
  97. b.p_data = malloc(C.m*a.n*sizeof(double));
  98. // condicao do produto matriz vetor
  99. if(C.m == a.n){
  100. // percorre linha matriz
  101. for(i=0;i<C.m;i++){
  102. // percorre coluna vetor
  103. for(j=0;j<a.n;j++){
  104. // terceira matriz para receber os valores calculados
  105. b.p_data[i*C.n+k] = 0;
  106. for(k=0;k<a.n;k++){
  107. b.p_data[i] = b.p_data[i]+(C.p_data[i*C.n+k]*a.p_data[k*a.n+j]);
  108. }
  109. }
  110. }
  111. return b;
  112. }
  113. }
  114.  
  115. void mostra_vetor(vetor_t b){
  116. int i;
  117. // mostra valores do vetor
  118. for(i=0;i<b.n;i++){
  119. printf("%lf\n",b.p_data[i]);
  120. }
  121. }
  122.  
  123.  
Success #stdin #stdout 0s 74944KB
stdin
Standard input is empty
stdout
tam M1
elem m1:

tam M1
elem m1:

tam v1

elemv1: