segunda-feira, 22 de setembro de 2014

Conversor de Vetor em Matriz

Alguns iniciantes em Linguagem C tem dificuldades em coverter
vetores em matriz, por isso criei este código para mostrar na
prática como se faz, acompanhe atentamente o exemplo proposto
que não traz nenhuma dificuldade em entender.

Veja duas imagens do programa em execução:


Veja abaixo o código do programa:

#include <stdio.h>

#include <conio2.h>

 

void Janela5 ( ) {

    int lin, col;

    for ( lin = 0; lin <= 36; lin++ ) {

          for ( col = 0; col <= 80; col++ ) {

                 gotoxy ( col, lin );

                 if ( lin == 2 ) {

                        textbackground ( LIGHTRED );

                        printf ( " " );

                 }

                 if ( col == 1 ) {

                        textbackground ( LIGHTRED );

                        printf ( " " );

                 }

                 if ( lin == 36 ) {

                        textbackground ( LIGHTRED );

                 }

                 if ( col == 80 ) {

                        textbackground ( LIGHTRED );

                        printf ( " " );

                 }

          }

    }

    textbackground ( BLACK );

}

 

void inicio ( ) {

    system ( "title CONVERSOR DE VETOR EM MATRIZ" );

    int i;

    do {

          Janela5 ( );

          textcolor ( LIGHTRED );

          gotoxy ( 30, 7 );

          printf ( "CONVERSOR DE VETOR EM MATRIZ" );

          textcolor ( YELLOW );

          gotoxy ( 25, 10 );

          printf ( "Programa desenvolvido por:" );

          textcolor ( LIGHTCYAN );

          gotoxy ( 52, 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 ( "1" );

          textcolor ( LIGHTRED );

          gotoxy ( 47, 14 );

          printf ( "2" );

          gotoxy ( 41, 16 );

          scanf ( "%d", &i );

          fflush ( stdin );

          if ( i == 1 ) {

                 textcolor ( LIGHTRED );

                 gotoxy ( 35, 20 );

                 printf ( "MUITO OBRIGADO" );

                 Sleep ( 1800 );

                 exit ( 0 );

          }

          if ( i == 2 ) {

                 system ( "cls" );

                 return;

          } else {

                 textcolor ( LIGHTRED );

                 gotoxy ( 37, 20 );

                 printf ( "\aOPÇÃO ERRADA" );

                 Sleep ( 1800 );

                 system ( "cls" );

          }

    } while ( i );

}

int main ( ) {

    inicio ( );

    Janela5 ( );

    int vet1 [ 5 ] = { 1, 2, 3, 4, 5 };

    int vet2 [ 5 ] = { 6, 7, 8, 9, 10 };

    int vet3 [ 5 ] = { 11, 12, 13, 14, 15 };

    int vet4 [ 5 ] = { 16, 17, 18, 19, 20 };

    int vet5 [ 5 ] = { 21, 22, 23, 24, 25 };

    int Matriz [ 5 ] [ 5 ];

    int a = 0, b = 0, i;

    textcolor ( LIGHTRED );

    gotoxy ( 30, 3 );

    printf ( "CONVERSOR DE VETOR EM MATRIZ" );

    textcolor ( LIGHTGREEN );

    gotoxy ( 24, 5 );

    printf ( "Veja abaixo a impressão dos 5 vetores " );

    textcolor ( LIGHTBLUE );

    gotoxy ( 20, 7 );

    printf ( "Vetor 1 ==> " );

    textcolor ( YELLOW );

    for ( i = 0; i < 5; i++ ) {

          printf ( " %d ", vet1 [ i ] );

          Matriz [ a ] [ b ] = vet1 [ i ];

          b++;

    }

    textcolor ( LIGHTBLUE );

    gotoxy ( 20, 8 );

    printf ( "Vetor 2 ==> " );

    textcolor ( YELLOW );

    for ( i = 0; i < 5; i++ ) {

          printf ( " %d ", vet2 [ i ] );

          Matriz [ a ] [ b ] = vet2 [ i ];

          b++;

    }

    textcolor ( LIGHTBLUE );

    gotoxy ( 20, 9 );

    printf ( "Vetor 3 ==> " );

    textcolor ( YELLOW );

    for ( i = 0; i < 5; i++ ) {

          printf ( " %d ", vet3 [ i ] );

          Matriz [ a ] [ b ] = vet3 [ i ];

          b++;

    }

    textcolor ( LIGHTBLUE );

    gotoxy ( 20, 10 );

    printf ( "Vetor 4 ==> " );

    textcolor ( YELLOW );

    for ( i = 0; i < 5; i++ ) {

          printf ( " %d ", vet4 [ i ] );

          Matriz [ a ] [ b ] = vet4 [ i ];

          b++;

    }

    textcolor ( LIGHTBLUE );

    gotoxy ( 20, 11 );

    printf ( "Vetor 5 ==> " );

    textcolor ( YELLOW );

    for ( i = 0; i < 5; i++ ) {

          printf ( " %d ", vet5 [ i ] );

          Matriz [ a ] [ b ] = vet5 [ i ];

          b++;

    }

    getche ( );

    textcolor ( LIGHTGREEN );

    gotoxy ( 20, 13 );

    printf ( "Veja abaixo os 5 vetores convertido em Matriz" );

    textcolor ( LIGHTBLUE );

    for ( a = 0; a < 5; a++ ) {

          gotoxy ( 23, a + 15 );

          for ( b = 0; b < 5; b++ ) {

                 printf ( " %d ", Matriz [ a ] [ b ] );

          }

    }

    Sleep ( 1800 );

    textcolor ( LIGHTRED );

    gotoxy ( 36, 22 );

    printf ( "MUITO OBRIGADO" );

    getche ( );

    exit ( 0 );

}


Nenhum comentário:

Postar um comentário

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