Spring 2020

PIC 10B: Intermediate Programming

Discussion Section 2A

Wonjun Lee



Week2

Tuesday

About pointers

Link to the video

Enter password:

#include < iostream >

using namespace std;

int main()
{
  int a = 54;
  int b = a;

  cout << a << " " << b << endl; // will print out 54 54

  b = 12;

  cout << a << " " << b << endl; // will print out 54 12 -> change of b doesn't affect a
}
            
#include < iostream >

using namespace std;

int main()
{
  int a = 54;
  int *b = & a; // b is a pointer pointing to a

  cout << a << " " << *b << endl; // will print out 54 54

  *b = 12;

  cout << a << " " << b << endl; // will print out 12 12 -> change of b affects a
}
            
#include < iostream >

using namespace std;


double maximum(const double* a, int a_size)
{
  if(a_size==0) return 0;

  double highest = *a;
  const double* p = a+1;
  int count = a_size - 1;

  while(count > 0)
  {
    if(*p > highest) highest = *p;

    p++;
    count--;
  }
  return highest;
}

int main()
{
  int a_size = 10;
  double a[] = {2,4,6,8,10,1,3,5,7,9};

  // This for loop will print out all the items in an array a
  for(int i = 0; i < a_size; ++i)
  {
    cout << *(a+i) << " ";
  }
  cout << endl;
  
  cout << "The maximum is " << maximum(a,a_size) << endl; // will print out 10
}
            

Thursday

Problems from Chapter 7: Pointers

Link to the video

Enter password:

Exercise P7.1 from the textbook

Implement a class Person with the following fields:

  • the name
  • a pointer to the person's best friend (a Person*)
  • a popularity counter that indicates how many other people have this person as their best friend

Write a program that reads in a list of names, allocates a new Person for each of them, and stores them in a vector<Person*>. Then ask the name of the best friend for each of the Person objects. Locate the object matching the friend's name and call a set_best_friend member function to update the pointer and counter. Finally, print out all Person objects, listing the name, best friend, and popularity counter for each.


Sample Output
formula random
Solution
#include < iostream >
#include < vector >
#include < string >

using namespace std;

class Person
{
public:
  Person();
  Person(string pname);

  string get_name() const;
  Person* get_best_friend() const;
  int get_popularity() const;

  void set_best_friend(Person* someone);
  void add_popularity();

private:
  string name;
  Person* best_friend;
  int popularity;
};

Person::Person():name(""), best_friend(NULL), popularity(0) {}

Person::Person(string pname):name(pname), best_friend(NULL), popularity(0) {}

string Person::get_name() const
{
  return name;
}

Person* Person::get_best_friend() const
{
  return best_friend;
}

int Person::get_popularity() const
{
  return popularity;
}

void Person::add_popularity()
{
  popularity++;
}

void Person::set_best_friend(Person* someone)
{
  someone->add_popularity();
  best_friend = someone;
}

int main()
{
  vector< Person* > person_list;

  while(true)
  {
    cout << "Type name (Q to quit) : ";
    string name;
    cin >> name;

    if(name == "Q") break;

    Person* new_person = new Person(name);
    person_list.push_back(new_person);
  }

  for(int i = 0; i < person_list.size(); ++i)
  {
    cout << person_list[i]->get_name() << "'s best friend? : ";
    string name;
    cin >> name;

    for(int j = 0; j < person_list.size(); ++j)
    {
      if(name == person_list[j]->get_name())
      {
        person_list[i]->set_best_friend(person_list[j]);
        break;
      }
    }
  }

  for(int i = 0; i < person_list.size(); ++i)
  {
    string name = person_list[i]->get_name();
    string best_friend_name = person_list[i]->get_best_friend()->get_name();
    int popularity = person_list[i]->get_popularity();
    cout << name << "'s best friend is " << best_friend_name << " and popularity is " << popularity << endl;
  }

  // Delete pointers
  for(int i = 0; i < person_list.size(); ++i)
  {
    delete person_list[i];
  }

}