quarta-feira, 25 de fevereiro de 2015

Matriz tridimensional estática

O C nos dá amplas possibilidades de criar e utilizar matrizes com mais de duas dimensões.Sinceramente não sei o limite exato das dimensões suportadas, más de uma coisa sei, matrizes de três ou mais dimensões não são recomendadas em uso estático devido à grande quantidade de memória que elas consomem.
Para calcular o consumo de memória de sua matriz, basta multiplicar o número
de linhas por colunas vêzes o total de bytes do tipo.
Aqui no meu computador um inteiro vale 4 bytes, como a matriz apresentada é tridimensional, com tamanhos de 4, o consumo total ficou em 256 bytes.
Cabe ao programador em C optar por alocar dinamicamente matrizes multidimensionais acima de duas dimensões, isto traz melhores desempenho para seus programas em C.
O exemplo aqui proposto imprime uma matriz tridimensional, e ainda permite uma
precisa pesquisa pelos elementos contidos.

Veja abaixo imagens do programa em execução:






Veja abaixo, o código do programa:




#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define tam 4
void Moldura ( int a, int c, int b, int d, int e, int f ) {
         system ( "Color 10" );
     for ( a = e; a < c; a++ ) {
         for ( b = f; b < d; b++ ) {
              gotoxy ( b, a );
              textbackground ( WHITE );
              printf ( " " );
         }
     }
}
int Imp_Matr ( char *num ){
    unsigned int i;
    for( i = 0; i < strlen ( num ); i++ ){
        if ( num [ i ] < '0' || num [ i ] > '9' )
             return 0;
    }
    return 1;
}
int main(void){
    system("title MATRIZ TRIDIMENSIONAL ESTÁTICA");
    int M [ tam ] [ tam ] [ tam ] = {
                       {{ 1, 2, 3, 4},
                       { 5, 6, 7, 8 },
                       { 9, 10, 11, 12},
                       { 13, 14, 15, 16}},

                       {{ 17, 18, 19, 20},
                        { 21, 22, 23, 24},
                        { 25, 26, 27, 28},
                        { 29, 30, 31, 32}},

                        {{ 33, 34, 35, 36},
                         { 37, 38, 39, 40},
                         { 41, 42, 43, 44},
                         { 45, 46, 47, 48}},

                        {{ 49, 50, 51, 52},
                         { 53, 54, 55, 56},
                         { 57, 58, 59, 60},
                         { 61, 62, 63, 64}}};
     do {
         system ( "cls" );
         Moldura ( 2, 30, 2, 79, 2, 3 );
         int i, j, l, n;
         char num [ 3 ];
         textcolor ( LIGHTRED );
         gotoxy ( 36, 5 );
         printf ( "MATRIZ TRIDIMENSIONAL ESTÁTICA" );
         textcolor ( WHITE );
         gotoxy ( 5, 5 );
         int a = 15;
         int b = 20;
         int c = 25;
         textcolor ( BLACK );
         for ( i = 0; i < tam; i++ ) {
              for ( j = 0; j < tam; j++ ) {
                   for ( l = 0; l < tam; l++ ) {
                        printf ( "%5d", M [ i ] [ j ] [ l ] );
                        if ( M [ i ] [ j ] [ l ] == 4 )
                            gotoxy ( 5, i + 6 );
                        if ( M [ i ] [ j ] [ l ] == 8 )
                            gotoxy ( 5, i + 7 );
                        if ( M [ i ] [ j ] [ l ] == 12 )
                            gotoxy ( 5, i + 8 );
                        if ( M [ i ] [ j ] [ l ] == 16 )
                            gotoxy ( 5, i + 10 );
                        if ( M [ i ] [ j ] [ l ] == 20 )
                            gotoxy ( 5, a - 4 );
                        if ( M [ i ] [ j ] [ l ] == 24 )
                            gotoxy ( 5, i + 11 );
                        if ( M [ i ] [ j ] [ l ] == 28 )
                            gotoxy ( 5, i + 12 );
                        if ( M [ i ] [ j ] [ l ] == 32 )
                            gotoxy ( 5, b - 5 );
                        if ( M [ i ] [ j ] [ l ] == 36 )
                            gotoxy ( 5, i + 14 );
                        if ( M [ i ] [ j ] [ l ] == 40 )
                            gotoxy ( 5, i + 15 );
                        if ( M [ i ] [ j ] [ l ] == 44 )
                            gotoxy ( 5, i + 16 );
                        if ( M [ i ] [ j ] [ l ] == 48 )
                            gotoxy ( 5, c - 5 );
                        if ( M [ i ] [ j ] [ l ] == 52 )
                            gotoxy ( 5, i + 18 );
                        if ( M [ i ] [ j ] [ l ] == 56 )
                            gotoxy ( 5, i + 19 );
                        if ( M [ i ] [ j ] [ l ] == 60 )
                            gotoxy ( 5, i + 20 );
                   }
              }
         }
         textcolor ( LIGHTBLUE );
         gotoxy ( 36, 8 );
         printf ( "Digite um Número " );
         printf ( "para pesquisar " );
         gotoxy ( 36, 9 );
         printf ( "ou [ -1 ] para sair " );
         textcolor ( LIGHTRED );
         gets ( num );
         fflush ( stdin );
         n = atoi ( num );
         if ( n == -1 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 17, 25 );
              printf ( "Por: " );
              textcolor ( LIGHTMAGENTA );
              printf ( "Samuel Lima" );
              textcolor ( BLACK );
              gotoxy ( 17, 26 );
              printf ( "sa_sp10@hotmail.com" );
              Sleep ( 1800 );
              textcolor ( LIGHTRED );
              gotoxy ( 36, 28 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         }
         if ( n < 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 36, 10 );
              printf ( "\aO único negativo válido é o " );
              textcolor ( LIGHTRED );
              printf ( "-1" );
              Sleep ( 1800 );
         }
         if ( n > 64 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 36, 12 );
              printf ( "\aO número " );
              textcolor ( LIGHTRED );
              printf ( "%d", n );
              textcolor ( LIGHTBLUE );
              printf ( " não existe na Matriz" );
              Sleep ( 1800 );
              system ( "cls" );
         }
         if ( Imp_Matr ( num ) == 0 || n == 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 36, 12 );
              printf ( "\aVocê digitou " );
              textcolor ( LIGHTRED );
              printf ( "%s", num );
              Sleep ( 1800 );
              textcolor ( LIGHTBLUE );
              gotoxy ( 36, 13 );
              printf ( "\aNão foi aceito" );
              Sleep ( 1800 );
         } else {
              textcolor ( LIGHTBLUE );
              gotoxy ( 18, 9 );
              for ( i = 0; i < tam; i++ ) {
                   for ( j = 0; j < tam; j++ ) {
                        for ( l = 0; l < tam; l++ ) {
                            if ( n == M [ i ] [ j ] [ l ] ) {
                                 textcolor ( LIGHTBLUE );
                                 textcolor ( LIGHTBLUE );
                                 gotoxy ( 36, 12 );
                                 printf ( "O valor " );
                                 textcolor ( LIGHTRED );
                                 printf ( "%d", n );
                                 textcolor ( LIGHTBLUE );
                                 printf ( " foi encontrado na tabela " );
                                 textcolor ( LIGHTRED );
                                 printf ( "%d", i );
                                 textcolor ( LIGHTBLUE );
                                 gotoxy ( 36, 13 );
                                 printf ( "e linha " );
                                 textcolor ( LIGHTRED );
                                 printf ( "%d ", j );
                                 textcolor ( LIGHTBLUE );
                                 printf ( " e coluna " );
                                 textcolor ( LIGHTRED );
                                 printf ( "%d ", l );
                                 Sleep ( 1800 );
                                 textcolor ( LIGHTRED );
                                 gotoxy ( 36, 23 );
                                 printf ( "PRESSIONE QUALQUER TECLA" );
                                 getche ( );
                            }
                        }
                   }
              }
         }
     } while ( 1 );
     return 0;
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.