domingo, 19 de abril de 2015

Elementos comuns entre dois vetores

Comparar dois vetores de inteiros e procurar por elementos repetidos
e numerar o total de ocorrências, é a finalidade deste programa,
 que pode ser alterado, mudando os vetores pré - definidos
para vetores com entradade dados com scanf();
O código é indicado a iniciantes em C, que com poucas modificações
pode ser adaptado, foi criado no eclipse onde rodou
sem nenhum warnings.

Veja abaixo uma imagem do programa em execução:



Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
void Janela5 ( ) {
     int lin, col;
     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 );
}
int Vet_Num ( int Num [ 10 ], int di [ 10 ] ) {
     int i, q, h = 0, k, Vet [ 10 ];
     int j, vezes;
     for ( i = 0; i < 10; i++ ) {
         for ( q = 0; q < 10; q++ ) {
              if ( Num [ i ] == di [ q ] ) {
                   Vet [ h ] = di [ q ];
                   h++;
              }
         }
     }
     k = h;
     textcolor ( LIGHTBLUE );
     gotoxy ( 26, 13 );
     printf ( "Números comuns nos dois vetores" );
     textcolor ( WHITE );
     gotoxy ( 26, 15 );
     for ( h = 0; h < k; h++ ) {
         printf ( " %i ", Vet [ h ] );
     }
     getche ( );
     for ( i = 0; i < k; i++ ) {
         vezes = 1;
         j = i + 1;
         while ( j < k )
              if ( Vet [ j ] != Vet [ i ] )
                   j++;
              else {
                   vezes++;
                   k--;
                   Vet [ j ] = Vet [ k ];
              }
         if ( vezes > 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 20, i + 17 );
              printf ( "O Número " );
              textcolor ( LIGHTRED );
              printf ( "%d", Vet [ i ] );
              textcolor ( LIGHTBLUE );
              printf ( " é comum e ocorre" );
              textcolor ( LIGHTRED );
              printf ( " %d ", vezes );
              textcolor ( LIGHTBLUE );
              printf ( "vezes no vetor B" );
         }
     }
     return 0;
}
int main ( ) {
     system ( "title ELEMENTOS COMUNS ENTRE DOIS VETORES" );
     int i, nr;
     Janela5 ( );
     int A [ 10 ] = { 2, 7, 3, 5, 1, 9, 4, 8, 0, 6 };
     int B [ 10 ] = { 9, 7, 9, 7, 5, 9, 5, 9, 6, 7 };
     textcolor ( LIGHTRED );
     gotoxy ( 26, 3 );
     printf ( "ELEMENTOS COMUNS ENTRE DOIS VETORES" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 26, 5 );
     printf ( "Imprimindo abaixo o vetor A " );
     textcolor ( WHITE );
     gotoxy ( 26, 7 );
     for ( i = 0; i < 10; i++ ) {
         printf ( " %d ", A [ i ] );
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 26, 9 );
     printf ( "Imprimindo abaixo o vetor B " );
     textcolor ( WHITE );
     gotoxy ( 26, 11 );
     for ( i = 0; i < 10; i++ ) {
         printf ( " %d ", B [ i ] );
     }
     nr = Vet_Num ( B, A );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 36, 23 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     exit ( 0 );
     getche ( );
     return ( 0 );
}

Nenhum comentário:

Postar um comentário

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