Thursday, February 24, 2011

Boolean

Here's a very simple program with standard questions :)

Question 1> : Is there anything wrong with this code ?
Question 2> : Will this code compile ?
Question 3> : Are there any specific options that I should give for compiling it ?
Question 4> : What is the output of this program ?



Thursday, November 4, 2010

Excel formula not updating

I came across this strange problem where all of a sudden my excel sheet suddenly stopped calculating the formulas on its own. I had to click the "Save" button to make excel do the calculations.

Normally, I enter a value in a cell and expect the value in another cell to be automatically updated based on the formula present in it. It was frustrating to find that this suddenly stopped working !!!

A simple google gave me this link.

http://www.techsupportforum.com/microsoft-support/microsoft-office-support/163598-solved-excel-formula-not-updating.html

And it saved my day :)

Pressing F9 would make excel do the calculations without me clicking on "Save" button.
But, an even better thing was to change the setting under Tools-> Options-> Calculations to "Automatic" .. and vroom the calculations starting getting updated "automatically" as earlier.

I have no idea how I ended up in this but seeing it work back make my day !!!

Wednesday, June 9, 2010

[ Solved ] Why does cin not wait for Enter ?

Question

When I compile and execute the following C++ program, I find it strange that the program does not wait for the user to hit 'Enter'. What could be the possible explanation for this behaviour ? How can we solve this anamoly ?


#include <iostream>

using namespace std;

int main()
{
 string key;

 cout << "Enter a word\n";
 cin >> key;
 cout << "You entered : " [[ key [[ "\n";
 cout << "Hit Enter to proceed...";
 key = cin.get();

 if (key == "\n")
  cout << "Exit\n";
 else
  cout << "Error\n";

 return 0;
}
The output of the program is something like this


# ./wait 
Enter a word
india
You entered : india
Hit Enter to be proceed...Exit
# 
Answer

Before writing the answer, it makes more sense to understand the problem. When you read input from the keyboard, the input is read through an input stream. The behaviour of this stream is to capture keyboard input and store it in an internal buffer. The stream will read from keyboard input, only after this internal buffer is empty.

When we entered the word "india" and pressed "Enter", the characters 'i','n','d','i','a','\n' were stored in the internal buffer. cin.get() only reads up to the delimiting character, viz., '\n' by default. [ More on this at : http://www.cplusplus.com/reference/istream/istream/get/ ].

So, the initial value in the string "key" was "india", but the input buffer still contained the trailing character '\n'. When we did a cin.get(); on line 13, we expected cin to read from keyboard input. But, instead it is the trailing character in the input buffer that is read by cin. And the program never prompted for user input.

To force the program to prompt for user input, we need to clear [ or eat ] the trailing contents in the input buffer. Remember that flush() is applicable only on output streams. So, it won't solve the problem here. We have to explicitly read the trailing characters in the input buffer. Keep in mind that we do not know how many characters are still remaining in the input buffer.

If the user has only entered the "india" and pressed "Enter" [ assuming he is a very obedient user :-) ] the simplest solution is to add the following line before line 12.
string tmp = cin.get();

The new code would be :

#include <iostream>

using namespace std;

int main()
{
 string key;

 cout << "Enter a word\n";
 cin >> key;
 cout << "You entered : " [[ key [[ "\n";
 string tmp = cin.get(); // This eats up the trailing '\n' still remaining in the input buffer
 cout << "Hit Enter to proceed...";
 key = cin.get();

 if (key == "\n")
  cout << "Exit\n";
 else
  cout << "Error\n";

 return 0;
}

And the new output behaves as below :

#./wait 
Enter a word
peace
You entered : peace
Hit Enter to proceed...
Exit
#./wait
Enter a word
more peace
You entered : more
Hit Enter to proceed...Error

As expected, this works only if the user enters one word. If he enters more than one word, this fails. And since we do not know what characters the user would input, and we need to program for the worst possible case, a more generic and proper fix is to bite into the entire input buffer and junk it. The complete fix would be like this one below :

#include <iostream>

using namespace std;

int main()
{
 string key;

 cout << "Enter a word\n";
 cin >> key;
 cout << "You entered : " [[ key [[ "\n";
 cin.clear();
 cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
 cout << "Hit Enter to proceed...";
 key = cin.get();

 if (key == "\n")
  cout << "Exit\n";
 else
  cout << "Error\n";

 return 0;
}

The cin.clear() : only clears the internal stream's error state flags [ and not the input buffer !!! ] [ More info : http://www.cplusplus.com/reference/ios/ios/clear/ ]

The cin.ignore() : extracts characters from the input stream and junks them till it has extracted the maximum number of input characters, or the '\n' character.

The output now is :

#./wait
Enter a word
world
You entered : world
Hit Enter to proceed...
Exit
#./wait
Enter a word
world cup
You entered : world
Hit Enter to proceed...
Exit
#./wait
Enter a word
2014 world cup
You entered : 2014
Hit Enter to proceed...
Exit

Enjoy :-)

Monday, June 7, 2010

Unnamed Namespace

Question

Do you think the following C++ program will compile ? If yes, then will it execute without any errors ? It yes, then what is the output on the screen ?


Answer

Tuesday, June 1, 2010

Find the value of the modified string

This is my favorite question. Its so much fun to listen to all the different answers and their logical explanations :)

Question

If my C program contains only the following 2 statements ( inside main function ofcourse !! ), then what is the value of the string pointed by "x", after the execution of these 2 statements.


Answer


Please follow this link to post your answer. I shall update this post with the correct answer in about a week's time.

Friday, May 28, 2010

(x-a)(x-b)(x-c)....(x-z)

Update : This puzzle is now solved

I consider this to be one of the more silly puzzle questions :p

Question

What is the value of this multiplication :

(x-a)(x-b)(x-c).....(x-z)

Answer

As rightly pointed out by Ajay and Kalpana, the answer is "zero".

The reason being, x-x = 0

Thursday, May 20, 2010

Delete any node in the middle of a linked list

Question

Given a pointer to a node in the middle of a linked list, how will you delete the present node ?

Answer
  
  The trick is in using simple pointer tricks. The "present node" should now point to the "next node". Make sure you delete the data in the "present node"