Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a program in C++ which should input a string array, and string should contain Employee Name Salary and Its Manager Name with comma seperated delimeter

The input will be provided following format:
Mr XX, 5200000, NONE
Mr YY,52000, Mr. XX
Mr ZZ, 34500, Mr. YY

There is a Employee whose Manager is NONE that means he is the boss of the company. Now I wanted the name of the employee who is farthest from the boss of the company in the list of employee string array. For above input the out put should be Mr. ZZ

I have taken input in string array and run the loop for the whole array. In the loop I have splitted the string in the substring with comma seperation to get the employee name and manager name and stored the result in two vectors. Now I am comparing the result of these vectors but am not able to write the efficient alogorithm which can result the desired output. Any direction would be great help


P.S.:
copied the following issue and code from comments below:



When am passing my vectors in the EvaluateLevel my code is crashing.

Here's my code:
C++
int EvaluateLevel (int idx,
    const vector<string>& names,
    const vector<string>& managers,
    vector<int>& levels)
{
    if (managers[idx]=="NONE")
    {
        levels[idx] = 0;
        return 0;
    }
    if (levels[idx] != -1)
        return levels[idx];
 
    // find the index of the manager
    string manager = managers[idx];
    int managersIdx = -1;
    for (int i = 0; i < names.size(); ++i)
        if (names[i] == manager)
        {
            managersIdx = i;
            break;
        }
    // or better use std::find instead of the above loop if you are familiar
    // enough with STL

    // assign the manager's level + 1
    if (managersIdx = -1)
        return -1; // not found; error in input data
    int managersLevel = EvaluateLevel (managersIdx, names, managers, levels);
    if (managersLevel < 0)
        return -1; // recursive call returned an error
    levels[idx] = managersLevel + 1;
    return levels[idx];
}
int main()
{
const int size =2;
string text[size] ;
vector<std::string> EmpName;
vector<std::string> MgrName;
vector<int> level;
cout << "Enput the Strings" << endl;
 
    for(int m =0 ;m<size;m++) { cin>> text[m];
        }
 
for(int n =0;n<size;n++) { std::vector<std::string> words;
    std::string::size_type beg = 0, end;
    do
    {
        end = text[n].find(',', beg);
        if (end == std::string::npos)
        {
            end = text[n].size();
        }
        words.emplace_back(text[n].substr(beg, end - beg));
        beg = end + 1;
    } while (beg < text[n].size());
 

 
    EmpName.push_back(words[0]);
    MgrName.push_back(words[2]);

	EvaluateLevel(size,EmpName,MgrName,level);
 

}
 

 
  
 
   cin.get();
 
}<pre lang="c++">
Posted
Updated 31-Jul-14 5:53am
v5
Comments
CPallini 29-Jul-14 16:31pm    
I suppose you may simply search for the boss and then get the farthest item from it.

The best approach would be to store your entire input data in a tree structure. If you are familiar with tree-structures, this will probably the most obvious solution.

As you have not chosen that path, I assume that you are not familiar with building a tree in C++. So here is an easier way to solve your problem almost as efficiently, just based on the two vectors that you already have:

1. Create a third vector, a std::vector<int> and name it level. The idea is to store the employee's level in the company hierarchy in that vector. The boss is assigned level 0, his immediate subordinates level 1, and so forth.

2. Initially fill the level vector with the value of -1 as indicator that no level has been assigned yet.

3. Loop over all employees and try to evaluate their level. This is easiest done in a recursive function, lets call it EvaluateLevel, which operates like this:

3a: if the manager of that employee is null (none), assign level 0.

3b: else search the name vector for the managers name and look up his level. If it is not assigned yet (i.e. -1) recursivly call the EvaluateLevel function on the manager's index. Add one to the manager's level and enter it into the employee's level field.

4. Finally loop over the level vector and find the highest value (or you do that as a side effect of the EvaluateLevel function by just remembering the highest level ever assigned).

I haven't spelled that out in C++ code, as I want to leave that as an exercise to you. Just let me know if you have any particular problem with the implementation.

[ADDED]
The recursive function would be something like this:
C++
using namespace std;

int EvaluateLevel (int idx,
    const vector<string>& names, 
    const vector<string>& managers, 
    vector<int>& levels)
{
    if (managers[idx].empty())
    {
        levels[idx] = 0;
        return 0;
    }
    if (levels[idx] != -1)
        return levels[idx];
    
    // find the index of the manager
    string manager = managers[idx];
    int managersIdx = -1;
    for (int i = 0; i < names.size(); ++i)
        if (names[i] == manager)
        {
            managersIdx = i;
            break;
        }
    // or better use std::find instead of the above loop if you are familiar
    // enough with STL

    // assign the manager's level + 1
    if (managersIdx = -1)
        return -1; // not found; error in input data
    int managersLevel = EvaluateLevel (managersIdx, names, managers, levels);
    if (managersLevel < 0)
        return -1; // recursive call returned an error
    levels[idx] = managersLevel + 1;
    return levels[idx];
}


In the next step, try to replace the three parallel vectors by a single vector of a class that describes a person, i.e.
C++
class Person
{
public:
    string name;
    string manager;
    int    level;

    //...
};

vector <Person> 
 
Share this answer
 
v4
Comments
WENzER 30-Jul-14 1:10am    
I am not able to understand the recursive call for LEVEL vector..
WENzER 31-Jul-14 0:58am    
When am passing my vectors in the EvaluateLevel my code is crashing.

Here's my code:
int EvaluateLevel (int idx,
const vector<string>& names,
const vector<string>& managers,
vector<int>& levels)
{
if (managers[idx].empty())
{
levels[idx] = 0;
return 0;
}
if (levels[idx] != -1)
return levels[idx];

// find the index of the manager
string manager = managers[idx];
int managersIdx = -1;
for (int i = 0; i < names.size(); ++i)
if (names[i] == manager)
{
managersIdx = i;
break;
}
// or better use std::find instead of the above loop if you are familiar
// enough with STL

// assign the manager's level + 1
if (managersIdx = -1)
return -1; // not found; error in input data
int managersLevel = EvaluateLevel (managersIdx, names, managers, levels);
if (managersLevel < 0)
return -1; // recursive call returned an error
levels[idx] = managersLevel + 1;
return levels[idx];
}
int main()
{
const int size =2;
string text[size] ;
vector<std::string> EmpName;
vector<std::string> MgrName;
vector<int> level;
cout << "Enput the Strings" << endl;

for(int m =0 ;m<size;m++) {="" cin="">> text[m];
}

for(int n =0;n<size;n++) {="" std::vector<std::string=""> words;
std::string::size_type beg = 0, end;
do
{
end = text[n].find(',', beg);
if (end == std::string::npos)
{
end = text[n].size();
}
words.emplace_back(text[n].substr(beg, end - beg));
beg = end + 1;
} while (beg < text[n].size());



EmpName.push_back(words[0]);
MgrName.push_back(words[2]);


}



EvaluateLevel(size,EmpName,MgrName,level);

cin.get();

}
Stefan_Lang 31-Jul-14 6:02am    
You shouldn't post code in comments, or at least take extra precautions it isn't mangled to become unreadable. I copied this to your original posting for now, and added code tags to make it readable. But next time you should do that yourself.
Stefan_Lang 31-Jul-14 6:16am    
You probably crashed because you didn't correctly check for the boss: in your inputs, the boss is indicated as "NONE", but in your code you check for an empty string. Note that checking for an empty string will never succeed, since you are only ever adding non-empty strings to your lists!
nv3 31-Jul-14 3:08am    
Two things: (1) Please paste your program code not as comment, but use the green "Improve question" button to add it to your question. And there you can use the "code" tag to have it properly formatted. In the text you posted as comment all the angle brackets got lost, so it's mor or less unreadable.

(2) Use a debugger to find the exact location where your program crashes. If you want to accomplish anything in programming you have to get used to using a debugger.
hi,
for simply comparing 2 vectors you can check this out [^]
hope it helps !
 
Share this answer
 
Comments
nv3 30-Jul-14 8:21am    
Did you read the question? The title is obviously somewhat misleading.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900