Code Quiz!

Bcm

Senior Member
Joined
Aug 2, 2005
Messages
687
Reaction score
0
Location
Cape Town
Hey guys, lets play a programming game! :cool:

Rules.
1. A programming problem is posted (I'll start)
2. 1st person to solve it with a detailed explanation, posts the next problem
3. The person who posted the problem confirms that solution is correct and passes the baton.
4. We all learn something.
 
C++ Question. Stack Overflow when run! Why?

Code:
class Base
{
public:
   Base() {}
   virtual void func() { /* do something */ }
};

class Derived : public Base
{
public:
   Derived() {}
   virtual void func()
   {
     Base:func();
     /* do something else */
   }
};

void main()
{
   Derived d;
   d.func(); // Never returns!
}
 
C++ Question. Stack Overflow when run! Why?

Code:
class Base
{
public:
   Base() {}
   virtual void func() { /* do something */ }
};

class Derived : public Base
{
public:
   Derived() {}
   virtual void func()
   {
     [b]Base::func();[/b]
     /* do something else */
   }
};

void main()
{
   Derived d;
   d.func(); // Never returns!
}

That will prevent function recursion. Cant remember why it does it thou.. someting todo with class labels..
 
Last edited:
That will prevent function recursion. Cant remember why it does it thou..

Dude, I thought you were the C++ fanboy Jedi. :confused::cool:

Ok, I'll explain...

Base:func(); is actually 2 lines
Base: -> Label called base
func(); -> Method call function

so Instead of using the correct line base::func(); which would call the correct method. The func() method is calling itself recursively until the stack overflows.

Your turn to ask a question.
 
C++ Question. Stack Overflow when run! Why?

Code:
class Base
{
public:
   Base() {}
   virtual void func() { /* do something */ }
};

class Derived : public Base
{
public:
   Derived() {}
   virtual void func()
   {
     Base:func();
     /* do something else */
   }
};

void main()
{
   Derived d;
   d.func(); // Never returns!
}

becuase you posted it... ::p
 
Dude, I thought you were the C++ fanboy Jedi. :confused::cool:

Ok, I'll explain...

Base:func(); is actually 2 lines
Base: -> Label called base
func(); -> Method call function

so Instead of using the correct line base::func(); which would call the correct method. The func() method is calling itself recursively until the stack overflows.

Your turn to ask a question.

are programmers/developers this bored... :p
 
Dude, I thought you were the C++ fanboy Jedi. :confused::cool:

Ok, I'll explain...

Base:func(); is actually 2 lines
Base: -> Label called base
func(); -> Method call function

so Instead of using the correct line base::func(); which would call the correct method. The func() method is calling itself recursively until the stack overflows.

Your turn to ask a question.

Hey i got half of it right, it was cryptic.. ok let me think of one..
 
argg ok i cant think of any.. ive only slept 3 hours today, you post more and let me try..
 
Code:
void main(int argc, char* argv[])
{
  int n = 255;
  size_t size = sizeof(++n);
  printf("%d", n);
}

what will the output of that be without running the code.. ok that wasnt obscure..
 
Code:
int main void()
{
   double x = 1.0;
   double n = 1e308;
   double d  = x/n;
   cout << "d = " d << endl;
   double e = pow(1.0+d,n);
   cout << "e = " << e;
   return 0;"
}

what is the output and why?
 
Code:
int main void()
{
   double x = 1.0;
   double n = 1e308;
   double d  = x/n;
   cout << "d = " d << endl;
   double e = pow(1.0+d,n);
   cout << "e = " << e;
   return 0;"
}

what is the output and why?

Code:
{
";0 uɹnʇǝɹ   
;ǝ << " = ǝ" << ʇnoɔ   
;(u'p+0.1)ʍod = ǝ ǝ1qnop   
;1puǝ << p " = p" << ʇnoɔ   
;u/x =  p ǝ1qnop   
;803ǝ1 = u ǝ1qnop   
;0.1 = x ǝ1qnop   
}
()pıoʌ uıɐɯ ʇuı
 
Code:
{
";0 uɹnʇǝɹ   
;ǝ << " = ǝ" << ʇnoɔ   
;(u'p+0.1)ʍod = ǝ ǝ1qnop   
;1puǝ << p " = p" << ʇnoɔ   
;u/x =  p ǝ1qnop   
;803ǝ1 = u ǝ1qnop   
;0.1 = x ǝ1qnop   
}
()pıoʌ uıɐɯ ʇuı

funny:P but no..
 
Code:
int main void()
{
   double x = 1.0;
   double n = 1e308;
   double d  = x/n;
   cout << "d = " d << endl;
   double e = pow(1.0+d,n);
   cout << "e = " << e;
   return 0;"
}

what is the output and why?


yeah, may be a noob question, but what type of function declaration is "int main void()" anyway?

And then perhaps you will get a complaint about the extra " after the return statement...
 
yeah, may be a noob question, but what type of function declaration is "int main void()" anyway?

And then perhaps you will get a complaint about the extra " after the return statement...

apart from the obvious typos:) its a 3rd year comp sci question, well i thats when i saw it.
 
yeah, may be a noob question, but what type of function declaration is "int main void()" anyway?

And then perhaps you will get a complaint about the extra " after the return statement...

And don't forget the
cout << "d = " d << endl;
should be
cout << "d = "<< d << endl; ;)
 
Ah, I was wondering - thought I was seeing things... but the clearing of my potential stupid questions and lack of many years of experience with a "noob" reference still has effect :D

I'm still curious about the whole "int main void()" reference... Maybe I missed something along the way, but where does void fit in? Perhaps someone could help me out of my misery
 
To me this is a math question:
x = 1.0;
n = 1e308 = 10^308
d = x/n
= 1.0
----
10^308
= 10^-308.
= 10e-308

e = (1.0 + 10^-308)^(10^308)
= 1.0^(10^308) + (10^-308) ^(10^308)
= 1.0 + 10^(-3080^308)
= 1.0 + 10^(-948640)

Now 10^(-948640) << 1 so we can assume 0 for this application.

e = 1.0;

QED
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X