sábado, 1 de dezembro de 2012

Maior - Menor e Media num Vetor

Neste Vetor, inicializado e declarado com o tipo float, nos dar a possibilidade de mostrar
o maior, e o menor elemento contido, e também a média dos valores.
 O Programa imprime primeiro o vetor com seus valores, depois mostra estes valores ordenados.
Muito simples e funcional, teste e comprove:

#include <stdio.h>
#include <conio2.h>
int main(){
    float vet [10] = {2.4, 4.9, 3.5, 5.7, 6.8, 1.7, 8.9, 10.2, 7.5, 9.3}, x = 0, y = 0, media = 0, soma = 0;
    int j, i;
    system ("title MAIOR - MENOR E MEDIA NUM VETOR");
    textcolor(LIGHTRED);gotoxy(27,2);printf("MAIOR - MENOR E MEDIA NUM VETOR");
    // textcolor(LIGHTBLUE);gotoxy(33,5);printf("sa_sp10@hotmail.com");
    textcolor(LIGHTGREEN);gotoxy(30,4);printf("Os valores do vetor sao ");
    textcolor(YELLOW);for(i = 0; i < 10; i++){
        textcolor(YELLOW); gotoxy(33,i + 6);printf("Posicao  ");
        textcolor(LIGHTRED);gotoxy(83,i + 6); printf("[%d] = %.1f", i, vet [i]);
    }
    for(i = 0; i < 9; i++){
        for(j = i + 1; j < 10; j++){
            if(vet [i] > vet [j]){
                x = vet [i];
                vet [i] = vet [j];
                vet [j] = x;
            }
        }
    }
    textcolor(LIGHTBLUE);gotoxy(30,17);printf("Os valores ordenados sao: ");
    textcolor(LIGHTRED);gotoxy(15,19);    for(i = 0; i < 10; i++){
        printf(" %.1f ",vet [i]);
        soma = soma + vet [i];
        media = soma / 10;
    }
    for(i = 0; i < 9; i++){
        for(j = i + 1; j < 10; j++){
            if(vet [i] < vet [j]){
                y = vet [i];
                vet [i] = vet [j];
                vet [j] = y;
            }
        }
    }
    textcolor(YELLOW);gotoxy(30,21);printf("A soma dos valores e: ");
    textcolor(LIGHTRED);gotoxy(83,21);printf("%.1f", soma);
    textcolor(YELLOW);gotoxy(30,22);printf("O maior valor e: ");
    textcolor(LIGHTRED);gotoxy(83,22);printf("%.1f", x);
    textcolor(YELLOW);gotoxy(30,23);printf("O menor valor e: ");
    textcolor(LIGHTRED);gotoxy(83,23);printf("%.1f", y);
    textcolor(YELLOW);gotoxy(30,24);printf("A media e: ");
    textcolor(LIGHTRED);gotoxy(83,24);printf("%.1f", media);
    getche();
}

Nenhum comentário:

Postar um comentário

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