Home > other >  How to decide who is the youngest of the three people using loop - comparing year, month and day of
How to decide who is the youngest of the three people using loop - comparing year, month and day of

Time:11-27

I have three people and I need to find out who is the youngest of them I need the code to work in the main part.

person is data structure I had created:

typedef struct 
{
    char name[50];
    ....
    char day;
    char month;
    int year;
} person;

And here is how I search for the youngest person:

the_youngest(**person** s[]);

    the_youngest(person s[])
{
        if (s[0].year > s[1].year > s[2].year || s[0].year > s[2].year > s[1].year)

            printf("%d %d is the youngest", s[0].name, s[0].lastname);

        else if (s[0].year = > s[1].year = > s[2].year || s[0].year = > s[2].year = > s[1].year && s[0].month > s[1].month > s[2].month || s[0].month > s[2].month > s[1].month)

            printf("%d %d is the youngest", s[0].name, s[0].lastname);

        else if (s[0].month = > s[1].month = > s[2].month || s[0].month = > s[2].month = > s[1].month && s[0].day > s[1].day > s[2].day || s[0].day > s[2].day > s[1].day)

            printf(" %d %d is the youngest", s[0].name, s[0].lastname);

        else if (s[1].year > s[0].year > s[2].year || s[1].year > s[2].year > s[0].year)

            printf("%d %d is the youngest", s[1].name, s[1].lastname);
        
        else if (s[1].year => s[0].year = > s[2].year || s[1].year = > s[2].year = > s[0].year && s[1].month > s[0].month > s[2].month || s[1].month > s[2].month > s[0].month)

            printf(" %d %d is the youngest", s[1].name, s[1].lastname);

        else if (s[1].month => s[0].month = > s[2].month || s[1].month = > s[2].month = > s[0].month && s[1].day > s[0].day > s[2].day || s[1].day > s[2].day > s[0].day)

            printf(" %d %d is the youngest", s[1].name, s[1].lastname);

        else if (s[2].year > s[1].year > s[0].year || s[2].year > s[0].year > s[1].year)

            printf(" %d %d is the youngest", s[2].name, s[2].lastname);

        else if (s[2].year => s[0].year = > s[1].year || s[2].year = > s[1].year = > s[0].year && s[2].month > s[1].month > s[0].month || s[2].month > s[0].month > s[1].month)

            printf(" %d %d is the youngest", s[2].name, s[2].lastname);

        else if (s[2].month => s[1].month => s[0].month || s[2].month = > s[0].month = > s[1].month && s[2].day > s[0].day > s[1].day || s[2].day > s[1].day > s[0].day)

            printf(" %d %d is the youngest", s[2].name, s[2].lastname);

        else printf("It isn't possible to decide, who's the youngest");
}

in the main I have:

o = the_youngest(s[]);

CodePudding user response:

You could simplify the problem by only comparing two persons at a time. That could be done by calling a function that takes pointers to two persons A and B, and returns -1 if A is younger than B, 0 if A and B are the same age, and 1 if A is older than B:

int compare_person_ages(const person *a, const person *b)
{
    if (a->rok < b->rok)
        return -1;
    if (a->rok > b->rok)
        return 1;
    if (a->mesic < b->mesic)
        return -1;
    if (a->mesic > b->mesic)
        return 1;
    if (a->den < b->den)
        return -1;
    if (a->den > b->den)
        return 1;
    return 0;
}

In the_youngest function, first assume that index 0 is the youngest and loop through index 1 to N-1 comparing with the youngest. If the current index is younger than the previous youngest, make the current index the youngest. Also check if more than one person is the youngest:

void the_youngest(const person s[], unsigned int n)
{
    int undecided = (n == 0); /* Treat 0 persons as undecided. */
    unsigned int youngest = 0; /* s[0] is the youngest so far. */
    /* Loop through the other persons. */
    for (unsigned int i = 1; i < n; i  )
    {
        int cmp = compare_person_ages(&s[i], &s[youngest]);
        if (cmp < 0)
        {
            youngest = i;  /* Make s[i] the youngest so far. */
            undecided = 0; /* No one else the same age so far. */
        }
        else if (cmp == 0)
        {
            undecided = 1; /* At least 2 people are the youngest so far. */
        }
    }
    if (undecided)
    {
        /* Either there are 0 persons, or at least 2 people are the youngest. */
        printf("It isn't possible to decide, who's the youngest\n");
    }
    else
    {
        /* Only 1 person is the youngest. */
        printf("%s %s is the youngest\n", s[youngest].name, s[youngest].lastname);
    }
}

CodePudding user response:

You can solve this by using two nested loops. For each person i, you check if any person j with a higher index is younger or the same age. If they are the same age, you should also handle it here (I leave this to you). If no person j is younger, person i is the youngest. If you found a younger person than person i, you can leave out person i in all the following comparisons.

Something along these lines (in pseude code):

for (int i=0; i<person_count; i  )
{
    bool youngerPersonFound = false;
    int j=i 1;
    while(j < person_count && !youngerPersonFound)
    {
        if(person[j] is younger or equal as person[i])
        youngerPersonFound = true;
    }

    if(!youngerPersonFound)
    {
        youngestPersonFound = true;
        print "person[i] is the youngest"
        break;
    }
}

Off topic: The formatting of your question was atrocious. Out of respect for the time of the people you want to answer you, please try to improve that for your next question. Structure and format your question properly, and you will find many people who are willing to help you.

CodePudding user response:

PART ONE: Determining which person is the youngest

The following: s[0].rok > s[1].rok > s[2].rok is most certainly not what you are trying to achieve.

In C, this is equivalent to (s[0].rok > s[1].rok) > s[2].rok.

This will first compute s[0].rok > s[1].rok, which is either 0 if the condition is false or any non-zero value (most likely 1) otherwise.

Then you evaluate 0 > s[2].rok or 1 > s[2].rok which is definitely not what you wanted.

The logical condition you are trying to achieve is "a is greater than b AND b is greater than c"

In C, this is written: a > b && b > c
This can be applied to all of your conditions and is something you should remember: operators on one line are not transitive. Meaning:

a > b > c //does not work
a == b == c //does not work
a < b < c //does not work

You should be able to fix your function from there.

BUT: I strongly advise you use loops as Benedict suggests in his answer, and not the tens of ifs you have. What if tomorrow you need to find the youngest of 4 people? Or 10?

PART TWO: About calling the function

o = the_youngest(s[]);

Passing an array as parameter

This is what you seem to be trying with the the_youngest(s[]) part.

Look here for an example of how to do that.
In the end it should look something like:

void the_youngest(struct person s[])
{
//Code here
}

int main(void)
{
  struct person persons[3];
  persons[0].year = 1960;
  //Initialize other values here
  the_youngest(persons);
}

Note that it does not matter whether the parameter is named s or not.

Getting a result through a return value

This is what you are trying to do with the o = part.

First you should know that all C functions should always be declared with a return type, which you didn't do with the_youngest(person s[]).
Looking here first should provide you with an idea of how to return a value in C.
You can then learn how to return the youngest person from your function.

Happy learning!

  • Related