sexta-feira, 30 de novembro de 2012

Programa as quatros operações em Vetor

As quatro operações Matemáticas em Vetor
Mostra aos iniciantes em Linguagem C,
e que estão estudando Vetores, como fazer operações de uma forma bem prática.
Declarei o vetor A [5], de cinco posições onde serão inseridos os
elementos digitados no teclado, e evidentemente também declarei o Vetor B[5],
também para 5 posições onde serão armazenados os resultados do Vetor A [5].
Nem precisa falar sobre os laços for usados, pois está muito claro
o uso e todos sabem da necessidade.

O programa foi criado no eclipse, e mostrou alguns warnigs do mesmo tipo
más que não afetaram o bom funcionamento do programa, veja abaixo;

"array subscript is above array bounds [-Warray-bounds]"

Se alguém souber como resolver, aceito sugestões.

Enfim, tá aí, más um ótimo código didático em linguagem C.

copie, cole e compile:

#include <stdio.h>
#include <conio2.h>
void Janela(){
    int i = 0;
    textcolor(12); //inicio do menu
    for(i = 2; i < 25; i++) {
        gotoxy(1, i); //linha da direita
        printf("\xdb\xdb");
    }
    for(i = 2; i < 25; i++) {
        gotoxy(80, i); //linha da esquerda
        gotoxy(79, i);
        printf("\xdb\xdb");
    }
    gotoxy(1,2);
    int index = 0, contador;
    char letras [5] = "\xdb";
    for (contador = 0; contador < 80; contador++)
    {
        printf("%c",letras [index]);
    }
    gotoxy(1,25);
    for (contador = 0; contador < 80; contador++)
    {
        printf("%c", letras [index]);
    }
}
int main(){
    system ("title PROGRAMA AS 4 OPERACOES EM VETOR");
    int A [5], B [5], i;Janela();
    textcolor(LIGHTRED);gotoxy(25,5);printf("PROGRAMA AS 4 OPERACOES EM VETOR");
    textcolor(LIGHTBLUE);gotoxy(27,7);printf("DIGITE 5 NUMEROS PARA O VETOR");
    for(i = 1; i <= 5; i++){
        textcolor(LIGHTGREEN);gotoxy(25,9);printf("Digite um Numero para a posicao");
        textcolor(LIGHTRED);gotoxy(57,9);printf("%d:  ", i);
        textcolor(YELLOW);scanf("%d",& A [i]);fflush(stdin);
    }
    for(i = 1; i <= 5; i++){
        B [i] = A [i] * 5;
    }
    system("cls");Janela();
    textcolor(LIGHTRED);gotoxy(25,5);printf("PROGRAMA AS 4 OPERACOES EM VETOR");
    textcolor(LIGHTBLUE);gotoxy(30,7);printf("VETOR MULTIPLICADO POR 5");
    textcolor(LIGHTGREEN);gotoxy(16,9);printf("VETOR A");
    textcolor(LIGHTGREEN);gotoxy(47,9);printf("VETOR B");
    for(i = 1; i <= 5; i++){
        textcolor(YELLOW);gotoxy(14, 11 + i);printf("A [%d] = %d", i, A [i]);
        textcolor(LIGHTCYAN);gotoxy(46, 11 + i);printf("B [%d] = %d", i, B [i]);
    }
    getche();
    system("cls");Janela();
    textcolor(LIGHTRED);gotoxy(25,5);printf("PROGRAMA AS 4 OPERACOES EM VETOR");
    textcolor(LIGHTBLUE);gotoxy(30,7);printf("VETOR SOMADO COM 5");
    textcolor(LIGHTGREEN);gotoxy(16,9);printf("VETOR A");
    textcolor(LIGHTGREEN);gotoxy(47,9);printf("VETOR B");
    for(i = 1; i <= 5; i++){
        B [i] = A [i] + 5;
    }
    for(i = 1; i <= 5; i++){
        textcolor(YELLOW);gotoxy(14, 11 + i);printf("A [%d] = %d", i, A [i]);
        textcolor(LIGHTCYAN);gotoxy(46, 11 + i);printf("B [%d] = %d", i, B [i]);
    }
    getche();
    system("cls");Janela();
    textcolor(LIGHTRED);gotoxy(25,5);printf("PROGRAMA AS 4 OPERACOES EM VETOR");
    textcolor(LIGHTBLUE);gotoxy(30,7);printf("VETOR SUBTRAIDO 5");
    textcolor(LIGHTGREEN);gotoxy(16,9);printf("VETOR A");
    textcolor(LIGHTGREEN);gotoxy(47,9);printf("VETOR B");
    for(i = 1; i <= 5; i++){
        B [i] = A [i] - 5;
    }
    for(i = 1; i <= 5; i++){
        textcolor(YELLOW);gotoxy(14, 11 + i);printf("A [%d] = %d", i, A [i]);
        textcolor(LIGHTCYAN);gotoxy(46, 11 + i);printf("B [%d] = %d", i, B [i]);
    }
    getche();
    system("cls");Janela();
    textcolor(LIGHTRED);gotoxy(25,5);printf("PROGRAMA AS 4 OPERACOES EM VETOR");
    textcolor(LIGHTBLUE);gotoxy(30,7);printf("VETOR DIVIDIDO POR 5");
    textcolor(LIGHTGREEN);gotoxy(16,9);printf("VETOR A");
    textcolor(LIGHTGREEN);gotoxy(47,9);printf("VETOR B");
    for(i = 1; i <= 5; i++){
        B [i] = A [i] / 5;
    }
    for(i = 1; i <= 5; i++){
        textcolor(YELLOW);gotoxy(14, 11 + i);printf("A [%d] = %d", i, A [i]);
        textcolor(LIGHTCYAN);gotoxy(46, 11 + i);printf("B [%d] = %d", i, B [i]);
    }
    textcolor(LIGHTRED);gotoxy(33,21);printf ( "O ROCCO AGRADECE" );
    getche();
}

Nenhum comentário:

Postar um comentário

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