1 / 11

Estrutura de Dados Avançada

Estrutura de Dados Avançada. Matrizes Bidimensionais Prof. Mário Dantas. Forma geral da declaração de uma matriz bidimensional. tipo_da_variável nome_da_variável [ linha ][ coluna ];

sydnee-case
Télécharger la présentation

Estrutura de Dados Avançada

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Estrutura de Dados Avançada Matrizes Bidimensionais Prof. Mário Dantas

  2. Forma geral da declaração de uma matriz bidimensional • tipo_da_variávelnome_da_variável [linha][coluna]; • Quando vamos preencher ou ler uma matriz no C o índice mais à direita (coluna) varia mais rapidamente que o índice à esquerda (linha);

  3. Exemplo de Matriz #include <stdio.h> intmain () { intmtrx [20][10]; int i,j,count; count=1; for (i=0;i<20;i++)   for (j=0;j<10;j++)    { mtrx[i][j]=count; count++;    } return(0); }

  4. Matrizes de strings • A forma geral de uma matriz de strings: charnome_da_variável [num_de_strings][compr_das_strings]; • como acessar uma string individual? Fácil. É só usar apenas o primeiro índice. Então, para acessar uma determinada string faça: nome_da_variável [índice] ;

  5. Exemplo #include <stdio.h> intmain () { char strings [5][100]; int i; for (i=0; i<5; i++) { printf ("Digite uma string: "); gets (strings[i]); } printf ("\nAs strings que voce digitou foram:\n\n"); for (i=0; i<5; i++) printf ("%s\n",strings[i]); return(0); }

  6. Matrizes multidimensionais • A forma geral de uma matriz multidimensional: tipo_da_variávelnome_da_variável [tam1][tam2] ... [tamN]; • Uma matriz N-dimensional funciona basicamente como outros tipos de matrizes. • Lembrar que o índice que varia mais rapidamente é o índice mais à direita.

  7. Inicialização • A forma geral de uma matriz como inicialização é: tipo_da_variávelnome_da_variável [tam1][tam2] ... [tamN] = {lista_de_valores}; • A lista de valores é composta por valores (do mesmo tipo da variável) separados por vírgula. • Os valores devem ser dados na ordem em que serão colocados na matriz.   

  8. Inicialização • Alguns exemplos de inicializações de matrizes: floatvect [6] = { 1.3, 4.5, 2.7, 4.1, 0.0, 100.1 };         intmatrx [3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };        intmatrx [3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };   charstr [10] = { 'J', 'o', 'a', 'o', '\0' };         charstr [10] = "Joao";         charstr_vect [3][10] = { "Joao", "Maria", "Jose" };

  9. Inicialização • charmess [] = "Linguagem C: flexibilidade e poder.";                 • intmatrx [][2] = { 1,2,2,4,3,6,4,8,5,10 }; • intmatrx [][2] = {{1,2},{2,4},{3,6},{4,8},{5,10} };

  10. AUTO AVALIAÇÃO # include <stdio.h> intmain() { int t, i, M[3][4];     for (t=0; t<3; ++t)         for (i=0; i<4; ++i)             M[t][i] = (t*4)+i+1;     for (t=0; t<3; ++t)     {         for (i=0; i<4; ++i) printf ("%3d ", M[t][i]); printf ("\n");     } return(0);}

  11. Referências • http://www.mtm.ufsc.br/~azeredo/cursoC/c.html

More Related