Find the person who is younger


I have two arrays in C ++ , one with the names of people and another with the ages.

How could I find the person younger and show it on the screen?



Alejandro 37
  Tatiana 28
  Maria 52

... and show on the screen, The youngest person is Tatiana.

I have this code:

int n;
char nombre[8][20];
int edad[8];
int numero[100];
int menor=100;

cout<<"Digite la cantidad de jugadores: ";
for(int i=0;i<n;i++){    
    cout<<"\nDigite el nombre del jugador "<<i+1<<" : ";
    cout<<"Digite la edad del jugador "<<i+1<<" : ";

for(int i=0; i<n; i++){
    cout<<"Jugador "<<i+1<<" -> "<<nombre[i]<<endl;
for(int i=0; i<n; i++){
    if(edad[i] < menor){
        menor = edad[i];
        cout<<"\nEl menor elemento del vector es: "<<nombre[i]<<" "<<menor<<endl;

... and print this screen:


Type the number of players: 4


Type the name of player 1: Maria
  Enter the age of the player 1: 52


Type the name of player 2: Juan
  Enter the age of the player 2: 20


Type the name of player 3: Pablo
  Enter player's age 3: 14


Type the name of player 4: Carmen
  Enter player's age 4: 32


Player 1 - > Maria
  Player 2 - > Juan
  Player 3 - > Pablo
  Player 4 - > Carmen


The smallest element of the vector is: María 52
  The smallest element of the vector is: Juan 20
  The smallest element of the vector is: Pablo 14

I need you to print me in this case: The smallest element of the vector is: Pablo 14

This is because Pablo is the youngest.

asked by Ali-Rash 16.07.2017 в 22:47

3 answers


You could do it by defining a class Persona that has the attributes nombre and edad (in this way when creating an object of this type you will have associated a nombre and edad to a Persona ), these objects could have these attributes as public or as private and be accessed by methods (when object-oriented program the idea is to do it in the latter way).

Then you can finally create an array of several objects of type Persona and search for the object of this type that has the lowest age by traversing the entire array with a simple for and comparing the ages with the aforementioned method.

Then the steps would be:

  • Navigate arrays of names and ages and instantiate objects of type Persona .
  • Find the type object Persona that has the youngest age and print its name.
  • Here is an example to guide you, you just need to create the Personas array from your name and age arrays.

    #include <iostream>
    #include <string>
    class Persona {
        Persona(std::string nombre, int edad);
        std::string getNombre();
        int getEdad();
        std::string nombre;
        int edad;
    Persona::Persona(std::string nombre, int edad) {
      this->nombre = nombre;
      this->edad = edad;
    std::string Persona::getNombre() {
      return nombre;
    int Persona::getEdad() {
      return edad;
    int main( ) {
      Persona P1("Juan", 35);
      Persona P2("Maria", 20);
      Persona P3("Roberto", 49);
      Persona P4("Patricio", 19);
      Persona P5("Fernanda", 25);
      Persona arreglo[5] = {P1, P2, P3, P4, P5};
      int menorEdad = arreglo[0].getEdad();
      Persona resultado(arreglo[0]);
      for (int i = 1; i < 5; i++)
        if (menorEdad > arreglo[i].getEdad()) {
          resultado = arreglo[i];
          menorEdad = arreglo[i].getEdad();
      std::cout << "Las persona con menor edad es " << resultado.getNombre();
      return 0;

    EDIT :

    Considering the code that you put the error you have it in this part.

    for (int i = 0; i < n; i++) {
      if (edad[i] < menor) {
        menor = edad[i];
        cout << "\nEl menor elemento del vector es: " << nombre[i] << " " << menor << endl;

    Since menor you define as an integer equal to 100, and within the loop for (which traverses the entire array) every time you find someone with an age less than menor the execution will enter if and will be printed by screen El menor elemento del vector es... and update the value of menor , but what you want to do is print only the smallest of all, so you should change that for this other code:

    int indice_menor = 0;
    for (int i = 0; i < n; i++) {
      if (edad[i] < menor) {
        menor = edad[i];
        indice_menor = i;
    cout << "\nEl menor elemento del vector es: " << nombre[indice_menor] << " " << menor << endl;

    Here what you would do is find the index that corresponds to the lowest age of all and then print the message once with the data of the one who is younger.


    answered by 17.07.2017 / 00:27

    I propose a variant that uses the min_element function of the standard C ++ library and a lambda expression as a predicate, as a C ++ 11 way to address the problem.

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <string>
    // datos de un jugador
    struct Jugador {
        std::string nombre;
        int edad;
    // [NOTA] Como los miembros nombre y edad van a ser accedidos directamente,
    // tanto para lectura como para escritura, no necesitan ser privados
    // o funciones de acceso.
    int main()
        // cantidad de jugadores:
        int n;
        std::cout << "Digite la cantidad de jugadores: ";
        std::cin >> n;
        // vector de Personas:
        std::vector<Jugador> grupo;
        // variables auxiliares para facilitar la entrada de datos
        std::string nombre;
        int edad;
        // creas cada Jugador y lo incluyes en el grupo:
        for (int i = 0; i < n; ++i) {
            std::cout << "nombre del jugador: ";
            std::cin >> nombre;
            std::cout << "edad del jugador: ";
            std::cin >> edad;
            grupo.push_back({ nombre, edad }); // ingreas este jugador al gurpo
        // obtienes el jugador de menor edad 
        // con la función de la biblioteca estándar min_element
        // usando como predicado una expresión que devuelve el menor de dos ints
        auto it = std::min_element(grupo.begin(), grupo.end(),
            [](const Jugador& j1, const Jugador& j2)
        { return j1.edad < j2.edad; });
        std::cout << "el jugador de menor edad es: " << it->nombre << ' ' << ' '  
                  << it->edad << '\n';
    answered by 19.07.2017 в 17:43

    Well, showing the age from least to greatest or vice versa is easy, just do the algorithm of the bubble so that it is ordered and ready, but you have to relate the age arrangement with the names, there must be some type of relationship, either by the index or by its position

    answered by 16.07.2017 в 23:23