sábado, 22 de agosto de 2015

Aleatórios de 1 a 100 sem repetições

Um ato útil e muitas vêzes necessário é poder gerar números aleatórios, em linguagem C e isto é perfeitamente possível.
Quando queremos uma cadeia de números assim, usamos a função rand(); da biblioteca stdlib.h.
Um valor aleatório entre 0 e a constante RAND_MAX, é apresentado na chamada desta função.
Más outra função se faz necessário quando queremos criar valores aleatórios, porque é desagradável criar sempre a mesma sequência aleatória não é mesmo?
Para fazer com que a sequência não seja a mesma a cada execução do programa, usamos a função srand(); que inicia a função rand();
com um valor denominado "semente", esta função recebe um argumento
do tipo inteiro sem sinal, também chamado unsigned int.
Más programadores iniciantes ainda tem outra frustação relacionada a geração de números aleatórios sequênciais, o que ocorre é que quando uma sequência é um tanto extensa, tende a repetir alguns números na mesma sequência.
Más o problema acaba seguindo o método deste código, portanto,aproveite este exemplo que é uma ajuda muito boa aos iniciantes em Linguagem C.

Veja abaixo imagens do programa em execução:




Veja abaixo o código do programa:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define tam 100
void Janela5 ( ) {
     int lin, col;
     col = 0;
     system ( "Color 10" );
     for ( lin = 2; lin <= 24 ; lin++ )
         for ( col = 2; col <= 78 ; col++ ) {
              gotoxy ( col , lin );
              textbackground ( WHITE );
              printf ( " " );
         }
}
int continuando ( ) {
     int nr;
     do {
         system ( "cls" );
         system ( "Color 90" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 26 , 5 );
         printf ( "ALEATÓRIOS DE 1 A 100 SEM REPETIÇÕES" );
         textcolor ( BROWN );
         gotoxy ( 25 , 7 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( GREEN );
         gotoxy ( 52 , 7 );
         printf ( "Samuel Lima" );
         textcolor ( BLACK );
         gotoxy ( 34 , 9 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 23 , 11 );
         printf ( "Digite " );
         textcolor ( LIGHTRED );
         printf ( " 0 " );
         textcolor ( LIGHTBLUE );
         printf ( " para sair ou " );
         textcolor ( LIGHTRED );
         printf ( " 1 " );
         textcolor ( LIGHTBLUE );
         printf ( " para continuar " );
         textcolor ( LIGHTRED );
         gotoxy ( 24 , 13 );
         scanf ( "%d" , &nr );
         fflush ( stdin );
         if ( nr == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 36 , 18 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( BLACK );
         gotoxy ( 36 , 16 );
         printf ( "\aopcão errada!" );
         getche ( );
     } while ( 1 );
     return 1;
}
int Ordena_Num_Ger ( int *A ) {
     system ( "cls" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 26 , 3 );
     printf ( "ALEATÓRIOS DE 1 A 100 SEM REPETIÇÕES" );
     int i, j, temp;
     for ( i = 0; i < 100 - 1 ; i++ ) {
         for ( j = 0; j < 100 - 1 ; j++ ) {
              if ( A [ j ] > A [ j + 1 ] ) {
                   temp = A [ j ];
                   A [ j ] = A [ j + 1 ];
                   A [ j + 1 ] = temp;
              }
         }
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 24 , 5 );
     printf ( "Abaixo os números gerados bem ordenados" );
     textcolor ( BLACK );
     gotoxy ( 17 , 7 );
     for ( i = 0; i < 100 ; i++ ) {
         if ( i == 10 ) {
              gotoxy ( 17 , 8 );
         }
         if ( i == 20 ) {
              gotoxy ( 17 , 9 );
         }
         if ( i == 30 ) {
              gotoxy ( 17 , 10 );
         }
         if ( i == 40 ) {
              gotoxy ( 17 , 11 );
         }
         if ( i == 50 ) {
              gotoxy ( 17 , 12 );
         }
         if ( i == 60 ) {
              gotoxy ( 17 , 13 );
         }
         if ( i == 70 ) {
              gotoxy ( 17 , 14 );
         }
         if ( i == 80 ) {
              gotoxy ( 17 , 15 );
         }
         if ( i == 90 ) {
              gotoxy ( 17 , 16 );
         }
         printf ( " %3d " , A [ i ] );
     }
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 36 , 23 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     return ( 0 );
}
int Gera_Num_Sem_Repetidos ( int *A ) {
     int aut, i, j, t = 100;
     int result = 0;
     continuando ( );
     system ( "cls" );
     Janela5 ( );
     srand ( time ( NULL ) );
     for ( i = 0; i < 100 ; i++ ) {
         do {
              do {
                   t = rand ( ) % 100;
                   if ( t == 0 ) {
                        t = 100;
                   }
                   break;
              } while ( 1 );
              aut = 0;
              for ( j = 0; j < 100 ; j++ )
                   if ( t == A [ j ] ) //Comente esta linha
                        aut = 1; //Comente esta linha
              if ( aut == 0 ) {
                   A [ i ] = t;
              }
         } while ( aut == 1 );
     }
     textcolor ( LIGHTRED );
     gotoxy ( 26 , 3 );
     printf ( "ALEATÓRIOS DE 1 A 100 SEM REPETIÇÕES" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 21 , 5 );
     printf ( "Abaixo os 100 números gerados sem repetições\n" );
     textcolor ( BLACK );
     gotoxy ( 17 , 7 );
     for ( i = 0; i < 100 ; i++ ) {
         if ( i == 10 ) {
              gotoxy ( 17 , 8 );
         }
         if ( i == 20 ) {
              gotoxy ( 17 , 9 );
         }
         if ( i == 30 ) {
              gotoxy ( 17 , 10 );
         }
         if ( i == 40 ) {
              gotoxy ( 17 , 11 );
         }
         if ( i == 50 ) {
              gotoxy ( 17 , 12 );
         }
         if ( i == 60 ) {
              gotoxy ( 17 , 13 );
         }
         if ( i == 70 ) {
              gotoxy ( 17 , 14 );
         }
         if ( i == 80 ) {
              gotoxy ( 17 , 15 );
         }
         if ( i == 90 ) {
              gotoxy ( 17 , 16 );
         }
         printf ( " %3d " , A [ i ] );
         result += A [ i ];
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 18 );
     Sleep ( 500 );
     printf ( "Total de números gerados ==> %d" , i );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 19 );
     Sleep ( 500 );
     printf ( "Soma dos números gerados ==> %d" , result );
     if ( result == 5050 ) {
         return 1;
     }
     if ( result > 5050 || result < 5050 ) {
         return 2;
     }
     getche ( );
     return ( 0 );
}
int Repete_num ( int *A ) {
     int i, j, x, y = 0, vezes;
     int VET [ 100 ];
     for ( i = 0; i < 100 ; i++ ) {
         VET [ y ] = A [ i ];
         y++;
     }
     for ( x = 2; x < y ; x++ ) {
         vezes = 1;
         j = x + 1;
         while ( j < y )
              if ( VET [ j ] != VET [ x ] )
                   j++;
              else {
                   vezes++;
                   y--;
                   VET [ j ] = VET [ y ];
              }
         if ( vezes >= 2 ) {
              textcolor ( LIGHTBLUE );
              printf ( "\n O número " );
              textcolor ( LIGHTRED );
              printf ( " %d " , VET [ x ] );
              textcolor ( LIGHTBLUE );
              printf ( " ocorre " );
              textcolor ( LIGHTRED );
              printf ( " %d " , vezes );
              textcolor ( LIGHTBLUE );
              printf ( " vêzes " );
         }
     }
     getche ( );
     return ( 0 );
}
void tecla ( ) {
     textcolor ( BLACK );
     gotoxy ( 31 , 23 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
}
int main ( ) {
     int *A;
     int vezes = 0;
     A = ( int* ) malloc ( tam * sizeof(int) );
     system ( "title ALEATÓRIOS DE 1 A 100 SEM REPETIÇÕES" );
     vezes = Gera_Num_Sem_Repetidos ( A );
     if ( vezes == 1 ) {
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 20 );
         printf ( "Não houve repetidos" );
         tecla ( );
     }
     if ( vezes == 2 ) {
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 20 );
         printf ( "Muitos números se repetiram" );
         tecla ( );
     }
     Repete_num ( A );
     Ordena_Num_Ger ( A );
     free ( A );
     return ( 0 );
}

Nenhum comentário:

Postar um comentário

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