Friday, April 9, 2010

Write a C Program to reverse a stack "in place" using recursion ?

You can only use the following ADT functions on Stack:
IsEmpty
IsFull
Push
Pop
Top

#include
#include
using namespace std;
stack S;

void func(int n){
    if(S.empty())
        S.push(n);
    else{
        int t= S.top();
        S.pop();
        func(n);
        S.push(t);
    }
}

void Rec(){
    if(!S.empty()){
        int t = S.top();
        S.pop();
        Rec();
        func(t);
    }
}

void printStack(){
    if(!S.empty()){
        int t= S.top();
        S.pop();
        cout<<<",";
        printStack();
        S.push(t);
    }
    else{
        cout<
    }
  
}

int main(int argc, char* argv[])
{

    S.push(4);
    S.push(3);
    S.push(2);
    S.push(1);

    printStack();
  
    Rec();
    printStack();
  
    return 0;
}

You are given a datatype, say X in C. The requirement is to get the size of the datatype, without declaring a variable or a pointer variable of that type, And, of course without using sizeof operator !

Sol:- #define size(type) ( (int)((type *)0+1) - (int)((type *)0)) 

What can be maximum memory that a process can have in 32-bit machine? What assigns a process this memory?

What should be done when an exception occurs in the constructor?

How do you know where the memory leaks when you have multiple files?

One Way to detect the memory leaks is to check that while using Inheritance and Virtual Functions some where we have initialized baseclass pointer with the derived class object but when calling destructor the destructor that u have used in the baseclass is not declared as Virtual.
eg:
#include
using namespace std;

class Base1 {
public:
  ~Base1() { cout << "~Base1()\n"; }
};

class Derived1 : public Base1 {
public:
  ~Derived1() { cout << "~Derived1()\n"; }
};

class Base2 {
public:
  virtual ~Base2() { cout << "~Base2()\n"; }
};

class Derived2 : public Base2 {
public:
  ~Derived2() { cout << "~Derived2()\n"; }
};

int main() {
  Base1* bp = new Derived1; // Upcast
  delete bp;
  Base2* b2p = new Derived2; // Upcast
  delete b2p;
}

The output of the following program is :--
~Base1()
~Derived2()
~Base2()

In this example we can see that after declaring the destructor of class Base2 as virtual when we delete a pointer to class Base2 then first the Derived2() destructor is called first then the base class destructor. So during upcasting this can introduce a memory leak.This is the case that i know which can introduce memory leak in multiple files.(Reference: TICPP Vol1 Bruce Eckel Chapter 15(polymorphism and Virtual functions))

WAP to find the number containing all the digits (1-9, no 0) such that number till nth digit is divisible by n

wo teen women wali puzzle ka sol..

Q : Three very intelligent women (rare case :P) are sitting across a circular table. A fourth person notifies them that he will blind fold them and put either blue or red mark mark on their forehead. Then he will open their eyes. If any woman sees a red mark on the forehead of any of the women, she should raise her hand. And as soon as, one can logically deduce the mark of her forehead, she should then lower her hand, if she raised it earlier.
The fourth person then proceeds and mark all their foreheads with red mark and then open their eyes. All three raised her hand initially, after some moment, the most intelligent of the lot lower her hand.
What and how did she conclude about the mark color of her forehead?

A : Possible cases : (2 R, 1 W) and  3 R as all three initially raised their hand which caused the confusion and the corresponding delay in lowering the hands.
For (2R, 1W) case, one of the women with red mark would have thought, that since third lady has white mark, the only reason the other lady raised her hand is that she had red mark on her forehead. Hence would have lowered her hand almost instantaneously.
The most intelligent woman after analyzing all the cases, must have thought that the only reason for none of the women being able to deduce the color of her forehead instantaneously is due to the 3 R case.