2

This is the background:

Three million men with distinct names were laid end-to-end, reaching from New York to California. Each participant was given a slip of paper on which he wrote down his own name and the name of the person immediately west of him in the line. The man at the extreme western end of the line didn't understand what to do, so he threw his paper away; the remaining 2,999,999 slips of paper were put in a huge basket and taken to the National Archives in Washington, D.C. Here the contents of the basket were shuffled completely and transferred to magnetic tapes.

I'm doing the naive solution provide by tutor, which random find a Xc, then use it search in the unordered_map, if it have this Xc as key, get the value of that key, which is the west name for nameMap1(shows below), or east name for nameMap2 search until can't find the westerly name or easterly name.

According to this, I have following code.

unique_ptr<string> temXc;

nameList1.push_back(*westName);

temXc.reset(new string);

temXc = move(westName);

bool isRunning = true;

while (isRunning==true)
{
    unordered_map<string, string>::const_iterator gotWest = nameMap1.find(*temXc);

    if (gotWest == nameMap1.end())
    {
        nameMap1.clear();
        isRunning = false;
    }
    else
    {
        temWN.reset(new string);
        *temWN = gotWest->second;

        nameList1.push_back(*temWN);
        nameMap1.erase(*temXc);

        temXc.reset(new string);
        temXc = move(temWN);
    }
}

The while loop should help me to find the most westerly name, but it didn't.

For example as the background said, the name should shows like this:

  • e,f
  • a,b
  • b,c
  • c,d
  • d,e

the first name should be the east name, another is west name.

If I randomly choose b as Xc, this while loop can give the list which is b, c, d, e, f which works fine, but if I change to 3M.txt which have 3M names, it's not working, the answer do give me a list, but after I check the 3M.txt file, I find out the last name still have a westerly neighbour, and is the first name of the list(which is Xc).

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Wenge Xu
  • 21
  • 1
  • Sorry it looks terrible, it's my first time to post the answer. – Wenge Xu Dec 30 '15 at 17:28
  • Same background as http://stackoverflow.com/questions/14574394/c-sorting-algorithm but not same issue – Wenge Xu Dec 30 '15 at 17:28
  • Could the names have been repeated? – Untitled123 Dec 30 '15 at 17:56
  • @Untitled123 I don't think so, because other student also have the same file, if the file is wrong, they should have already told the tutor, if you need a file, I can send you one. – Wenge Xu Dec 30 '15 at 20:07
  • @ Lightness Races in Orbit sorry what do you mean? – Wenge Xu Dec 30 '15 at 20:08
  • Manually create a list of around 10 members, and print out intermediate results to ensure things are going right. – Untitled123 Dec 30 '15 at 21:57
  • @Untitled123 yes I have do this, and things are going right, but when it becomes to 3M names, it's wrong – Wenge Xu Dec 30 '15 at 22:31
  • by 3M do you mean 3 million? Just find a case of 10 ish that goes wrong, try random selecting with every possible case. A big part of finding where things go wrong is reducing the problem but keeping the error. – Untitled123 Dec 30 '15 at 22:32
  • , but after I check the 3M.txt file, I find out the last name still have a westerly neighbour, and is the first name of the list(which is Xc), this seems to indicate there is a loop in our 3M.txt file... Are you meant to handle these – Untitled123 Dec 30 '15 at 22:33
  • @Untitled123 Thanks for your reply, 3M means 3 millions names in my case, sorry my question not clearly, I wanna know how to reduce error when this loop handle 3 million names, because I do find out there is something wrong in the code when I handle 3 million names, both loops are wrong. But when I using a small number of names, it shows me the right answer. – Wenge Xu Dec 30 '15 at 23:02
  • Did you test every single possible initial choice for 10 people? for 100 people? – Untitled123 Dec 30 '15 at 23:25
  • @Untitled123 yes I have try 100 names, maybe the 3M.txt have some error, I'm testing it now. Thx – Wenge Xu Dec 31 '15 at 00:50
  • @Untitled123 hah My tutor have changed the file, he siad there are error in the file, thanks anyway – Wenge Xu Jan 03 '16 at 18:16

0 Answers0