Lista de Exercicios
12. Escrever um programa que classifique em ordem crescente ou decrescente
uma série de N valores. Ao final o programa dever imprimir os valores
lidos e classificados.
- program P12;
- uses crt;
- type
- vetor = array [1..20] of real;
- var
- n : byte;
- orig : vetor;
- cres, decres : vetor;
- houve_troca : boolean;
- procedure tela;
- begin
- clrscr;
- highvideo;
- gotoxy(19,2);
- write('PROGRAMA FEITO POR KARINE E MARIA DA GLORIA');
- gotoxy(18,4);
- write ('ORDENA VALORES UTILIZANDO VARIÁVEIS INDEXADAS');
- lowvideo;
- end;
- procedure ler_quant;
- begin
- while not (n in [2..20]) do
- begin
- gotoxy(5,6);
- write('Quantos valores para ordenar (2 - 20)? ');
- readln(n);
- end;
- clrscr;
- end;
- procedure ler_valores;
- var
- begin
- gotoxy(30,1);
- highvideo;
- write('Entrada de Valores');
- lowvideo;
- for i := 1 to n do
- begin
- gotoxy(1,i+2);
- write ('Entre com o ',i,'§ valor : ');
- read(orig[i]);
- end;
- end;
- procedure ordena_crescente;
- var
- begin
- cres := orig;
- repeat
- houve_troca := false;
- for i:=1 to n-1 do
- if cres[i] > cres[i+1] then
- begin
- temp := cres[i];
- cres[i] := cres[i+1];
- cres[i+1] :=temp;
- houve_troca := true;
- end;
- until not houve_troca;
- end;
- procedure ordena_decrescente;
- var
- begin
- decres := orig;
- repeat
- houve_troca := false;
- for i:=1 to n-1 do
- if decres[i] < decres[i+1] then
- begin
- temp := decres[i];
- decres[i] := decres[i+1];
- decres[i+1] :=temp;
- houve_troca := true;
- end;
- until not houve_troca;
- end;
- procedure imp_valores;
- var
- begin
- clrscr;
- highvideo;
- gotoxy(10,1);
- write('Valores');
- gotoxy(9,2);
- write('Originais');
- gotoxy(33,1);
- write('Valores em');
- gotoxy(30,2);
- write('Ordem Crescente');
- gotoxy(55,1);
- write('Valores em');
- gotoxy(52,2);
- write('Ordem Decrescente');
- lowvideo;
- for i:=1 to n do
- begin
- gotoxy(6,i+3);
- writeln (orig[i]:10:2);
- gotoxy(30,i+3);
- writeln (cres[i]:10:2);
- gotoxy(52,i+3);
- writeln (decres[i]:10:2);
- end;
- end;
- {Programa Principal}
- begin
- tela;
- ler_quant;
- ler_valores;
- ordena_crescente;
- ordena_decrescente;
- imp_valores;
- repeat until keypressed;
- end.
Para retornar a tela principal clique aqui.