Print India Map using C Program

For those who didn’t understand the topic and those who yaks, “Bro!! What’s so tough in this ?! Its sooo simple . Just do printf(“India Map”) 😛 “, lemme clear with the topic. We gonna print the following pattern.

If I ask you to draw a square/rectangle instead of India, you’ll achieve that by printing m rows of n ‘!’ (exclamatory marks).  The difficulty in finding the better solution for this problem is, the irregularity in its shape.  I am sure, the first/layman solution that’ll crop up from your mind is “printf() statements”. But, that solution may pass in SPOJ/Top Coder but surely it’s not enough to impress the interviewer, not even your friend 😛 . So, We need to think some out of box solution Before explaining how, let me give you the code which will print the map. Take some time to analyze/brainstorm with your friends, how it may work

Hats-off to the nerds who cracked what it does  You are welcome to write guest articles in this blog 😉  And, for the rest of people like me who urges, “Dude, Are you kidding me? How the hell this will print India Map?”, here is the proof that above code will work and let us see how.

As I mentioned earlier, the problem is irregularity in data and we gonna regularize it.  We are going to encode the image into a string. If you astound on reading the term “encode”, consider we gonna store the image as a string.  “Using which technique we gonna encode the data? ” will be your next question. We should encode it in such a way that it can be retrieved easily. If you go through the map, it is nothing but the combinations of consecutive spaces( ) and exclamatory marks(!) but of varying length. Our technique is very simple. Store the count of consecutive spaces and exclamatory marks. For example, you’ll store the first line of the map as 20 spaces,6 exclamatory marks, second line as 20 spaces, 10 exclamatory marks and so on. And how we’ll store the numberic data in string is by ASCII value. One restriction in storing ASCII value is the values greater than 32 is printable characters. So, if we want to store 10, store that as 42. In the code given above, the values are  upscaled by 64. There is no vital reason rather than starting the value from alphabet (‘A’ – 65).

Now the problem is less complicated than you might think of. Am I right ? 😀  Cool, We need to do some final touch up works to complete the program. “When we need to print new line character?”. If you confuse, “What’s the problem in that? Just print after spaces and exclamatory marks”, consider the 13th line which has the combination of two spaces and exclamatory marks whereas 15th line has 3 such combinations. “How to differ that from other lines?”. Instead of complicating where to print the new line character, we gonna print new line character at the end of 80 characters which we assume to be width of the image. “What if the consecutive length of either space or exclamatory mark exceeds 63?”, is the final edge case we need to handle. You might baffle,”What’s the problem if it exceeds 63?”. Since, chars can store maximum of 127 and we upscale each value by 64, you can’t store if the size exceeds 63. We gonna address that problem by using some sentinel characters i.e. i use some characters to represent zero.  Say if i need to store 98 in the string, we gonna store it as 60 spaces, 0 exclamatory and 38 spaces which is nothing but consecutive 98 spaces. If you say, “No no!! I want to store it as 49 spaces, 0 exclamatory, 49 spaces”, its your call how you want to split the number The symbols !, (, ) ,*,#,+  in the above code are used as sentinel characters. And how to vary the space and exclamatory count in the string is not at all problem, as they are alternative the characters in odd position represent space counts and even positions represent exclamatory mark counts. (Don’t confuse this with the index of the string which is vice-versa ).

Now go through the above code, you can figure out what’s being done there 😀 If you howls “Uhum,Even now I can’t understand it” , go through the non-obfuscated code given below

If you want to print any irregular shape, it’s enough to generate the encoded sequence and place it in the code.

Feel free to comment your doubts in the program #IMeantBlogCommentNotProgramComment 😛

Leave a Reply

Your email address will not be published. Required fields are marked *


© Mathan Kumar, All Rights Reserved