Spring 2020

PIC 10B: Intermediate Programming

Discussion Section 2A

Wonjun Lee



Week1

Tuesday

Review of class

Link to the video

Enter password:

#include < iostream >
#include < string >

using namespace std;

class Student
{
public:
    // constructor: to define the member variables
    Student(); // default constructor
    Student(const string& newname); // constructor with parameters
    Student(const string& newname, int newage, const string& newUID);

    void set_name(const string& newname); // mutator
    void set_age(int newage);

    string get_name() const; // accessor
    int get_age() const; // accessor
    string get_UID() const;
    void print() const; // accessor
private:
    string name;
    int age;
    string UID;
};

Student::Student()
{
    name = "default";
    age  = 0;
    UID  = "NOT GIVEN";
}

Student::Student(const string& newname)
{
    name = newname;
    age  = 0;
    UID  = "this is the second constructor!";   
}

Student::Student(const string& newname, int newage, const string& newUID)
{
    name = newname;
    age  = newage;
    UID  = newUID; 
}

void Student::set_name(const string& newname)
{
    name = newname;
}

void Student::set_age(int newage)
{
    age = newage;
}

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

int Student::get_age() const
{
    return age;
}

string Student::get_UID() const
{
    return UID;
}

void Student::print() const
{
    cout << "name : " << name << " age : " << age << " UID : " << UID << endl;
}

int main()
{
    Student a1("john", 16, "1231231231");

    cout << a1.get_age() << endl;
}

            

Thursday

Many problems related to vector

Link to the video

Enter password:

Exercise P6.1.

Write a function

double scalar_product(vector< double > a, vector< double > b)

that computes the scalar product of two vectors. The scalar product is

a0 b0 + a1 b1 + ... + an-1 bn−1

Solution
#include < iostream >
#include < vector >

using namespace std;

double scalar_product(vector< double > a, vector< double > b)
{
  double sum = 0;
  for(int i = 0; i < a.size(); ++i)
  {
    sum += a[i] * b[i];
  }
  return sum;
}

int main()
{
  vector< double > a = {1, 2, 3};
  vector< double > b = {2, 1, -1};

  double answer = scalar_product(a,b);

  cout << answer << endl; // Expect to print out 1
}

            
Exercise P6.2.

Write a function that computes the alternating sum of all elements in a vector. For example, if alternating_sum is called with a vector containing

1 4 9 16 9 7 4 9 11

then it computes

1 − 4 + 9 − 16 + 9 − 7 + 4 − 9 + 11 = −2

Solution
#include < iostream >
#include < vector >

using namespace std;

int alternating_sum(vector< int > a)
{
  int sum = 0;
  for(int i = 0; i < a.size(); ++i)
  {
    sum += pow(-1,i) * a[i];
  }
  return sum;
}

int main()
{
  vector< int > a = {1,4,9,16,9,7,4,9,11};

  int answer = alternating_sum(a);

  cout << answer << endl; // Expect to print out -2
}

            
Exercise P6.3.

Write a procedure reverse that reverses the sequence of elements in a vector. For example, if reverse is called with a vector containing

1 4 9 16 9 7 4 9 11

then the vector is changed to

11 9 4 7 9 16 9 4 1

Solution
#include < iostream >
#include < vector >

using namespace std;

void reverse(vector< int >&  a) // Important : I am using passing by reference here.
{
  for(int i = 0; i < a.size()/2; ++i)
  {
    int tmp = a[i];
    a[i] = a[a.size() - i];
    a[a.size() - i] = tmp;
  }
}

int main()
{
  vector< int > a = {1,4,9,16,9,7,4,9,11};

  reverse(a);

  for(int i = 0; i < a.size(); ++i){
    cout << a[i] << ", ";
  }
  cout << endl;
}

            
Exercise P6.4.

Write a function

vector< int > append(vector< int > a, vector< int > b)

that appends one vector after another. For example, if a is

1 4 9 16

and b is

9 7 4 9 11

then append returns the vector

1 4 9 16 9 7 4 9 11

Solution
#include < iostream >
#include < vector >

using namespace std;

vector< int > append(const vector< int >&  a, const vector< int >&  b)
{
  // Create a new vector c (which is empty when initialized)
  vector < int > c;

  // push back all of items in a
  for(int i=0; i < a.size(); ++i)
  {
    c.push_back(a[i]);
  }

  // push back all of items in b
  for(int i=0; i < b.size(); ++i)
  {
    c.push_back(b[i]);
  }

  return b
}

int main()
{
  vector< int > a = {1,4,9,16};
  vector< int > b = {9,7,4,9,11};

  vector< int > c = append(a, b);

  for(int i = 0; i < a.size(); ++i){
    cout << c[i] << ", ";
  }
  cout << endl;
}

            
Exercise P6.5.

Check the textbook to see the problem

Solution
#include < iostream >
#include < cmath > // We use fmax function
#include < vector > // We use vector function

using namespace std;

vector< int > merge(vector< int >& a, vector< int >& b)
{
    vector< int > c;

    int larget_num = fmax(a.size(), b.size());

    for(int i=0 ; i < larget_num; ++i)
    {
        if(i < a.size())
        {
            c.push_back(a[i]);
        }
        if(i < b.size())
        {
            c.push_back(b[i]);
        }
    }
    return c;
}

int main()
{
    vector< int > a = {1,1,1,1};
    vector< int > b = {3,3,3,3,3,3};

    vector< int > c = merge(a,b);

    for(int i=0;i < c.size(); ++i)
    {
        cout << c[i] << endl;
    }
}

            
Exercise P6.7.

Check the textbook to see the problem

Solution
#include < iostream >
#include < vector > // We use vector function

using namespace std;

bool equals(vector< int >& a, vector< int >& b)
{
    if(a.size() != b.size())
    {
        return false;
    }

    for(int i=0; i < a.size(); ++i)
    {
        if(a[i] != b[i])
        {
            return false;
        }
    }
    return true;
}

int main()
{
    vector< int > a = {1,1,1,1};
    vector< int > b = {3,3,3,3,3,3};

    bool same = equal(a,b);

    cout << same << endl; // will return false
}

            
Exercise P6.9.

Check the textbook to see the problem

Solution
#include < iostream >
#include < vector > // We use vector function

using namespace std;

bool equals(vector< int >& a, vector< int >& b)
{
    if(a.size() != b.size())
    {
        return false;
    }

    for(int i=0; i < a.size(); ++i)
    {
        if(a[i] != b[i])
        {
            return false;
        }
    }
    return true;
}

void permutation(vector< int >& a) // Here you need to put & because you need to modify a vector at the end
{
    vector< int > c; // initialize a new vector c

    for(int i=0; i < a.size(); ++i)
    {
        c.push_back(a[(i+3) % a.size()]);
    }
    a = c;
}

bool same_elements(vector< int > a, vector< int > b)
{
    // check if two vectors have the same number of items
    if(a.size() != b.size())
    {
        return false;
    }

    for(int i=0; i < a.size(); ++i)
    {
        permutation(b);
        if(equal(a,b) == true)
        {
            return true;
        }
    }
    return false;
}

int main()
{
    vector< int > a = {1,2,3,4};
    vector< int > b = {4,1,2,3};

    bool same = same_elements(a,b);

    cout << same << endl; // will return true
}

            
Exercise P6.10.

Check the textbook to see the problem

Solution
#include < iostream >
#include < vector > // We use vectors

using namespace std;

// This function will check if an integer n is in a vector a
// Suppose a = {1,2,3} and n = 2. Then check(n,a) will return true.
bool check(int n, vector< int > a)
{
    for(int i=0; i < a.size(); ++i)
    {
        // if one of items in a is equal to n then return true.
        if(a[i] == n)
        {
            return true;
        }
    }

    // if none of them equal to n then return false.
    return false;
}

void remove_duplicates(vector< int >& a)
{
    // initialzie a new vector c
    vector< int > c;

    for(int i=0; i < a.size(); ++i)
    {
        if(check(a[i], c) == false) // check if an integer a[i] is in a vector c. If it doesn't then push back to c.
        {
            c.push_back(a[i]);
        }
    }
    a = c;
}

int main()
{
    vector< int > a = {1,1,1,5,5,5,2,2,6,6,1,2,6};

    remove_duplicates(a);

    // This will print out 1, 5, 2, 6,
    for(int i=0; i < a.size(); ++i)
    {
        cout << a[i] << ", ";
    }
    cout << endl;
}