I'm up the chapter Structures with Pointer Members. I'm finding pointers to be very confusing.
Anyway they have a "complete this program" section which i'm confused as to what they want. They give you the code and then they want you to fill in the "..."
Complete this function that yields the person who is befriended by the most people.people.cpp
1.You have a list of Person's and their best friend, all stored in the buddies[] array.
2. They are all finds of each other and each has a unique identifier which is the index number in the buddies array.
3. So, "the person who is befriended by the most people" is the Person who is a best_friend, and who appears as the best_friend of the buddies the most number of times.
3. So take each best_friend and score +1 if they are a friend.
4. The one with the highest score is the answer.
You'll probably need a second array to keep track of the scores.
Yes, the instructions seem a bit vague. They want you to choose the person who is most recurring as a best friend. Harry is the answer in the example since element 1 and 2 both have Harry as a best friend. That's 2 occurrences of Henry compared to only 1 occurrence of Sally, Paul, and Mary (elements 1, 3, and 4 in the string array).
You only need to go through the given array in the function and keep track of who comes up most often, then you'll return a that object as a pointer.
@OP I can see from your other post elsewhere, and from this one you are experiencing many 'uncertainties'. This is how my suggestion above translate to code.
I couldn't use your code in the "fill in the blank" exercise because its runs over a server and they would not let me change much, just insert lines of code. However i played around with your code in visual studio and it actually taught me allot, that i was able to figure out what they wanted int the exercise.
Yep, I saw that you had to fill in a few lines but obviously had/have no knowledge of your server, and any simple lines required were somewhat obscure.
So I 'did what I did' given the other matters you raised.
Person* current = people[i];
int current_popularity = std::count_if( people, people + size,
[current](Person* p){ return p->best_friend == current; } );
if (current_popularity > result_popularity)
The [current](Person* p){return p->best_friend == current;} is a lambda closure. A nameless function defined inline. Lambdas were added to C++ in C++11.
While the use of algorithm would bring focus to the fact that we are counting and what is being counted, a simple loop is more than sufficient here.
After surfing around various websites it became apparent that each Person is supposed to have an added 'popularity' member. This results in a probable solution requiring the addition of a few (almost trivial) missing lines. But there again who knows for sure.