CS200 Quiz6
CS200 Quiz6
#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;
}