quinta-feira, 29 de novembro de 2012

Ordenando e mostrando números repetidos num vetor

Se usuário houver digitado números iguais ele ordena e agradece o uso
más se o usuário digitar números repetidos, este programa está ápto a mostrar os repetidos.

 FUNCIONAMENTO:

Os números digitados são armazenados no vetor VET, e é enviado a um bloco
de ordenação, depois de ordenado, vem o bloco mais importante do código
onde os valores são testados; na análise a variável ord++ incrementa
se os valores forem iguais, más se forem diferentes o nosso else entra
na briga e aciona os blocos de ifs onde a variável ord é sempre maior que 1,
Por fim estes valores muito bem ordenados previamente,
agora são atualizados e entregues com a finalidade para a qual foi programado.

Copie, cole e compile:


#include <stdio.h>
#include <conio.h>
int VET [ 8 ];
void repetido ( ) {
     int i, ord = 0, aux;
     system ( "title ORDENANDO E MOSTRANDO NUMEROS REPETIDOS NUM VETOR" );
     for ( i = 0; i < 8 ; i++ ) {
         system ( "cls" );
         textcolor ( LIGHTRED );
         gotoxy ( 17 , 3 );
         printf ( "ORDENANDO E MOSTRANDO NUMEROS REPETIDOS NUM VETOR" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 25 , 5 );
         printf ( "DIGITE UM NUMERO PARA A POSICAO:  " );
         textcolor ( LIGHTRED );
         gotoxy ( 57 , 5 );
         printf ( " %d  " , i );
         textcolor ( YELLOW );
         scanf ( "%d" , &VET [ i ] );
         fflush ( stdin );
     }
     textcolor ( LIGHTGREEN );
     gotoxy ( 25 , 7 );
     printf ( "MOSTRANDO OS NUMEROS DIGITADOS DESORDENADOS" );
     textcolor ( YELLOW );
     gotoxy ( 25 , 9 );
     for ( i = 0; i < 8 ; i++ )
         printf ( "%d " , VET [ i ] );
     Sleep ( 800 );
     textcolor ( LIGHTGREEN );
     gotoxy ( 25 , 11 );
     printf ( "MOSTRANDO EM ORDEM OS NUMEROS DIGITADOS " );
     ord = 0;
     while ( ord == 0 ) {
         ord = 1;
         for ( i = 0; i < 7 ; i++ ) {
              if ( VET [ i ] > VET [ ( i + 1 ) ] ) {
                   aux = VET [ i ];
                   VET [ i ] = VET [ ( i + 1 ) ];
                   VET [ ( i + 1 ) ] = aux;
                   ord = 0;
              }
         }
     }
     textcolor ( YELLOW );
     gotoxy ( 25 , 13 );
     for ( i = 0; i < 8 ; i++ )
         printf ( "%d " , VET [ i ] );
     Sleep ( 800 );
     aux = VET [ 0 ];
     for ( i = 1; i < 9 ; i++ )
         if ( VET [ i ] == aux )
              ord++;
         else {
              if ( ord > 1 ) {
                   textcolor ( LIGHTBLUE );
                   printf ( "\n\t\tO VALOR %i ESTA REPETIDO %i VEZES" , aux ,
                            ord );
                   Sleep ( 800 );
                   ord = 1;
              }
              aux = VET [ i ];
         }
     textcolor ( LIGHTRED );
     gotoxy ( 35 , 21 );
     printf ( "\aO ROCCO AGRADECE" );
     getche ( );
     exit ( 0 );
}
int main ( ) {
     repetido ( );
}

Nenhum comentário:

Postar um comentário

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