You are on page 1of 3

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE_NAME 128

typedef struct date


{
int day;
int month;
int year;
} Date;

typedef struct person


{
char name[MAX_SIZE_NAME];
Date birthday;
float height;
} Person;

void readPersons(Person *persons, int size);


void findOldest(Person *persons, int size, char *name, Date *birthday);
void countPersonsAverageHeightPerMonth(Person *persons, int size);
void printPersonsBornOnDate(Person *persons, int size, Date *date);

int main()
{
Person *arrayOfPersons;
int size;
char name[MAX_SIZE_NAME];
Date birthday;

do
{
printf("Give number of persons you want to process: ");
scanf("%d", &size);
if (size <= 0)
{
printf("You have to give a positive number of persons. Try
again.\n");
}
} while (size <= 0);

//δέσμευση και έλεγχος απόδοσης μνήμης


...

//διάβασμα στοιχείων προσώπων

readPersons(arrayOfPersons, size);

//εύρεση του μεγαλύτερου ατόμου


findOldest(arrayOfPersons, size, name, &birthday);
printf("Oldest person is %s born on %d/%d/%d\n", name, birthday.day,
birthday.month, birthday.year);
//εύρεση του πλήθους των ατόμων και του ΜΟ των υψών τους ανά μήνα
countPersonsAverageHeightPerMonth(arrayOfPersons, size);

//εύρεση του ή των ατόμων με την ίδια ημερομηνία γέννησης που δίνει ο
χρήστης
printf("\nGive the date (DD/MM/YYYY) to find persons born on that
date: ");
scanf("%d/%d/%d", &birthday.day, &birthday.month, &birthday.year);

printPersonsBornOnDate(arrayOfPersons, size, &birthday);

//ελευθέρωση δεσμευμένης μνήμης


free(arrayOfPersons);

return 0;
}

void readPersons(Person *persons, int size)


{
...
}

// It returns only the first person found with the oldest age. Consider
the extension to return all oldest persons.
void findOldest(Person *persons, int size, char *name, Date *birthday)
{
...
}

void countPersonsAverageHeightPerMonth(Person *persons, int size)


{
int i;
float sumOfHeights[12] = {0};
int count[12] = {0};

for (i = 0; i < size; i++)


{
//κατάλληλο γέμισμα διατάξεων sumOfHeights και count
}
for (i = 0; i < 12; i++)
{
//εκτύπωση του πλήθους των ατόμων και του ΜΟ των υψών τους ανά
μήνα
}
}

//εναλλακτική υλοποίηση της συνάρτησης για την εύρεση του πλήθους των
ατόμων και του ΜΟ των υψών τους ανά μήνα
void countPersonsAverageHeightPerMonth2(Person *persons, int size)
{
int i, j;
float sum;
int count;

for (i = 0; i < 12; i++)


{
sum = 0;
count = 0;
for (j = 0; j < size; j++)
{
//κατάλληλος υπολογισμός των sum και count
}
//εκτύπωση του πλήθους των ατόμων και του ΜΟ των υψών τους για το
μήνα
}
}
void printPersonsBornOnDate(Person *persons, int size, Date *date)
{
int i;
int noPersonsFound = 1;

for (i = 0; i < size; i++)


{
if (persons[i].birthday.day == date->day &&
persons[i].birthday.month == date->month && persons[i].birthday.year ==
date->year)
{
noPersonsFound = 0;
printf("%s\n", persons[i].name);
}
}
if (noPersonsFound)
{
printf("No persons found\n");
}
}

You might also like