domingo, 2 de março de 2014

Convertendo um Vetor em Matriz

É muito comum, encontrarmos códigos onde se converte
uma matriz bidimensional em vetor unidimensional.
Más, neste código, faço o contrário, mostro como converter um
Vetor unidimensional em Matriz bidimensional.


Algumas imagens do programa em execução:

                              
Veja este excelente código abaixo:


#include <stdio.h>
#include <conio.h>
#define tam 100
void Moldura ( int a, int c, int b, int d, int e, int f ) {
     for ( a = e; a < c; a++ ) {
         for ( b = f; b < d; b++ ) {
              gotoxy ( b, a );
              textbackground ( WHITE );
              textcolor ( BLACK );
              printf ( " " );
         }
     }
}
int main ( ) {
     system ( "title CONVERTENDO UM VETOR EM MATRIZ" );
     Moldura ( 2, 25, 2, 79, 2, 3 );
     int Vet [ tam ];
     int a = 0, b = 0, v, i, j, M [ 10 ] [ 10 ];
     textcolor ( LIGHTRED );
     gotoxy ( 29, 3 );
     printf ( "CONVERTENDO UM VETOR EM MATRIZ" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 30, 5 );
     printf ( "IMPRIMINDO ABAIXO O CONTADOR " );
     textcolor ( BLUE );
     gotoxy ( 17, 7 );
     for ( i = 1; i <= tam; i++ ) {
         if ( i == 11 ) {
              gotoxy ( 17, 8 );
         }
         if ( i == 21 ) {
              gotoxy ( 17, 9 );
         }
         if ( i == 31 ) {
              gotoxy ( 17, 10 );
         }
         if ( i == 41 ) {
              gotoxy ( 17, 11 );
         }
         if ( i == 51 ) {
              gotoxy ( 17, 12 );
         }
         if ( i == 61 ) {
              gotoxy ( 17, 13 );
         }
         if ( i == 71 ) {
              gotoxy ( 17, 14 );
         }
         if ( i == 81 ) {
              gotoxy ( 17, 15 );
         }
         if ( i == 91 ) {
              gotoxy ( 17, 16 );
         }
         printf ( "%5d", i );
         Vet [ i ] = i;
     }
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 30, 23 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     system ( "cls" );
     system ( "Color A9" );
     Moldura ( 2, 25, 2, 79, 2, 3 );
     textcolor ( LIGHTRED );
     gotoxy ( 28, 3 );
     printf ( "CONVERTENDO UM VETOR EM MATRIZ" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 31, 5 );
     printf ( "IMPRIMINDO ABAIXO O VETOR" );
     gotoxy ( 17, 7 );
     textcolor ( BLACK );
     for ( i = 1; i <= tam; i++ ) {
         if ( i == 11 ) {
              gotoxy ( 17, 8 );
         }
         if ( i == 21 ) {
              gotoxy ( 17, 9 );
         }
         if ( i == 31 ) {
              gotoxy ( 17, 10 );
         }
         if ( i == 41 ) {
              gotoxy ( 17, 11 );
         }
         if ( i == 51 ) {
              gotoxy ( 17, 12 );
         }
         if ( i == 61 ) {
              gotoxy ( 17, 13 );
         }
         if ( i == 71 ) {
              gotoxy ( 17, 14 );
         }
         if ( i == 81 ) {
              gotoxy ( 17, 15 );
         }
         if ( i == 91 ) {
              gotoxy ( 17, 16 );
         }
         printf ( "%5d", Vet [ i ] );
     }
     for ( i = 1; i <= tam; i++ ) {
         M [ a ] [ b ] = Vet [ i ];
         b++;
     }
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 30, 23 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     system ( "cls" );
     do {
         system ( "Color B5" );
         Moldura ( 2, 25, 2, 79, 2, 3 );
         textcolor ( LIGHTRED );
         gotoxy ( 29, 3 );
         printf ( "CONVERTENDO UM VETOR EM MATRIZ" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 32, 5 );
         printf ( "IMPRIMINDO AGORA A MATRIZ" );
         textcolor ( BROWN );
         gotoxy ( 2, 7 );
         for ( a = 0; a < 10; a++ ) {
              gotoxy ( 18, a + 8 );
              for ( b = 0; b < 10; b++ ) {
                   printf ( "%5d", M [ a ] [ b ] );
              }
         }
         textcolor ( LIGHTBLUE );
         gotoxy ( 20, 19 );
         printf ( "Digite um número entre 1 e 100 ou [ 0 ] Para sair ");
         textcolor ( LIGHTRED );
         scanf ( "%d", &v );
         for ( i = 0; i < 10; i++ ) {
              for ( j = 0; j < 10; j++ ) {
                   if ( v == M [ i ] [ j ] ) {
                        textcolor ( LIGHTGREEN );
                        gotoxy ( 26, 21 );
                        printf ( "O valor %d foi encontrado ", v );
                        gotoxy ( 26, 22 );
                        printf ( "Na linha %d e coluna %d ", i, j );
                   }
              }
              if ( v == 0 ) {
                   Sleep ( 1800 );
                   textcolor ( LIGHTBLUE );
                   gotoxy ( 20, 21 );
                   printf ( "Por: " );
                   textcolor ( LIGHTMAGENTA );
                   printf ( "Samuel Lima" );
                   textcolor ( BLACK );
                   gotoxy ( 20, 22 );
                   printf ( "sa_sp10@hotmail.com" );
                   Sleep ( 1800 );
                   textcolor ( LIGHTRED );
                   gotoxy ( 36, 24 );
                   printf ( "MUITO OBRIGADO" );
                   getche ( );
                   exit ( 0 );
              }
         }
         if ( v <= 100 ) {
              printf ( "\n" );
         } else {
              textcolor ( LIGHTGREEN );
              gotoxy ( 29, 22 );
              printf ( "A matriz nao possui o valor %d\n", v );
         }
         getche ( );
         system ( "cls" );
     } while ( 1 );
}

Nenhum comentário:

Postar um comentário

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