Advertisement
cristianocamposvr

linguagem c

Oct 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void vetorSoma(float *v1, float *v2, int tam);
  5. void vetorDiferenca(float *v1, float *v2, int tam);
  6. void vetorProduto(float *v1, float *v2, int tam);
  7. float buscaMaior(float *v1, float *v2, int tam, float *menor);
  8. float somaElementos(float *v1, float *v2, int tam, float *produto);
  9.  
  10. int main()
  11. {
  12. int i, tam, op;
  13. float *v1,*v2, maior, menor, soma, produto=1;
  14.  
  15. printf("\nInforme o tamanho do vetor: \n");
  16. scanf("%d", &tam);
  17.  
  18. v1=(float*)malloc(tam*sizeof(float));
  19. v2=(float*)malloc(tam*sizeof(float));
  20.  
  21. if(v1==NULL && v2==NULL)
  22. {
  23. printf("\nImpossível alocar memória\n");
  24.  
  25. }
  26.  
  27. printf("\n\nLeitura vetor 1: \n\n");
  28. for(i=0; i<tam; i++)
  29. {
  30. printf("\n\nv1 [%d]: ", i);
  31. scanf("%f", v1+i);
  32. }
  33.  
  34. printf("\nLeitura vetor 2: \n\n");
  35. for(i=0; i<tam; i++)
  36. {
  37. printf("\n\nv1 [%d]: ", i);
  38. scanf("%f", v2+i);
  39. }
  40.  
  41. printf("\nImpressão do vetor v1 : \n");
  42. for(i=0; i<tam; i++);
  43. {
  44. printf("%f", *(v1+i));
  45. }
  46.  
  47. printf("\nImpressão do vetor v2: \n");
  48. for(i=0; i<tam; i++);
  49. {
  50. printf("%f", *(v2+i));
  51. }
  52.  
  53. do
  54. {
  55.  
  56. printf("\n\n1-Vetor Soma \n2-Vetor Diferença \n3-Vetor Produto \n4-Maior Elemento de v1 e Menor Elemeno de v2 \n5-Soma entre os Elementos de v1 e Produto entre os Elementos de v2 \n6-Sair \n\nOpção:");
  57. scanf("%f", &op);
  58. switch(op)
  59. {
  60. case 1:
  61. vetorSoma(v1,v2,tam);
  62.  
  63. break;
  64.  
  65. case 2:
  66. vetorDiferenca(v1,v2,tam);
  67.  
  68. break;
  69.  
  70. case 3:
  71. vetorProduto(v1,v2,tam);
  72.  
  73. break;
  74.  
  75. case 4:
  76. maior=buscaMaior(v1,v2,tam, &menor);
  77. printf("\n\nMaior elemento: %f", maior);
  78. printf("\n\nMenor elemento: %f", menor);
  79. break;
  80.  
  81. case 5:
  82. soma=somaElementos(v1,v2,tam, &produto);
  83. printf("\nSoma entre os elementos de v1: %f", soma);
  84. printf("\nProduto elementos de v2: %f", produto);
  85. break;
  86.  
  87. case 6:
  88. printf("\n\nObrigado\n\n");
  89. break;
  90.  
  91. default:
  92. printf("\n\nInformação inválida\n\n");
  93.  
  94.  
  95.  
  96.  
  97. }
  98. }while(op!=6);
  99.  
  100. free(v1);
  101. free(v2);
  102. }
  103.  
  104. void vetorSoma(float *v1, float *v2, int tam)
  105. {
  106. int i;
  107. float v3[tam];
  108.  
  109. for(i=0; i<tam; i++)
  110. {
  111. *(v3+i)=*(v1+i)+*(v2+i);
  112. }
  113. printf("\n\nVetor Soma: \n");
  114. for(i=0; i<tam; i++)
  115. {
  116. printf("%f\t", *(v3+i));
  117. }
  118. }
  119.  
  120. void vetorDiferenca(float *v1, float *v2, int tam)
  121. {
  122. int i;
  123. float v3[tam];
  124.  
  125. for(i=0; i<tam; i++)
  126. {
  127. *(v3+i)=*(v1+i)-*(v2+i);
  128. }
  129. printf("\n\nVetor Diferença: \n");
  130. for(i=0; i<tam; i++)
  131. {
  132. printf("%f\t", *(v3+i));
  133. }
  134. }
  135. void vetorProduto(float *v1, float *v2, int tam)
  136. {
  137. int i;
  138. float v3[tam];
  139.  
  140. for(i=0; i<tam; i++)
  141. {
  142. *(v3+i)=*(v1+i)**(v2+i);
  143. }
  144. printf("\n\nVetor Produto: \n");
  145. for(i=0; i<tam; i++)
  146. {
  147. printf("%f\t", *(v3+i));
  148. }
  149. }
  150.  
  151. float buscaMaior(float *v1, float *v2, int tam, float *menor)
  152. {
  153. int i;
  154. float maior;
  155.  
  156. maior=*v1;
  157.  
  158. for(i=0; i<tam; i++)
  159. {
  160. if(*(v1+i)>maior)
  161. {
  162. maior=*(v1+i);
  163. }
  164. }
  165.  
  166. *menor=*v2;
  167.  
  168. for(i=0; i<tam; i++)
  169. {
  170. if(*(v2+i)<*menor)
  171. {
  172. *menor=*(v2+i);
  173. }
  174. }
  175.  
  176. return maior;
  177.  
  178.  
  179. }
  180. float somaElementos(float *v1, float *v2, int tam,float *produto)
  181. {
  182. int i;
  183. float soma=0;
  184.  
  185. for(i=0; i<tam; i++)
  186. {
  187. soma+=*(v1+i);
  188. }
  189.  
  190. for(i=0; i<tam; i++)
  191. {
  192. *produto=*produto**(v2+i);
  193. }
  194.  
  195. return(soma);
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement