An Introduction to Programming Through C++ Week 9 Answers

Consider the following piece of code:
struct x {
int y;
int z;
};

Q1. Which of the following is true

a) The code creates a structure type named x.
b) The code creates a structure variable named x.
c) The code creates variables y and z.
d) The code has a syntax error.

Answer: a) The code creates a structure type named x.

Q2. What is the value of Q.x?

Answer: 60

Q3. What is the value of L2.p1.y?

Answer: 12

Q4. Which of the following statements will be syntactically incorrect following the code given above?

a) L1 = L2;
b) L1.p1 = L2;
c) L1.p2 = L2.p1;
d) L2.p2.x = L2.p1.y;

Answer: b) L1.p1 = L2;

Q5. What should blank1 be?

Answer: marklist[i].name[0]

Q6. What should blank2 be?

Answer: marklist[i].marks

Q7. What is the first number printed?

Answer: 12

Q8. What is the second number printed?

Answer: 9

Q9. Which of these lines give a compiler error:

a) Line 1, Line 2
b) Line 2, Line 3
c) Only Line 3
d) Only Line 1

Answer: c) Only Line 3

Q10. We can prevent the compiler error by:

a) Writing a constructor for the Counter class
b) Changing the access specifier for the member variable x
c) Changing the access specifier for the member function inc
d) Including iostream in the program

Answer: b) Changing the access specifier for the member variable x

Q11. Which the following is true?

a) The second statement will cause a compile-time error because the assignment operator was not defined for the class rational.
b) The second statement will cause a compile-time error because the addition operator was not defined for the class rational.
c) The second statement will cause a compile-time error for some different reason.
d) The second statement will not cause a compile-time error.

Answer: b) The second statement will cause a compile-time error because the addition operator was not defined for the class rational.

Q12. Which of the following is true?

a) The third statement will cause a compile-time error because there is no constructor for rational that does not take any arguments.
b) The third statement will cause a compile-time error because I have not defined a destructor for the rational.
c) The third statement will not cause a compile-time error.

Answer: a) The third statement will cause a compile-time error because there is no constructor for rational that does not take any arguments.

Q13. What is the first number printed by this code?
Answer: 15

Q14. What is the second number printed by this code?

Answer: 5

Q15. What is the third number printed by this code?

Answer: 16

Q16. What is the fourth number printed by this code?

Answer: 5

Programming Assignment 9.1

Define a class for storing time durations. A time duration is a triple h, m, s of non negative integers, respectively denoting the number of hours, minutes and seconds. The numbers should satisfy the natural condition that h >= 0, 60 > m >=0, and 60 > s >= 0. Define constructors that create a duration given arguments h, m, s, and also a constructor with no arguments which creates a duration of 0 hours 0 minutes 0 seconds. It should be possible to add durations but in the result duration you should ensure that the natural condition is satisfied. Similarly it should be possible to multiply a duration D by a non negative integer I, this should result in a duration that is I time as large as D. Again the result should satisfy the natural constraints. Thus 4 times 1 hour 30 minutes 23 seconds should give the duration 6 hours 1 minute 32 seconds. Finally the class should support a member function print which prints h,m,s i.e. only with intervening commas and no spaces nor new lines.

Solution:

class Duration
{
    int hour,min,sec;
    public:
    Duration(int h,int m,int s)
    {
       hour=h;
       min=m;
       sec=s;
    }
    Duration()
    {
        hour=0;
        min=0;
        sec=0;
    }
    void format()
    {
        min=min+sec/60;
        hour=hour+min/60;
        sec=sec%60;
        min=min%60;
    }
    Duration operator +(Duration &d)
    {
        Duration t;
        t.sec = sec + d.sec;
        t.min = min + d.min;
        t.hour = hour + d.hour;
        t.format();
        return (t);
    }
    Duration operator *(int i)
    {
      Duration t;
      t.sec=sec*i;
      t.min=min*i;
      t.hour=hour*i;
      t.format();
      return(t);
    }
    void print()
    {
        cout<<hour<<","<<min<<","<<sec;
    }
};

Programming Assignment 9.2

Rectangles whose sides are parallel to the coordinate axes can be represented by specifying the coordinates of diagonally opposite vertices.Suppose the southwest and northeast corners are (x1, y1) and (x3, y3). Clearly these 4 numbers determine the other two corners as well:they will be (x1, y3) and(x3 , y1). Define a struct Rect with integer members x1, y1, x3, y3. Define a function intersect which takes 2rectangles as arguments and returns a rectangle that is the intersection of the area spanned by the two rectangles. If the intersection is empty thenthe returned rectangle should have all coordinates 0. Note that, by “rectangle” we are only talking about the inside of the rectangle withoutincluding the boundary. So if two rectangles were to touch just at one edge, then their intersection is still empty.
We have already entered the main program given below. You have to first enter the structure definition, and then the function intersect.

Solution:

struct Rect
{
int x1, y1, x3, y3;
};
struct Rect intersect(struct Rect r1, struct Rect r2)
{
struct Rect r;
if((r1.x1 >= r2.x3) || (r2.x1 >= r1.x3) || (r1.y1 >= r2.y3) || (r2.y1 >= r1.y3))
{
r.x1 = r.x3 = r.y1 = r.y3 = 0;
}
else
{
if(r1.x1 > r2.x1)
r.x1 = r1.x1;
else
r.x1 = r2.x1;
if(r1.y1 > r2.y1)
r.y1 = r1.y1;
else
r.y1 = r2.y1;
if(r1.x3 < r2.x3)
r.x3 = r1.x3;
else
r.x3 = r2.x3;
if(r1.y3 < r2.y3)
r.y3 = r1.y3;
else
r.y3 = r2.y3;

}
return (r);
} 
The content uploaded on this website is for reference purposes only. Please do it yourself first.