terça-feira, 4 de dezembro de 2012

Ordenando Colunas de Matriz Bidimensional

Ordenar uma Matriz bidimensional por colunas sem converte-la primeiro num vetor simples não é tarefa muito fácil, e neste programa eu mostro um dos métodos de fazer isto. A matriz é do tipo float e a ordenação é pelo método da bolha, sabemos que tem outros métodos mais este é um dos mais fácil de imprementar.
O código é muito fácil de entender, como todos os outros que já postei, porque são indicados a iniciantes em linguagem c.

Veja abaixo uma imagem do programa em execução:


Veja abaixo o código do programa:

#include <conio2.h>
#include <stdio.h>

void Janela5 ( ) {
	int lin, col;
	system ( "color 5F" );
	for ( lin = 0; lin <= 36; lin++ ) {
		for ( col = 0; col <= 80; col++ ) {
			gotoxy ( col, lin );
			if ( lin == 2 ) {
				textbackground ( BLACK );
				printf ( " " );
			}
			if ( col == 1 ) {
				textbackground ( BLACK );
				printf ( " " );
			}
			if ( lin == 36 ) {
				textbackground ( BLACK );
			}
			if ( col == 80 ) {
				textbackground ( BLACK );
				printf ( " " );
			}
		}
	}
	textbackground ( BLACK );
}
int main ( ) {
	Janela5 ( );
	system ( "title ORDENANDO COLUNAS DE UMA MATRIZ BIDIMENSIONAL " );
	int i, j;
	float 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 } };
	textcolor ( LIGHTRED );
	gotoxy ( 19, 3 );
	printf ( "ORDENANDO COLUNAS DE UMA MATRIZ BIDIMENSIONAL" );
	textcolor ( LIGHTBLUE );
	gotoxy ( 7, 5 );
	printf ( "Veja abaixo a Matriz original" );
	textcolor ( LIGHTGRAY );
	for ( i = 0; i < 8; i++ ) { // Faz a leitura das linhas
		gotoxy ( 13, i + 7 );
		for ( j = 0; j < 3; j++ ) { // Faz a leitura das colunas
			printf ( " %.1f ", mat_riz [ i ] [ j ] ); // Imprime linhas e colunas
		}
	}
	Sleep ( 1800 );
	while ( ord == 0 ) {
		ord = 1;
		for ( i = 0; i < 7; i++ ) {
			for ( j = 0; j < 3; j++ ) {
				if ( mat_riz [ i ] [ j ] > mat_riz [ ( i + 1 ) ] [ j ] ) {
					aux = mat_riz [ i ] [ j ];
					mat_riz [ i ] [ j ] = mat_riz [ ( i + 1 ) ] [ j ];
					mat_riz [ ( i + 1 ) ] [ j ] = aux;
					ord = 0;
				}
			}
		}
	}
	textcolor ( LIGHTGREEN );
	gotoxy ( 45, 5 );
	printf ( "Abaixo ordenada por colunas" );
	printf ( "\n" );
	textcolor ( YELLOW );
	for ( i = 0; i < 8; i++ ) {
		gotoxy ( 51, i + 7 );
		for ( j = 0; j < 3; j++ ) {
			printf ( " %.1f ", mat_riz [ i ] [ j ] );
		}
	}
	Sleep ( 1800 );
	textcolor ( YELLOW );
	gotoxy ( 25, 17 );
	printf ( "Programa desenvolvido por:" );
	textcolor ( LIGHTCYAN );
	gotoxy ( 52, 17 );
	printf ( "Samuel Lima" );
	textcolor ( LIGHTGREEN );
	gotoxy ( 34, 19 );
	printf ( "sa_sp10@hotmail.com" );
	Sleep ( 1800 );
	textcolor ( LIGHTRED );
	gotoxy ( 20, 23 );
	printf ( "::::::::::::::  MUITO OBRIGADO  ::::::::::::::" );
	getche ( );
	exit ( 0 );

}

Nenhum comentário:

Postar um comentário

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