Programando en C – Carrera
Este programa pide 5 corredores con sus respectivos tiempo, te muestra la clasificación y por último te devuelve el nombre del ganador y el tiempo medio.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define MAX_CORREDORES 5 #define MAX_CADENA 20 //Estructuras struct Corredor{ char nombre[MAX_CADENA]; float tiempo; }; //Prototipos de funciones void leer_corredores(int numero_corredores, struct Corredor clasificacion[]); void mostrar_corredores(int numero_corredores, struct Corredor clasificacion[]); void calcular_datos(int numero_corredores, struct Corredor clasificacion[], float &media_tiempo, char ganador[]); //Funcion main int main() { float media_tiempo=0; struct Corredor clasificacion[MAX_CORREDORES]; char ganador[MAX_CADENA]; leer_corredores(MAX_CORREDORES,clasificacion); system("cls"); mostrar_corredores(MAX_CORREDORES,clasificacion); getch(); system("cls"); calcular_datos(MAX_CORREDORES,clasificacion,media_tiempo,ganador); printf("El ganador es %s",ganador); printf("El tiempo medio ha sido %f",media_tiempo); getch(); return 0; } //Funciones void leer_corredores(int numero_corredores, struct Corredor clasificacion[]) { int i; for(i=0;i<numero_corredores;i++) { printf("\nNombre del corredor %d: ",i+1); fflush(stdin); gets(clasificacion[i].nombre); printf("Tiempo: "); scanf("%f", &clasificacion[i].tiempo); } } void mostrar_corredores(int numero_corredores, struct Corredor clasificacion[]) { int i; for(i=0;i<numero_corredores;i++) { printf("El corredor %s ha hecho un tiempo de %.3f segundos" ,clasificacion[i].nombre, clasificacion[i].tiempo); } } void calcular_datos(int numero_corredores, struct Corredor clasificacion[], float &media_tiempo, char ganador[]) { int i; float tiempo; tiempo=clasificacion[0].tiempo; ganador[0]=''; strcpy(ganador,clasificacion[0].nombre); for(i=0;i<numero_corredores;i++) { media_tiempo+=clasificacion[i].tiempo; if(clasificacion[i].tiempo<tiempo) { tiempo=clasificacion[i].tiempo; ganador[0]=''; strcpy(ganador,clasificacion[i].nombre); } } media_tiempo/=numero_corredores; } |
Recent Comments