Structures with Pointer Members

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>

using namespace std;

struct Person
{
   string name;
   Person* best_friend;
};

Person* most_popular(Person* people[], int size)
{
   Person* result = nullptr;
   int result_popularity = 0;
   for (int i = 0; i < size; i++)
   {
      Person* current = people[i];
      int current_popularity = 0;
      ...
      if (current_popularity > result_popularity)
      {
         result_popularity = current_popularity;
         result = current;
      }
   }
   return result;
}


Tester.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>

using namespace std;


struct Person
{
	string name;
	Person* best_freind;
};

Person* most_popular(Person* people[], int size);

int main()
{
	Person* buddies[5];
	string names[] = { "Harry", "Sally", "Peter", "Paul", "Mary" };
	for (int i = 0; i < 5; i++)
	{
		buddies[i] = new Person;
		buddies[i]->name = names[i];
		buddies[i]->best_freind = nullptr;
	}
	buddies[0]->best_freind = buddies[1];
	buddies[1]->best_freind = buddies[0];
	buddies[2]->best_freind = buddies[0];
	buddies[3]->best_freind = buddies[4];
	buddies[4]->best_freind = buddies[3];

	Person* most_liked = most_popular(buddies, 5);
	cout << most_liked->name << endl;
	cout << "Expected: Harry" << endl;
	buddies[1]->best_freind = buddies[4];
	most_liked = most_popular(buddies, 5);
	cout << most_liked->name << endl;
	cout << "Expected: Mary" << endl;

	return 0;


}
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.



BTW

lines 23 etc: its friend, not freind
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <string>

struct Person
{
    std::string name;
    Person* best_friend;
};

Person* most_popular(Person people[], int size)
{
    int* popularity = new int[size]{0};
    std::string bf_name;
    
    // SETUP popularity ARRAY
    for (int i = 0; i < size; i++)
    {
        bf_name = people[i].best_friend -> name;
        
        // FIND THE best_friend
        for(int j = 0; j < size; j++)
        {
            if (people[j].name == bf_name)
                popularity[j]++;
        }
    }
    
    int index_max{0};
    int popularity_max{0};
    for(int i = 0; i < size; i++)
    {
        if(popularity[i] > popularity_max)
        {
            popularity_max = popularity[i];
            index_max = i;
        }
    }
    delete[] popularity;
    
    return &people[index_max];
}

#include <iostream>

int main()
{
    Person buddies[5];
    std::string names[] = { "Harry", "Sally", "Peter", "Paul", "Mary" };
    
    for (int i = 0; i < 5; i++)
    {
        buddies[i].name = names[i];
        buddies[i].best_friend = nullptr;
    }
    
    buddies[0].best_friend = &buddies[1];
    buddies[1].best_friend = &buddies[4];
    buddies[2].best_friend = &buddies[4];
    buddies[3].best_friend = &buddies[2];
    buddies[4].best_friend = &buddies[3];
    
    Person* p = most_popular(buddies, 5);
    std::cout << p->name << '\n';
    
    return 0;
}



Mary
Program ended with exit code: 0
thank you againtry,

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.
@JamesHelp

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.

Glad it was some help :)
1
2
3
4
      Person* current = people[i];
      int current_popularity = 0;
      ...
      if (current_popularity > result_popularity)

It seems that the ... should update the current_popularity to be the count of persons, who have current as their best friend.
FOR EACH person IN people
  IF person->best_friend == current
  THEN add 1 to current_popularity

The expectation of the exercise is to write a loop.

However, C++ Standard Library has std::count_if() http://www.cplusplus.com/reference/algorithm/count_if/
You can't use it, because <algorithm> is not included for you. If you could
1
2
3
4
5
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 2/08/2020
 http://www.cplusplus.com/forum/beginner/272135/#msg1173642
 Calculates and displays who are the most popular best friends
 
 */

#include <string>

struct Person
{
    std::string name;
    Person* best_friend;
    int popularity{0};
    
    void addBestFiend(Person* aBestFriend)
    {
        best_friend = aBestFriend;
        aBestFriend -> popularity++;
    }
    
    static Person* most_popular(Person* people, int size)
    {
        Person* result = nullptr;
        int max_popularity{0};
        int current_popularity{0};
        
        for(int i = 0; i < size; i++)
        {
            current_popularity = people[i].popularity;
            if( people[i].popularity > max_popularity )
            {
                max_popularity = current_popularity;
                result = &people[i];
            }
        }
        return result;
    }
};


#include <iostream>

int main()
{
    Person buddies[5];
    std::string names[] = { "Harry", "Sally", "Peter", "Paul", "Mary" };
    
    for (int i = 0; i < 5; i++)
    {
        buddies[i].name = names[i];
        buddies[i].best_friend = nullptr;
    }
    
    buddies[0].addBestFiend(&buddies[1]);
    buddies[1].addBestFiend(&buddies[4]);
    buddies[2].addBestFiend(&buddies[1]);
    buddies[3].addBestFiend(&buddies[1]);
    buddies[4].addBestFiend(&buddies[1]);
    
    std::cout << Person::most_popular(buddies, 5)->name << '\n';
    
    return 0;
}


Sally
Program ended with exit code: 0
Topic archived. No new replies allowed.