quarta-feira, 28 de novembro de 2012

Convertendo uma matriz bidimensional em vetor unidimensional I

 Talvez você já precisou imprimir em ordem uma Matriz Bidimensional do tipo float e
encontrou muitas dificuldades, más usando este método
torna-se muito fácil a ordenação. Primeiro a Matriz Bidimensional é transformada em um vetor simples,
veja que ele foi declarado previamente com 24 posições. Dois laços for, sendo um para linhas e outro para colunas,
fazem a varredura em loop, e então atribuimos a mat_triz [] [], ao vetor vet[]; e incrementamos a variável i,
onde junto com o vetor receberão os valores transformados. Agora printf imprime o nosso vetor  do tipo float
que recebeu a matriz: mat_triz [] [];
A ordenação mesmo é pelo método da bolha que é um dos métodos mais simples embora a eficiencia não seja das melhores em se tratando de ordenação em muitos itens.
Veja a saída deste programa no cmd:



Copie, cole e compile:



#include <stdio.h>
#include <conio2.h>
#include <stdlib.h>
int main ( ) {
     system (
              "title CONVERTENDO UMA MATRIZ BIDIMENSIONAL EM VETOR UNIDIMENSIONAL" );
     int i = 0, ord = 0;
     float aux;
     float mat_riz [ 8 ] [ 3 ] =
           {{ 10.2, 14.8, 11.4 },
              { 9.5, 15.3, 8.9 },
              {8.6, 14.3, 21.6 },
              { 9.7, 16.5, 19.2},
              { 6.1, 17.4, 27.5},
              { 7.7, 16.1, 3.4 },
              { 8.3, 17.1, 7.8 },
              { 7.3, 15.9, 33.8}};
     float vet [ 24 ];
     int g = 0, f;
     for ( f = 0; f < 8 ; f++ ) {
         for ( g = 0; g < 3 ; g++ ) {
              vet [ i ] = mat_riz [ f ] [ g ];
              i++;
         }
     }
     textcolor ( LIGHTRED );
     gotoxy ( 12 , 3 );
     printf ( "CONVERTENDO UMA MATRIZ BIDIMENSIONAL EM VETOR UNIDIMENSIONAL" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 32 , 5 );
     printf ( "sa_sp10@hotmail.com" );
     textcolor ( LIGHTGREEN );
     gotoxy ( 25 , 7 );
     printf ( "MATRIZ CONVERTIDA EM VETOR SIMPLES" );
     textcolor ( YELLOW );
     printf ( "\n\n" );
     for ( int i = 0 ; i < 24 ; i++ ) {
         printf ( "\t\t\t\tposicao:  %i  %.1f\n" , i , vet [ i ] );
         Sleep ( 100 ); // Agora temos um vetor do tipo float
     }
     Sleep ( 800 );
     system ( "cls" );
     textcolor ( LIGHTRED );
     gotoxy ( 12 , 3 );
     printf ( "CONVERTENDO UMA MATRIZ BIDIMENSIONAL EM VETOR UNIDIMENSIONAL" );
     textcolor ( LIGHTGREEN );
     gotoxy ( 30 , 5 );
     printf ( "ESTA E A MATRIZ ORIGINAL " );
     printf ( "\n" );
     textcolor ( LIGHTBLUE );
     for ( f = 0; f < 8 ; f++ ) { // Faz a leitura das linhas
         printf ( "\n" );
         gotoxy ( 33 , f + 7 );
         for ( g = 0; g < 3 ; g++ ) { // Faz a leitura das colunas
              printf ( " %.1f " , mat_riz [ f ] [ g ] ); //Sleep(400);// Imprime linhas e colunas
         }
     }
     textcolor ( LIGHTGREEN );
     gotoxy ( 25 , 17 );
     printf ( "IMPRIMINDO AGORA A MATRIZ ORDENADA" );
     textcolor ( YELLOW );
     ord = 0;
     while ( ord == 0 ) {
         ord = 1;
         for ( i = 0; i < 24 ; i++ ) {
              if ( vet [ i ] > vet [ ( i + 1 ) ] ) {
                   aux = vet [ i ];
                   vet [ i ] = vet [ ( i + 1 ) ];
                   vet [ ( i + 1 ) ] = aux;
                   ord = 0;
              }
         }
     }
     printf ( "\n" );
     for ( i = 0; i < 18 ; i++ ) {
         gotoxy ( 5 , i + 19 );
         for ( i = 0; i < 24 ; i++ )
              printf ( "  %.1f  " , vet [ i ] );
     }
     textcolor ( LIGHTRED );
     gotoxy ( 35 , 23 );
     printf ( "\aO ROCCO AGRADECE" );
     getche ( );
}

Nenhum comentário:

Postar um comentário

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