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"

Friday, April 30, 2010

Reverse a string

Question

Write code to reverse a string in its place.

Answer

[ This basically means, that we cannot allocate extra memory for the reversed string and have to return it back in the same memory ]

To reverse a string in its place, we'll have to swap the 1st character with the nth character, then the 2nd character with the (n - 1)th character and so on ...


Corollary Question

Reverse the string in its place, without using a temporary variable.

Answer

The logic for this comes from the simple swapping technique that does not use a temporary variable.


Using the above logic we can re-write our function to reverse the string as below

Thursday, April 29, 2010

Find the box containing 9 grams gold coin

Puzzle : 


Update : This puzzle is solved now

There are 5 boxes. Each box contains 10 gold coins. 
4 of the boxes contain gold coins weighing 10 grams each. 
One of the box contains gold coins that weigh 9 grams each. 
You are allowed to open the boxes if needed. 
But, the gold coins are completely identical and the difference in their weight cannot be identified by the naked eye. 
You are given a digital weighing machine.

Find out the box that contains the 9 grams gold coins by using the weigh only once.

Solution : 


Lets label the 5 boxes as A,B,C,D and E


Now pick 1 gold coin from box A, 2 from box B, 3 from box C, 4 from box D and 5 from box E
Keep them on the weighing machine and find out the total weight.
If all the boxes contained gold coins that weighed 10 grams each, then the weight would be :

10 + 20 + 30 + 40 + 50 = 150 grams.

But, since one box contains all the gold coins weighing 9 grams, based on the box that contains this defective gold coin the measured weight would vary.

If box A contained the 9 gram gold coins, the measured weight would be :

9 + 20 + 30 + 40 + 50 = 149 grams.

Following the same logic, based on the single measured weight we can identify the box that contains 9 grams gold coin as below :

If measured weight is 149 grams, then the 9 gram gold coin is in box A.
If measured weight is 148 grams, then the 9 gram gold coin is in box B.
If measured weight is 147 grams, then the 9 gram gold coin is in box C.
If measured weight is 146 grams, then the 9 gram gold coin is in box D.
If measured weight is 145 grams, then the 9 gram gold coin is in box E.

Thursday, April 15, 2010

Introduction

Have been thinking of trying out the new Blogger templates since quite a while now. Got some time now and thought of creating this new blog, where I would be posting all the interesting FAQs and various questions, some with answers, some seeking answers :)

Lets see how far I can go with posts on this new blog of mine.