0% found this document useful (0 votes)
18 views2 pages

CS200 Quiz6

The document is a quiz with two programming questions. The first question asks the student to identify the output of a recursive function call. The answer is that the output is "Garbage/Unknown value" because the global variable y is uninitialized, causing infinite recursion. The second question asks the student to overload the subtraction and insertion operators for a Point class. It provides hints on calculating Manhattan distance and printing points. The student is asked to modify the given Point class to include these operator overloads without writing a main function.

Uploaded by

Ammar Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

CS200 Quiz6

The document is a quiz with two programming questions. The first question asks the student to identify the output of a recursive function call. The answer is that the output is "Garbage/Unknown value" because the global variable y is uninitialized, causing infinite recursion. The second question asks the student to overload the subtraction and insertion operators for a Point class. It provides hints on calculating Manhattan distance and printing points. The student is asked to modify the given Point class to include these operator overloads without writing a main function.

Uploaded by

Ammar Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS200 : Introduction to Programming Quiz 6

Time Allowed: 15 minutes Total Marks: 10


University ID: Name:
No extra sheets will be provided for any case. Please stop writing when you are asked
to do so. Make sure to write your name and Roll-Number to avoid any consequences.

1. Write the output of the following code: (3 marks)

#include <iostream>
using namespace std;
int y;
void p(int x){
x = y - 1;
y+= ++y*(x--);
cout << x << " " << y << endl;
if(x<2)
p(--x+2);
cout << x << " " << y << endl;
}
int main()
{
int x = 1, y = 2;
p(x++*--x/++x);
cout << x << " " << y <<endl;
return 0;
}

Output:

Garbage/Unknown value stored in the global variable y caused an infinite recursion. The
fact that we are declaring the variable y locally once more in the main function and placing
the value 2 there in the local y(scope of local y is limited within the main function only). Global
variable y is uninitialised and it will be storing some unknown/garbage value.

1
2. Modify the following Point class to overload binary ‘-’ and stream insertion ‘<<’ operators.
The Point class represents a two-dimensional point having x and y coordinates. The overloaded
binary ‘-’ operator should return manhattan distance between two points. The overloaded
insertion ‘<<’ operator should print a point in the following format: (x, y) = (0, 1)
Moreover, it must be possible to print multiple points in a single statement. You don’t need
to write any code for main function! (4 + 3 = 7 marks)
Hint: manhattan(p1, p2) = | p1.x - p2.x | + | p1.y - p2.y | Here | is the sign of absolute
value. The absolute value of a negative number is that number multiplied by -1,
but the absolute value of positive numbers and zero is that number itself.
#include <iostream>
#include <cstdlib> //helps you to use abs(number) where required.
using namespace std;
class Point{
private:
int x,y;
public:
Point(int a,int b){
x=a,y=b;
}

// write code for overloaded binary '-' operator


int operator - (Point p2){
Point temp(0,0);
/*if someone has written Point temp; then it's wrong because default constructor is
not there in the class. To resolve this error, you need to define a default
constructor because when you are creating an object temp, it will try to call the
default constructor(constructor without arguments). Otherwise an error will be
generated because your code is unable to find the default constructor!*/
temp.x=(x>p2.x)?(x-p2.x):(p2.x-x); //if used abs(), then also fine
temp.y=(y>p2.y)?(y-p2.y):(p2.y-y);
return temp.x+temp.y;
}
// write code for overloaded stream insertion '<<' operator
friend ostream& operator << (ostream& output, Point p){
output<<"(x,y)=("<<p.x<<","<<p.y<<")";
return output;
}
};
//main wasn't required, I'm just giving it so you can test the provided solution!
int main(){
Point a(4,-5),b(-9,2);
cout<<"The points are: "<<a<<" and "<<b<<endl;
cout<<"Manhattan distance is : "<< b-a<<endl;
}

You might also like