{"id":604,"date":"2016-11-03T08:00:53","date_gmt":"2016-11-03T10:00:53","guid":{"rendered":"http:\/\/www.galirows.com.br\/meublog\/programacao\/?p=604"},"modified":"2016-10-25T16:07:24","modified_gmt":"2016-10-25T18:07:24","slug":"exercicio-operacoes-vetor-utilizando-funcoes","status":"publish","type":"post","link":"http:\/\/www.galirows.com.br\/meublog\/programacao\/exercicio-operacoes-vetor-utilizando-funcoes\/","title":{"rendered":"Exerc\u00edcio com opera\u00e7\u00f5es em vetor utilizando fun\u00e7\u00f5es"},"content":{"rendered":"<div class=\"box generalbox\">\n<div class=\"no-overflow\">\n<p>Nesse post apresente um exerc\u00edcio em <strong>linguagem de programa\u00e7\u00e3o C <\/strong>para ser utilizado no Laborat\u00f3rio Virtual de Programa\u00e7\u00e3o do Moodle.<\/p>\n<p>O exerc\u00edcio traz todo o c\u00f3digo necess\u00e1rio da fun\u00e7\u00e3o main() e exige ao aluno interpretar o c\u00f3digo e posteriormente implementar as <strong>fun\u00e7\u00f5es<\/strong> para que o c\u00f3digo funcione. As fun\u00e7\u00f5es s\u00e3o implementadas em uma <strong>biblioteca<\/strong>, mantendo o c\u00f3digo das fun\u00e7\u00f5es a parte do programa principal.<\/p>\n<hr \/>\n<p>Observe a fun\u00e7\u00e3o main() e elabore as seguintes fun\u00e7\u00f5es utilizadas por essa fun\u00e7\u00e3o:<\/p>\n<ul>\n<li>maiorValor: retorna o maior valor do vetor.<\/li>\n<li>mediaVetor: retorna a m\u00e9dia dos valores do vetor.<\/li>\n<li>subtraiVetor: subtra\u00ed uma constante de cada valor do vetor.<\/li>\n<li>zeraNegativos: subtitu\u00ed todo valor negativo do vetor por zero.<\/li>\n<\/ul>\n<p>Codifique as fun\u00e7\u00f5es no arquivo &#8220;biblioteca.h&#8221; e n\u00e3o altere nada no arquivo &#8220;solucao.c&#8221;.<\/p>\n<p><strong>C\u00f3digos iniciais (arquivos requeridos)<br \/>\n<\/strong><\/p>\n<pre class=\"lang:c decode:true \" title=\"solucao.c\">#include &lt;stdio.h&gt;\r\n#include \"biblioteca.h\"\r\n\r\nint main()\r\n{\r\n    int i, n;\r\n    float x;\r\n    scanf(\"%i\", &amp;n); \/\/l\u00ea o tamanho do vetor\r\n    int vet[n];\r\n\r\n    \/\/preenche vetor\r\n    for (i = 0; i &lt; n; i++) {\r\n        scanf(\"%i\", &amp;vet[i]);\r\n    }\r\n\r\n    mostraVetor(vet, n);\r\n    printf(\"\\n%i | %f\\n\", maiorValor(vet, n), mediaVetor(vet, n));\r\n\r\n    x = (int)(maiorValor(vet, n) - mediaVetor(vet, n));\r\n    subtraiVetor(vet, n, x);\r\n    zeraNegativos(vet, n);\r\n    mostraVetor(vet, n);\r\n    printf(\"\\n%i%f\", maiorValor(vet, n), mediaVetor(vet, n));\r\n\r\n    return 0;\r\n}<\/pre>\n<pre class=\"lang:c decode:true \" title=\"biblioteca.h\">void mostraVetor(int vet[], int tam){\r\n    int i;\r\n    for (i = 0; i &lt; tam; i++) {\r\n        printf(\"%i \", vet[i]);\r\n    }\r\n}\r\n\r\nint maiorValor(int vet[], int tam) {\r\n    \/\/fun\u00e7\u00e3o recebe um vetor e o tamanho dele\r\n    \/\/e retorna o maior valor existente nele\r\n}\r\n\r\nfloat mediaVetor(int vet[], int tam) {\r\n    \/\/fun\u00e7\u00e3o recebe um vetor e o tamanho dele\r\n    \/\/e retorna a m\u00e9dia dos valores do vetor\r\n}\r\n\r\nvoid subtraiVetor(int vet[], int tam, int x) {\r\n    \/\/fun\u00e7\u00e3o recebe um vetor, o tamanho dele e um n\u00famero\r\n    \/\/e subtrai cada valor do vetor do n\u00famero passado\r\n}\r\n\r\nvoid zeraNegativos(int vet[], int tam) {\r\n    \/\/fun\u00e7\u00e3o recebe um vetor e o tamanho dele\r\n    \/\/e substitui por zero cada n\u00famero negativo existente no vetor\r\n}\r\n<\/pre>\n<p><strong>Implementa\u00e7\u00e3o das fun\u00e7\u00f5es da biblioteca<\/strong><\/p>\n<\/div>\n<div class=\"no-overflow\">\n<pre class=\"lang:c decode:true \">void mostraVetor(int vet[], int tam){\r\n    int i;\r\n    for (i = 0; i &lt; tam; i++) {\r\n        printf(\"%i \", vet[i]);\r\n    }\r\n}\r\n\r\nint maiorValor(int vet[], int tam) {\r\n    int maior = vet[0], i;\r\n    for (i = 1; i &lt; tam; i++) {\r\n        if (vet[i] &gt;  maior) {\r\n            maior = vet[i];\r\n        }\r\n    }\r\n    return maior;\r\n}\r\n\r\nfloat mediaVetor(int vet[], int tam) {\r\n    int i, soma = 0;\r\n    for (i = 0; i &lt; tam; i++) {\r\n        soma += vet[i];\r\n    }\r\n    return soma \/ (float)tam;\r\n}\r\n\r\nvoid subtraiVetor(int vet[], int tam, int x) {\r\n    int i;\r\n    for (i = 0; i &lt; tam; i++) {\r\n            vet[i] -= x;\r\n    }\r\n}\r\n\r\nvoid zeraNegativos(int vet[], int tam) {\r\n    int i;\r\n    for (i = 0; i &lt; tam; i++) {\r\n        if (vet[i] &lt; 0) {\r\n            vet[i] = 0;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Casos de teste para o Laborat\u00f3rio Virtual de Programa\u00e7\u00e3o<\/strong><\/p>\n<pre class=\"font-size:8 height-set:true height:150 lang:tex decode:true\">case=teste1\r\ninput=5\r\n1 2 3 4 5\r\noutput=0 0 1 2 3\r\n31.200000\r\n\r\ncase=teste2\r\ninput=6\r\n0 5 6 1 2 4\r\noutput=0 2 3 0 0 1\r\n31.000000\r\n\r\ncase=teste3\r\ninput=8\r\n-5 6 1 2 -3 -2 1 2\r\noutput=0 1 0 0 0 0 0 0\r\n10.125000\r\n\r\ncase=teste4\r\ninput=4\r\n10 9 8 7\r\noutput=9 8 7 6\r\n97.500000<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Nesse post apresente um exerc\u00edcio em linguagem de programa\u00e7\u00e3o C para ser utilizado no Laborat\u00f3rio Virtual de Programa\u00e7\u00e3o do Moodle. O exerc\u00edcio traz todo o c\u00f3digo necess\u00e1rio da fun\u00e7\u00e3o main() e exige ao aluno interpretar o c\u00f3digo e posteriormente implementar as fun\u00e7\u00f5es para que o c\u00f3digo funcione. As fun\u00e7\u00f5es s\u00e3o implementadas em uma biblioteca, mantendo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[12,32,33,14,34],"class_list":["post-604","post","type-post","status-publish","format-standard","hentry","category-c","tag-funcao","tag-laboratorio-virtual-de-programacao","tag-lvp","tag-vetor","tag-vpl"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/comments?post=604"}],"version-history":[{"count":4,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/604\/revisions"}],"predecessor-version":[{"id":1068,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/604\/revisions\/1068"}],"wp:attachment":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/media?parent=604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/categories?post=604"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/tags?post=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}