terça-feira, 11 de fevereiro de 2014

Substituindo Elementos em Vetor


Substituir Elementos num Vetor Dinâmico usando o exemplo deste código
que eu criei, é muito fácil. Note que o Vetor é passado por referência,
e suas posições para substituição foram inicializadas na função subs_titui();
Código dedicado a iniciantes em Linguagem C, os Marmanjos da linguagem que me desculpe.

Veja algumas imagens do programa em execução:



Veja este excelente código abaixo:

#include <stdio.h>
#include <conio.h>
#define tam 5
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 );
}
void subs_titui ( int *Vetor ){
    Vetor [0] = 17;
    Vetor [1] = 9;
    Vetor [2] = 3;
    Vetor [3] = 15;
    Vetor [4] = 8;
}
int main(){
    int *Vetor;
    system ("title SUBSTITUINDO ELEMENTOS EM VETOR");system("cls");
    int i;
    Vetor = ( int* ) malloc ( tam * sizeof ( int ) );
    for ( i = 0; i < tam; i++ ){Janela5();
    textcolor(LIGHTRED);gotoxy(29,3);printf("SUBSTITUINDO ELEMENTOS EM VETOR" );
    textcolor(YELLOW);gotoxy(24,5);printf("Programa Desenvolvido Por:");
    textcolor(LIGHTCYAN);gotoxy(51,5);printf("Samuel Lima");
    textcolor(LIGHTGREEN);gotoxy(35,7);printf("sa_sp10@hotmail.com");
    textcolor(LIGHTBLUE);gotoxy(19,9);printf("Digite um numero para a posicao " );
    textcolor(LIGHTRED);printf(" %d%c ", i + 0, 167 );
    textcolor(LIGHTBLUE);printf("do Vetor " );
    textcolor(YELLOW);gotoxy(66, 9);scanf("%d", &Vetor [ i ] );fflush(stdin);system("cls");
    }Janela5();
    textcolor(LIGHTRED);gotoxy(28,3);printf("SUBSTITUINDO ELEMENTOS EM VETOR" );
    textcolor(LIGHTBLUE);gotoxy(30,5);printf( "Veja abaixo o Vetor Montado ");
    textcolor(YELLOW);gotoxy(29, 7);
    for( i = 0; i < tam; i++ ){
        printf(" %d ", Vetor [ i ] );
    }
    getche();
    textcolor(LIGHTGREEN);gotoxy(24,9);printf( "Agora vamos substituir estes valores: ");
    getche();
    subs_titui ( Vetor );
    textcolor(LIGHTBLUE);gotoxy(29,11);printf( "Veja abaixo o Vetor Modificado ");
    textcolor(YELLOW);gotoxy(29, 13);
    for( i = 0; i < tam; i++ ){
        printf(" %d ", Vetor [ i ] );
    }
    getche();
    textcolor(LIGHTRED);gotoxy(33,21); printf(" ROCCO AGRADECE");getche();exit(0);
}

Nenhum comentário:

Postar um comentário

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