domingo, 27 de abril de 2014

Maior e Menor em Matriz Ordenada e Desordenada II


A Matriz de inteiros M [ tam ] [ tam ], de Números naturais contando de 1 até 100,
é pré - definida, e neste código, é declarada como global, junto com as váriáveis inteiras,
exceto a variável g e m que foram passadas como parâmetros na função:

void maior_menor_Numero(int g, int m);

E assim temos a primeira impressão destes Números na tela do dos. Em seguida o programa já mostra o maior e o menor elemento contido na Matriz, mostrando sua posição correta, linha X coluna.
Porém, pra não deixar dúvidas sobre o funcionamento do programa, resolvi colocar um bloco de códigos para embaralhar estes Números que foram imprimidos ordenados, e tudo ocorre em perfeita ordem, a Matriz é Embaralhada e imprimida assim,
toda desordenada, más o programa é capaz de mostrar a posição linha X coluna do maior e do menor elemento contido na Matriz.
Código perfeito para iniciantes em Linguagem C, que não sabem como utilizar estes recursos da linguagem.

Veja algumas imagens do programa em execução:




Veja o código do programa abaixo:

#include <stdio.h>
#include <conio.h>
#define tam 10
int f = 0, h = 0, i, j, r, s, y, temp;
int M [ 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, 65, 66, 67, 68, 69, 70 },
{ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80 },
{ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 },
{ 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 }};
void Janela5(){
int lin, col;
col = 0;
for ( lin = 0; lin <= 25; lin++ ){
for ( col = 0; col <= 80; col++ ){
gotoxy( col, lin );
if ( lin == 2 ){textbackground(LIGHTBLUE);printf( " ");}
if ( col == 1 ){textbackground(LIGHTBLUE);printf(" ");}
if ( lin == 25 ){textbackground(LIGHTBLUE);}
if ( col == 80 ){textbackground(LIGHTBLUE);printf(" ");}
}
}textbackground( BLACK);
}
void maior_menor_Numero(int g, int m){
for ( f = 0; f < tam ; f++ ){
for ( h = 0; h < tam ; h++ ){
if ( M [ f ] [ h ] > m ){
m = M [ f ] [ h ];
textcolor(LIGHTBLUE);gotoxy(8,20);printf("Esta Na linha   e coluna ") ;
textcolor(LIGHTRED);gotoxy(22,20);printf("%d", f);
textcolor(LIGHTRED);gotoxy(34,20);printf("%d", h);
}
if ( M [ f ] [ h ] < g ){
g = M [ f ] [ h ];
textcolor(LIGHTBLUE);gotoxy(8,22);printf("Esta Na linha   e coluna ") ;
textcolor(LIGHTRED);gotoxy(22,22);printf("%d", f);
textcolor(LIGHTRED);gotoxy(34,22);printf("%d", h);
}
}
}
textcolor(LIGHTBLUE);gotoxy(8,19);printf("O Maior Elemento da Matriz é: ");
textcolor(LIGHTRED);gotoxy(38,19);printf("%d", m);
textcolor(LIGHTBLUE);gotoxy(8,21);printf("O Menor Elemento da Matriz e: ");
textcolor(LIGHTRED);gotoxy(38,21);printf("%d", g );
textcolor(LIGHTGRAY);gotoxy(33,23);printf("PRESSIONE QUALQUER TECLA");
}
int main (){
system ("title MAIOR E MENOR ELEMENTO EM MATRIZ ORDENADA E DESORDENADA");
Janela5();gotoxy(27,3);
textcolor(LIGHTRED);gotoxy(15,3);printf("MAIOR E MENOR ELEMENTO EM MATRIZ ORDENADA E DESORDENADA");
textcolor(LIGHTBLUE);gotoxy(30,5);printf("IMPRIMINDO A MATRIZ ORIGINAL");
textcolor(YELLOW);
for( i = 0; i < tam; i++ ){
gotoxy(18, i + 9);
for( j = 0; j < tam; j++ ){
printf(" %3d ",  M [ i ] [ j ] );
}
}
getche();
textcolor(LIGHTBLUE);gotoxy(26,5);printf("A Matriz foi Imprimida com sucesso");
textcolor(LIGHTGRAY);gotoxy(18,7);printf("Para sair Digite [ 0 ] Para Continuar Digite [ 1 ]  ");
textcolor(LIGHTRED);scanf("%d", &s);
if( s == 0 ){
textcolor(LIGHTRED);gotoxy(20,23);printf("::::::::::::::  MUITO OBRIGADO  ::::::::::::::" );
getche();exit(0);
}
maior_menor_Numero( i, j );
getche();system("cls");do{ Janela5();
for ( i = 0; i < tam; i++){
for ( j = 0; j < tam; j++){
r = rand() % tam;
y = rand() % tam;
temp = M [ i ] [ j ];
M [ i ] [ j ] = M [ r ] [ y ];
M [ r ] [ y ] = temp;
}
}
textcolor(LIGHTRED);gotoxy(15,3);printf("MAIOR E MENOR ELEMENTO EM MATRIZ ORDENADA E DESORDENADA");
textcolor(LIGHTBLUE);gotoxy(29,5);printf("Embaralhando Agora a Matriz ...");
textcolor(LIGHTGREEN);int  f = 0, h = 0;
for( r = 0; r  < tam ; r++ ){
gotoxy(18, r + 9);
for( y = 0; y  < tam; y++ ){
M [ f ] [ h ] = M [ r  ] [ y ];
Sleep(100);printf(" %3d ",  M [ r  ] [ y ] );
}
}
textcolor(LIGHTBLUE);gotoxy(26,5);printf("A Matriz foi Embaralhada com sucesso");
textcolor(LIGHTGRAY);gotoxy(18,7);printf("Para sair Digite [ 0 ] Para Continuar Digite [ 1 ]  ");
textcolor(LIGHTRED);scanf("%d", &s);
if( s == 0 ){
textcolor(LIGHTRED);gotoxy(20,23);printf("::::::::::::::  MUITO OBRIGADO  ::::::::::::::" );
getche();exit(0);
}
maior_menor_Numero( r, y );
getche();system("cls");
}while(1);
}

Nenhum comentário:

Postar um comentário

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