Home > Net >  Call function for both min and max in C
Call function for both min and max in C

Time:08-15

I have to write a program where I input the number of participants and then the name, surname, age, heigh and weight.

Restrictions- number of participants must be between 1-30

The under age(<18 years) and the ones with height <140cm and >220cm must be eliminated from the list(array).

Finally I have to display the name and weight of the thinest and fatest participants. I got stuck at writing the min/max function to find out the thinest and fatest from my array. I get the same result for both min and max and I don't actually know how to display the name for them.

Here's my code so far. Can somebody help? I already spent multiple hours playing with the code to no avail.

#include <iostream>
#include <string>
using namespace std;

int N;

void remove_persons(string name[], string surname[], float weight[], float height[], float age[])
{

    for (int i = 0; i < N; i  )
    {
        if (age[i] < 18 || height[i] < 140 || height[i] > 220)
        {
            for (int j = i; j < N - 1; j  )
            {
                name[j] = name[j   1];
                surname[j] = surname[j   1];
                age[j] = age[j   1];
                height[j] = height[j   1];
                weight[j] = weight[j   1];
            }
            N--;
            i--;
        }
    }
}

float fatfunc(string name[], string surname[], float weight[])
{

    float fat = weight[0];
    int index_fat = 0;

    for (int i = 1; i < N; i  )
    {

        if (weight[i] > fat)
        {
            fat = weight[i];
            index_fat = i;
        }
    }

    return index_fat;
}

float thinfunc(string name[], string surname[], float weight[])
{

    float thin = weight[0];
    int index_thin = 0;

    for (int i = 1; i < N; i  )
    {

        if (weight[i] < thin)
        {
            thin = weight[i];
            index_thin = i;
        }
    }

    return index_thin;
}

int main()
{

    string name[99];
    string surname[99];
    float weight[99];
    float height[99];
    float age[99];
    int fat, thin;

    do
    {
        cout << "Nr. of participants 1-30: ";
        cin >> N;
    } while (N <= 0 || N >= 30);

    for (int i = 0; i < N; i  )
    {
        cout << "Name[" << i << "]: ";
        cin >> name[i];
        cout << "Surname[" << i << "]: ";
        cin >> surname[i];
        do
        {
            cout << "Age[" << i << "] (>18 years): ";
            cin >> age[i];
        } while (age[i] <= 18);

        cout << "Height in cm [" << i << "] (>140 & <220 cm): ";
        cin >> height[i];

        cout << "Weight [" << i << "]): ";
        cin >> weight[i];
    }

    remove_persons(name, surname, age, weight, height);

    thin = thinfunc(name, surname, weight);
    cout << endl;
    cout << surname[thin] << " "   name[thin] << " is the thinest with a weight of " << weight[thin] << endl;

    fat = fatfunc(name, surname, weight);
    cout << endl;
    cout << surname[fat] << " "   name[fat] << " is the fatest with a weight of " << weight[fat] << endl;

    return 0;
}

CodePudding user response:

Not a direct answer, but in C 20 you can use a totally different approach. You don't have to remove anything from your input array (vector), you just skip over unwanted items in a range based for loop with filters.

#include <string>
#include <vector>
#include <ranges>
#include <iostream>
#include <format>

struct person_t
{
    std::string name;
    unsigned int age;
    double height;
};

// a predicate operating on a person and returning a boolean
// this can later be used in a filter in a range based for loop
bool valid_height(const person_t& person)
{
    return (person.height >= 1.4) && (person.height <= 2.2);
} 

bool valid_age(const person_t& person)
{
    return person.age >= 18;
}

// overload to output a person, makes code more readable later
std::ostream& operator<<(std::ostream& os, const person_t& person)
{
    os << std::format("person : [name = {0}, age = {1}, height = {2}]\n", person.name, person.age, person.height);
    return os;
}

int main()
{
    // Create an "array" with 3 persons
    std::vector<person_t> persons{ {"Alice", 20, 1.78}, {"Bob", 4, 78.5}, {"Charlie", 19, 2.4 } };

    // loop only over those persons that meet a criterion
    // https://en.cppreference.com/w/cpp/ranges/filter_view
    // and pick up to 30 matching persons
    // https://en.cppreference.com/w/cpp/ranges/take_view
    for (const auto& person : persons 
                            | std::views::filter(valid_height)
                            | std::views::filter(valid_age)
                            | std::views::take(30))
    {
        std::cout << person;
    }

    return 0;
}

CodePudding user response:

For your 1st problem writing min max function: answer: you passed the wrong values for remove_person function you did like this //remove_persons(name, surname, age, weight, height); it should be like this // remove_persons(name, surname, weight, height,age); and for your other problem with displaying names code is here(I mentioned where correction has been done):

    #include <iostream>
    #include <string>
    using namespace std;
    int N;
    void remove_persons(string name[], string surname[], float weight[], 
    float height[], float age[])
    {
    for (int i = 0; i < N; i  )
     {       if (age[i] < 18 || height[i] < 140 || height[i] > 220)
    {
        for (int j = i; j < N - 1; j  )
        {
            name[j] = name[j   1];
            surname[j] = surname[j   1];
            age[j] = age[j   1];
            height[j] = height[j   1];
            weight[j] = weight[j   1];
        }
        N--;
        i--;
        }
        }
        }
       //correction
       void fat_and_thin(string name[], string surname[], float weight[])
       {
       int fat, thin;
    fat = thin = weight[0];
    //correction
    int j = 0;
    int k = 0;

    for (int i = 1; i < N; i  )
    {
    if (weight[i] < thin)
    {
        thin = weight[i];
        j = i;
    }

    if (weight[i] > fat)
    {
        fat = weight[i];
        k = i;
    }
    }
    //correction
    cout <<name[j]<<surname[j]<<" is The thinest has : " << thin << endl;
    cout << name[k] << surname[k] << " is The fatest has : " << fat <<endl;
    }
    int main()
    {

    string name[99];
    string surname[99];
    float weight[99];
    float height[99];
    float age[99];

    do
    {
    cout << "Nr. of participants 1-30: ";
    cin >> N;
    } while (N <= 0 || N >= 30);

    for (int i = 0; i < N; i  )
    {
    cout << "Name[" << i << "]: ";
    cin >> name[i];
    cout << "Surname[" << i << "]: ";
    cin >> surname[i];
    do
    {
        cout << "Age[" << i << "] (>18 years): ";
        cin >> age[i];
    } while (age[i] <= 18);

    cout << "Height in cm [" << i << "] (>140 & <220 cm): ";
    cin >> height[i];

    cout << "Weight [" << i << "]): ";
    cin >> weight[i];
    }
    //correction
    remove_persons(name, surname, weight, height,age);
    fat_and_thin(name, surname, weight);

    return 0;
     }
  • Related