domingo, 3 de fevereiro de 2013

Gerando Números Aleatórios em Matriz Dinâmica


Quando não sabemos a quantidade de memória a alocar, É conveniente o uso de
Alocação dinâmica de memória, que é referida as funções malloc e free, ambas
da biblioteca stdlib.h.
Esta é a abreviatura da função malloc:  ( memory allocation ),Más informações
sobre estas funções e seus usos estão disponibilizados amplamente na internet.

A função GerNum_Aleatorios(); Contém todas as informações do código, na verdade
declarei a funçao rand(); "srand ( time ( NULL ) );", também da biblioteca stdlib.h, declarei a matriz dinâmica **matriz; e um vetor estático, int VET [100];
Os números gerados são armazenados na matriz dinâmica, más são passados ao vetor
estático VET[]; Na primeira impressão de printf, temos os números gerados apresentados na matriz, e a segunda impressão já mostra os mesmos números
armazenados no vetor estático VET[];
Já no próximo bloco os números gerados são ordenados pelo método da bolha, e são imprimidos em ordem más com todos os números gerados.
Agora, os mesmos números gerados passa num bloco onde se conta o número de vêzes
que estes números estão repetidos no vetor VET[]; e então se exclui os repetidos
e a impressão acontece mostrando o total de ocorrências dos números e em seguida
a impressão dos números sem os repetidos.


Eis a saída deste Programa no cmd:






#include <stdio.h>
#include <conio2.h>
#include <stdlib.h>
#include <time.h>
#define tam 10
void GerNum_Aleatorios ( ) {
     int i, j, x, y = 0, ord, aux, vezes;
     srand ( time ( NULL ) );
     int VET [ 100 ];
     int **matriz;
     system ( "cls" );
     textcolor ( LIGHTRED );
     gotoxy ( 21 , 2 );
     printf ( "GERANDO NÚMEROS ALEATORIOS EM MATRIZ DINAMICA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 30 , 4 );
     printf ( "ESTES SÃO OS NÚMEROS GERADOS" );
     textcolor ( YELLOW );
     gotoxy ( 10 , 5 );
     matriz = ( int** ) malloc ( tam * sizeof(int) );
     for ( i = 0; i < tam ; i++ ) {
         matriz [ i ] = ( int* ) malloc ( tam * sizeof(int*) ); //numero de linhas
         printf ( "\n\t\t" );
         for ( j = 0; j < tam ; j++ ) {
              matriz [ i ] [ j ] = rand ( ) % 10;
              Sleep ( 50 );
              VET [ y ] = matriz [ i ] [ j ];
              y++;
              printf ( " %3d " , matriz [ i ] [ j ] );
         }
         printf ( "\n" );
     }
     Sleep ( 800 );
     system ( "cls" );
     textcolor ( LIGHTRED );
     gotoxy ( 21 , 2 );
     printf ( "GERANDO NUMEROS ALEATORIOS EM MATRIZ DINAMICA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 30 , 4 );
     printf ( "ESTES são OS NUMEROS GERADOS" );
     textcolor ( YELLOW );
     gotoxy ( 2 , 6 );
     for ( x = 0; x < y ; x++ ) {
         printf ( " %d " , VET [ x ] );
     }
     Sleep ( 800 );
     textcolor ( LIGHTRED );
     gotoxy ( 25 , 12 );
     printf ( "MOSTRANDO EM ORDEM OS NUMEROS GERADOS " );
     ord = 0;
     while ( ord == 0 ) {
         ord = 1;
         for ( x = 1; x <= y ; x++ ) {

              if ( VET [ x ] > VET [ ( x + 1 ) ] ) {
                   aux = VET [ x ];
                   VET [ x ] = VET [ ( x + 1 ) ];
                   VET [ ( x + 1 ) ] = aux;
                   ord = 0;
              }
         }
     }
     textcolor ( YELLOW );
     gotoxy ( 2 , 14 );
     for ( int x = 1 ; x < y ; x++ ) {
         printf ( " %d " , VET [ x ] );
     }
     textcolor ( LIGHTGRAY );
     gotoxy ( 32 , 23 );
     printf ( "PRESSIONE QUALQUER TECLA" );
     getche ( );
     system ( "cls" );
     textcolor ( LIGHTRED );
     gotoxy ( 21 , 3 );
     printf ( "GERANDO NUMEROS ALEATORIOS EM MATRIZ DINAMICA" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 34 , 5 );
     printf ( "sa_sp10@hotmail.com" );
     textcolor ( LIGHTGREEN );
     gotoxy ( 29 , 7 );
     printf ( "NUMEROS ORDENADOS E REPETIDOS" );
     textcolor ( YELLOW );
     gotoxy ( 3 , 9 );
     for ( x = 1; x < y ; x++ ) {
         printf ( " %d " , VET [ x ] );
     }
     Sleep ( 800 );
     printf ( "\n" );
     for ( x = 1; x < y ; x++ ) {
         vezes = 1;
         j = x + 1;
         while ( j < y )
              if ( VET [ j ] != VET [ x ] )
                   j++;
              else {
                   vezes++;
                   y--;
                   VET [ j ] = VET [ y ];
              }
         printf ( "\n\tO VALOR %d OCORRE %d VEZES" , VET [ x ] , vezes );
     }
     Sleep ( 800 );
     textcolor ( LIGHTGREEN );
     gotoxy ( 37 , 14 );
     printf ( "NUMEROS GERADOS SEM OS REPETIDOS" );
     textcolor ( YELLOW );
     gotoxy ( 38 , 16 );
     for ( x = 1; x < y ; x++ ) {
         printf ( " %d " , VET [ x ] );
     }
     textcolor ( LIGHTRED );
     gotoxy ( 45 , 18 );
     printf ( "\aO ROCCO AGRADECE" );
     getche ( );
     system ( "cls" );
}
void Inicio ( ) {
     char ch;
     system ( "cls" );
     do {
         textcolor ( LIGHTRED );
         gotoxy ( 21 , 8 );
         printf ( "GERANDO NUMEROS ALEATORIOS EM MATRIZ DINAMICA" );
         textcolor ( YELLOW );
         gotoxy ( 24 , 10 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTCYAN );
         gotoxy ( 51 , 10 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTGREEN );
         gotoxy ( 34 , 12 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 24 , 14 );
         printf ( "DIGITE    PARA SAIR OU   PARA CONTINUAR  " );
         textcolor ( LIGHTRED );
         gotoxy ( 32 , 14 );
         printf ( "S" );
         textcolor ( LIGHTRED );
         gotoxy ( 47 , 14 );
         printf ( "C" );
         gotoxy ( 35 , 16 );
         ch = getche ( );
         fflush ( stdin );
         switch ( ch ) {
              case 'S':
                   exit ( 0 );
                   break;
              case 'C':
                   GerNum_Aleatorios ( );
                   break;
              default:
                   textcolor ( LIGHTRED );
                   gotoxy ( 35 , 16 );
                   printf ( "\aOPCAO ERRADA" );
                   Sleep ( 1800 );
                   system ( "cls" );
                   break;
         }
     } while ( 1 );
}
int main ( ) {
     system ( "title GERANDO NUMEROS ALEATORIOS EM MATRIZ DINAMICA" );
     system ( "cls" );
     Inicio ( );

}

Nenhum comentário:

Postar um comentário

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