IB001 – Seminární skupiny 16, 17, 18 a 19

Študent 5 – zdrojový kód

#include <stdio.h>
#include <string.h>

struct student
{
    char meno[20];
    char priezvisko[20];
    unsigned short uco;
    float priemer;
};

void nacitaj(struct student *stud)
{
    printf("Zadaj meno: ");
    scanf("%19s", stud->meno);
    printf("Zadaj priezvisko: ");
    scanf("%19s", stud->priezvisko);
    printf("Zadaj uco: ");
    scanf("%hu", &stud->uco);
    printf("Zadaj priemer: ");
    scanf("%f", &stud->priemer);
}

void vypis(struct student stud)
{
    printf("%s %s, uco %hu, priemer: %.2f", stud.meno, stud.priezvisko, stud.uco, stud.priemer);
}

int main()
{
    unsigned short pocet;

    printf("Zadaj pocet studentov: ");
    scanf("%hu", &pocet);
    printf("\n");

    struct student studenti[pocet];

    for (unsigned short i = 0; i < pocet; i++)
    {
        nacitaj(&studenti[i]);
        printf("\n");
    }

    printf("Nacitani boli nasledujuci studenti:\n");
    for (unsigned short i = 0; i < pocet; i++)
    {
        vypis(studenti[i]);
        printf("\n");
    }

    unsigned short uco;
    unsigned short najdeny = 0;
    float priemer;
    unsigned short horsi = 0; // pocet studentov s horsim alebo rovnakym priemerom

    printf("\nZistenie percentilu\n");
    printf("Zadaj uco: ");
    scanf("%hu", &uco);

    for (unsigned short i = 0; i < pocet; i++)
    {
        if (studenti[i].uco == uco)
        {
            najdeny = 1;
            priemer = studenti[i].priemer;
            break;
        }

    }

    if (najdeny)
    {
        for (unsigned short i = 0; i < pocet; i++)
        {
            if (studenti[i].priemer <= priemer)
            {
                horsi++;
            }
        }

        float percentil = ((float) horsi / pocet) * 100.0;
        printf("\nStudent s uco %hu ma percentil %.1f.\n", uco, percentil);
    }
    else
    {
        printf("\nStudent so zadanym uco neexistuje.\n");
    }

    return 0;
}