{"id":594,"date":"2016-10-20T10:34:33","date_gmt":"2016-10-20T12:34:33","guid":{"rendered":"http:\/\/www.galirows.com.br\/meublog\/programacao\/?p=594"},"modified":"2021-06-18T15:25:25","modified_gmt":"2021-06-18T18:25:25","slug":"exercicios-simples-com-matrizes","status":"publish","type":"post","link":"http:\/\/www.galirows.com.br\/meublog\/programacao\/exercicios-simples-com-matrizes\/","title":{"rendered":"Exerc\u00edcios simples com matrizes"},"content":{"rendered":"\n<p>Nesse post s\u00e3o apresentados 3 exerc\u00edcios resolvidos que utilizam matrizes. O primeiro exerc\u00edcio \u00e9 resolvido com e sem modulariza\u00e7\u00e3o (uso de fun\u00e7\u00e3o) e os demais utilizam a fun\u00e7\u00e3o elaborada no primeiro exerc\u00edcio, com altera\u00e7\u00e3o para deixar a fun\u00e7\u00e3o mais gen\u00e9rica.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Algoritmo 1<\/strong> &#8211; Elabore um algoritmo que preencha duas matrizes 10&#215;10 com valores aleat\u00f3rios 0 e 1 e em seguida, determine se as matrizes s\u00e3o iguais (possuem os mesmos valores em cada posi\u00e7\u00e3o).<\/p>\n\n\n\n<p><strong>Solu\u00e7\u00e3o em linguagem C sem utilizar fun\u00e7\u00f5es<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code lang:c decode:true\"><code lang=\"c\" class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;time.h&gt;\n\n#define TAM 2\n\nint main() {\n    srand(time(NULL));\n\n    int m1[TAM][TAM], m2[TAM][TAM], i, j, igual = 1;\n\n    \/\/preenche as matrizes\n    for (i = 0; i &lt; TAM; i++) {\n        for (j = 0; j &lt;  TAM; j++) {\n            m1[i][j] = rand()%2;\n            m2[i][j] = rand()%2;\n        }\n    }\n\n    \/\/mostra a primeira matriz\n    for (i = 0; i &lt; TAM; i++) {\n        for (j = 0; j &lt;  TAM; j++) {\n            printf(\"%i \", m1[i][j]);\n        }\n        printf(\"\\n\");\n    }\n    printf(\"\\n\");\n\n    \/\/mostra a segunda matriz\n    for (i = 0; i &lt; TAM; i++) {\n        for (j = 0; j &lt;  TAM; j++) {\n            printf(\"%i \", m2[i][j]);\n        }\n        printf(\"\\n\");\n    }\n\n    \/\/testa se matrizes s\u00e3o iguais\n    for (i = 0; i &lt; TAM; i++) {\n        for (j = 0; j &lt;  TAM; j++) {\n            if (m1[i][j] != m2[i][j]) {\n                igual = 0;\n            }\n        }\n    }\n\n    if (igual == 1) {\n        printf(\"Matrizes iguais\");\n    } else {\n        printf(\"Matrizes diferentes\");\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Solu\u00e7\u00e3o utilizando fun\u00e7\u00f5es<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code lang:c decode:true\"><code lang=\"c\" class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;time.h&gt;\n\n#define TAM 2\n\nvoid preencheMatriz(int tam, int mat[tam][tam]) {\n    int i, j;\n    for (i = 0; i &lt; tam; i++) {\n        for (j = 0; j &lt;  tam; j++) {\n            mat[i][j] = rand()%2;\n        }\n    }\n}\n\nvoid mostraMatriz(int tam, int mat[tam][tam]) {\n    int i, j;\n    for (i = 0; i &lt; tam; i++) {\n        for (j = 0; j &lt;  tam; j++) {\n            printf(\"%i \", mat[i][j]);\n        }\n        printf(\"\\n\");\n    }\n}\n\nint main() {\n    srand(time(NULL));\n\n    int m1[TAM][TAM], m2[TAM][TAM], i, j, igual = 1;\n\n    preencheMatriz(TAM, m1);\n    preencheMatriz(TAM, m2);\n\n    mostraMatriz(TAM, m1);\n    printf(\"\\n\");\n    mostraMatriz(TAM, m2);\n\n    for (i = 0; i &lt; TAM; i++) {\n        for (j = 0; j &lt;  TAM; j++) {\n            if (m1[i][j] != m2[i][j]) {\n                igual = 0;\n            }\n        }\n    }\n\n    if (igual == 1) {\n        printf(\"Matrizes iguais\");\n    } else {\n        printf(\"Matrizes diferentes\");\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/0pMAbAsDaBM\" allowfullscreen=\"allowfullscreen\" width=\"560\" height=\"315\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<p><strong>Link para o v\u00eddeo: <\/strong><a href=\"https:\/\/youtu.be\/0pMAbAsDaBM\" target=\"_blank\" rel=\"noopener\">https:\/\/youtu.be\/0pMAbAsDaBM<\/a><strong><br><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Algoritmo 2<\/strong> &#8211; Dada uma matriz de inteiros&nbsp; <i>A<sub>mxn<\/sub><\/i>, verificar se existem elementos repetidos em <i>A<\/i>.<\/p>\n\n\n\n<pre class=\"wp-block-code lang:c decode:true\"><code lang=\"c\" class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;time.h&gt;\n\n#define TAML 2\n#define TAMC 3\n\nvoid preencheMatriz(int lin, int col, int mat[lin][col]) {\n    srand(time(NULL));\n    int i, j;\n    for (i = 0; i &lt; lin; i++) {\n        for (j = 0; j &lt;  col; j++) {\n            mat[i][j] = rand()%10;\n        }\n    }\n}\n\nvoid mostraMatriz(int lin, int col, int mat[lin][col]) {\n    int i, j;\n    for (i = 0; i &lt; lin; i++) {\n        for (j = 0; j &lt;  col; j++) {\n            printf(\"%i \", mat[i][j]);\n        }\n        printf(\"\\n\");\n    }\n}\n\nint main() {\n    int mat[TAML][TAMC], i, j, k, w, valTeste, repetido = 0;\n\n    preencheMatriz(TAML, TAMC, mat);\n    mostraMatriz(TAML, TAMC, mat);\n\n    for (k = 0; k &lt; TAML; k++) {\n        for (w = 0; w &lt; TAMC; w++) {\n            valTeste = mat[k][w];\n\n            for (i = 0; i &lt; TAML; i++) {\n                for (j = 0; j &lt; TAMC; j++) {\n                    if (mat[i][j] == valTeste) {\n                        repetido++;\n                    }\n                }\n            }\n        }\n    }\n    printf(\"Repetido: %i\\n\", repetido);\n\n    if (repetido &gt; TAML * TAMC) {\n        printf(\"Valores repetem\");\n    } else {\n        printf(\"Valores nao repetem\");\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/GT_lpSUsTIk\" allowfullscreen=\"allowfullscreen\" width=\"560\" height=\"315\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<p><strong>Link para o v\u00eddeo: <\/strong><a href=\"https:\/\/youtu.be\/GT_lpSUsTIk\" target=\"_blank\" rel=\"noopener\">https:\/\/youtu.be\/GT_lpSUsTIk<\/a><strong><br><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Algoritmo 3<\/strong> &#8211; Dada uma matriz&nbsp; <i>A<sub>mxn<\/sub><\/i>, imprimir o n\u00famero de linhas e o n\u00famero de colunas nulas da matriz.<\/p>\n\n\n\n<p>Exemplo com uma matriz quadrada de ordem 4 (m = 4 e n = 4)<\/p>\n\n\n\n<center><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.ime.usp.br\/%7Emacmulti\/figuras\/Image66.gif\" width=\"96\" height=\"96\"><\/center>\n\n\n\n<center><\/center>\n\n\n\n<center>Resultado: Linhas nulas: 2 | Colunas nulas: 1<\/center>\n\n\n\n<center><\/center>\n\n\n\n<pre class=\"wp-block-code lang:c decode:true\"><code lang=\"c\" class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;time.h&gt;\n\n#define TAML 3\n#define TAMC 2\n\nvoid preencheMatriz(int lin, int col, int mat[lin][col]) {\n    srand(time(NULL));\n    int i, j;\n    for (i = 0; i &lt; lin; i++) {\n        for (j = 0; j &lt;  col; j++) {\n            mat[i][j] = rand()%2;\n        }\n    }\n}\n\nvoid mostraMatriz(int lin, int col, int mat[lin][col]) {\n    int i, j;\n    for (i = 0; i &lt; lin; i++) {\n        for (j = 0; j &lt;  col; j++) {\n            printf(\"%i \", mat[i][j]);\n        }\n        printf(\"\\n\");\n    }\n}\n\nint main() {\n    int mat[TAML][TAMC], i, j, zeros, linhas = 0, colunas = 0;\n\n    preencheMatriz(TAML, TAMC, mat);\n    mostraMatriz(TAML, TAMC, mat);\n\n    \/\/conta linhas nulas\n    for (i = 0; i &lt; TAML; i++) {\n        zeros = 0;\n        for (j = 0; j &lt; TAMC; j++) {\n            if (mat[i][j] == 0) {\n                zeros++;\n            }\n        }\n        if (zeros == TAMC) {\n            linhas++;\n        }\n    }\n    printf(\"\\nLinhas nulas: %i\", linhas);\n\n    \/\/conta colunas nulas\n    for (j = 0; j &lt; TAMC; j++) {\n        zeros = 0;\n        for (i = 0; i &lt; TAML; i++) {\n            if (mat[i][j] == 0) {\n                zeros++;\n            }\n        }\n        if (zeros == TAML) {\n            colunas++;\n        }\n    }\n    printf(\"\\nColunas nulas: %i\", colunas);\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/NZCC7SE-O30\" allowfullscreen=\"allowfullscreen\" width=\"560\" height=\"315\" frameborder=\"0\"><\/iframe><\/p>\n\n\n\n<p><strong>Link para o v\u00eddeo:<\/strong> <a href=\"https:\/\/youtu.be\/NZCC7SE-O30\" target=\"_blank\" rel=\"noopener\">https:\/\/youtu.be\/NZCC7SE-O30<\/a><strong><br><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nesse post s\u00e3o apresentados 3 exerc\u00edcios resolvidos que utilizam matrizes. O primeiro exerc\u00edcio \u00e9 resolvido com e sem modulariza\u00e7\u00e3o (uso de fun\u00e7\u00e3o) e os demais utilizam a fun\u00e7\u00e3o elaborada no primeiro exerc\u00edcio, com altera\u00e7\u00e3o para deixar a fun\u00e7\u00e3o mais gen\u00e9rica.<\/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,4],"tags":[6,36,12,37,13],"class_list":["post-594","post","type-post","status-publish","format-standard","hentry","category-c","category-videos","tag-desvio-condicional","tag-for","tag-funcao","tag-if","tag-matriz"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/594","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=594"}],"version-history":[{"count":5,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/594\/revisions"}],"predecessor-version":[{"id":1070,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/posts\/594\/revisions\/1070"}],"wp:attachment":[{"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/media?parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/categories?post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.galirows.com.br\/meublog\/programacao\/wp-json\/wp\/v2\/tags?post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}