Select Page

1. For this problem, you will be asked to write some code to accomplish a particular task given the code
fragment below. Each task will depend on the tasks that came before it. Your code must be syntactically
correct.int i1 = 100;
int i2 = -8;
int i3 = 15;

int *p1, **p2, ***p3;
a.) Set p1 to point to i1.
b.) Using p2 change the value of i1 to the value of i2 (you may not use i1).
c.) Using p3 make p1 point to i3 (you may not use p1). 2. Consider the code fragment below. It is supposed to construct a 3×4 (3 rows 4 columns) 2-level array of
integers and set each value to zero. However, as given it does not. Add the proper dereferences (*) or
references (&) to make this code work properly:int rows, col1, col2, col3;
rows = new int ; // Create 3 pointers to columns
col1 = new int ; // Create first row with 4 elements
col2 = new int ; // Create second row with 4 elements
col3 = new int ; // Create third row with 4 elements
( rows+0)= col1; //Pointtofirstrow
( rows+1)= col2; //Pointtosecondrow
( rows+2)= col3; //Pointtothirdrow
( rows+0)= col1; //Pointtofirstrow
( rows+1)= col2; //Pointtosecondrow
( rows+2)= col3; //Pointtothirdrow
for (int i = 0; i<3; i++) for (int j = 0; j<4; j++) ( ( rows+i)+j) = 0; //rows[i][j]=0; 3. For this problem, you will be asked to write some code to accomplish a particular task given the code fragment below. Each task may depend on the tasks that came before it. Your code must be syntactically correct. Note none of your operations may affect S::m_unique. class S { public: S(int init, int id) :m_num(init) { m_unique = new int(id);} S() :m_num(0) { m_unique = new int(-1);} ~S() { delete m_unique; } ... void set(int num) { m_num = num;} int get() { return m_num; } private: int m_num; int *m_unique; }; S d1, d2(4,2), d3(-15,3); S *sp1, **sp2, ***sp3;a) Set sp1 to point to d1. b) Using sp2 change the value of m_num in d1 to the value of m_num in d2 (you may not use d1). c) Using sp3 make sp1 point to d3 (you may not use sp1). 4. Referring to the object S in previous problem: a) Implement a copy constructor for the object S. Assume this is done inside the class definition. b) Overload the assignment operator for the object S. Assume this is done inside the class definition.