Thread: (name-age) pair vector program help needed

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    384

    (name-age) pair vector program help needed

    This is the exercise spec:
    Read five names into a vector<string> name, then prompt the user for the ages of the people named and store the ages in a vector<double> age. Then print out the five (name[i],age[i]) pairs. Sort the names (sort(name.begin(),name.end())) and print out the (name[i],age[i]) pairs. The tricky part here is to get the age vector in the correct order to match the sorted name vector. Hint: Before sorting name, take a copy and use that to make a copy of age in the right order after sorting name. Then, do that exercise again but allowing an arbitrary number of names.
    The part I'm having difficulty with, mainly, is the Hint part ("Hint: Before sorting name, take a copy and use that to make a copy of age in the right order after sorting name."). I'm not sure how to actually do that.

    This the code I've got:
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include "../custom_std_lib_facilities.h"
    
    using namespace std;
    
    int main()
    {
        vector<string> names;
        vector<int> ages;
        string name;
        int age;
        cout << "Please enter five names: ";
        for (int i  = 0; i < 5; i++)
        {
            cin >> name;
            cin.ignore();
            names.push_back(name);
            sort(names.begin(), names.end());
            cout << "This was name number " << i + 1 << "\n";
        }
        cout << "Please enter those five people's ages: ";
        for (int i = 0; i < 5; i++)
        {
            cin >> age;
            cin.ignore();
            ages.push_back(age);
            cout << "This was age number " << i + 1 << "\n";
        }
        for (unsigned i = 0; i < names.size(); i++)
        {
            cout << names[i] << " is of age " << ages[i] << "\n";
        }
        keep_window_open();
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Here is what I gather the exercise sounds like it wants you to do:

    Read 5 names into a vector of strings. Btw, don't use cin.ignore() after reading a string. A string can read anything, so you will end up blocking and ignoring the first newly inputted character.
    Read 5 ages into a vector of ints. Btw, there is no point in sorting either vector every iteration. The requirement is that the vector shall be sorted eventually, not kept sorted every time there is a new insertion.
    Make a copy of the names vector.
    Sort the names vector.
    For every element in the names vector, try to find the corresponding index in the old names vector. Use that index to find the right age. Using that information, create a new vector of ages.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    Okay, I fixed what you suggested for the string. This is what I've got right now:
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include "../custom_std_lib_facilities.h"
    
    using namespace std;
    
    int main()
    {
        vector<string> names;
        vector<int> ages;
        string name;
        int age;
        cout << "Please enter five names: ";
        for (int i  = 0; i < 5; i++)
        {
            cin >> name;
            names.push_back(name);
            cout << "This was name number " << i + 1 << "\n";
        }
        vector<string> names_copy(sort(names.begin(), names.end()));
        cout << "Please enter those five people's ages: ";
        for (int i = 0; i < 5; i++)
        {
            cin >> age;
            cin.ignore();
            ages.push_back(age);
            cout << "This was age number " << i + 1 << "\n";
        }
        for (unsigned i = 0; i < names.size(); i++)
        {
            for (unsigned j = 0; j < names_copy.size(); j++)
            {
                if (names[i] == names_copy[j])
                {
                     // guidance and suggestions needed about the code to put here
                }
            }
        }
        keep_window_open();
    }
    But now how do I find the right age?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Osman Zakir
    But now how do I find the right age?
    Since you have found the position of the name in the original name vector, you can thus use that position/index to access the corresponding element in the age vector.

    By the way, this is a horrible way of doing things. It would be better to at least have a simple struct to represent a name-age pair:
    Code:
    struct Person
    {
        std::string name;
        double age;
    };
    Then create a single std::vector<Person> to be sorted. Alternatively, one might use a std::vector<std::pair<std::string, double>> (sacrificing the benefit of the member names for less work), or a std::map<std::string, double> (in which case the sorting is implicit).

    If you really need to have parallel arrays, then perhaps sorting pairs of pointers to their elements would be better.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    But the names vector has been sorted already, and the ages may have been entered in a different order. How would I find the right age in that case?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    By the way, this is a horrible way of doing things. It would be better to at least have a simple struct to represent a name-age pair:
    I agree it's horrible, but the exercise says it should be done this way. Still, it is a good advice for future code.

    Quote Originally Posted by Osman Zakir View Post
    But the names vector has been sorted already, and the ages may have been entered in a different order. How would I find the right age in that case?
    Did I not mention this in my original reply? Read it again.

    Btw, what does this do?
    vector<string> names_copy(sort(names.begin(), names.end()));
    Did you compile your code before posting?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Osman Zakir
    But the names vector has been sorted already, and the ages may have been entered in a different order. How would I find the right age in that case?
    Even before Elysia mentioned it, this was in the hint provided by your instructor: "Before sorting name, take a copy and use that to make a copy of age in the right order after sorting name."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by laserlight View Post
    Even before Elysia mentioned it, this was in the hint provided by your instructor: "Before sorting name, take a copy and use that to make a copy of age in the right order after sorting name."
    Even with this hint instructor has chosen some very convoluted way of doing things
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    Yeah. I get all that. But how am I supposed to do this? Can't you try guiding me towards it? I'm willing to try working towards it as well.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What point of the hints I gave did you not understand?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    About post #6, after "Btw", yeah, I compiled it afterwords and caught that error. I fixed it to:
    Code:
    vector<string> names_copy(names.begin(), names.end());
    and now it works at least up to that part.

    Anyway, now I'd like some help on how to use the index of the name in the copied names vector to search out the corresponding age and print it for each name. And yeah, even if I do manage to understand the logic, I don't have the syntax down at this point yet. I'm not sure how to do this.

    What I need is just this part now, if the rest of it is good enough:
    Code:
         for (unsigned i = 0; i < names.size(); i++)
        {
            for (unsigned j = 0; j < names_copy.size(); j++)
            {
                if (names[i] == names_copy[j])
                {
    
                }
            }
        }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Osman Zakir
    Anyway, now I'd like some help on how to use the index of the name in the copied names vector to search out the corresponding age and print it for each name. And yeah, even if I do manage to understand the logic, I don't have the syntax down at this point yet. I'm not sure how to do this.
    The syntax is incredibly simple. It is a matter of logic, not syntax, unless you don't know the syntax for accessing the elements of a vector by index, but you obviously do know that syntax.

    Let's suppose that we have four names and ages in two parallel lists:
    Code:
    Trent    30
    Alice    25
    Mallory  15
    Bob      20
    After we sort the list of names, we get:
    Code:
    Alice
    Bob
    Mallory
    Trent
    Now, we want to print the list of ages corresponding to the sorted list of names. We start with the first element of the sorted list, i.e., Alice, and find that in the original list, Alice is at index 1. Therefore, we print the age at index 1, i.e., we print 25. Then, we go on to the next element of the sorted list, i.e., Bob. We find that in the original list, Bob is at index 3. Therefore, we print the age at index 3, i.e., 20. So on and so forth:
    Code:
    25
    20
    15
    30
    As such, the syntax required is just ages[index], where it is your job to figure out what exactly is index: in your code, is it i or j? If you understand the logic, the answer would be obvious. If you don't, complete mastery of the syntax of all the programming languages in the world will not help you.

    EDIT:
    By the way, instead of this:
    Code:
    vector<string> names_copy(names.begin(), names.end());
    You might as well write:
    Code:
    vector<string> names_copy(names);
    Remember to sort names after doing this.
    Last edited by laserlight; 05-05-2015 at 02:14 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse vector help needed
    By Osman Zakir in forum C++ Programming
    Replies: 15
    Last Post: 05-07-2015, 04:35 PM
  2. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  3. Vector pair
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2010, 01:50 PM
  4. need another pair of eyes please
    By dustyd in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2003, 03:12 PM
  5. need a second pair of eyes
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 06-27-2002, 12:36 PM