C,C++/JAVA/BASH/ASM ARENA

वह प्रदीप जो दीख रहा है झिलमिल दूर नही है थक कर बैठ गये क्या भाई मन्जिल दूर नही है चिन्गारी बन गयी लहू की बून्द गिरी जो पग से चमक रहे पीछे मुड देखो चरण-चिनह जगमग से बाकी होश तभी तक, जब तक जलता तूर नही है थक कर बैठ गये क्या भाई मन्जिल दूर नही है अपनी हड्डी की मशाल से हृदय चीरते तम का, सारी रात चले तुम दुख झेलते कुलिश का। एक खेय है शेष, किसी विध पार उसे कर जाओ; वह देखो, उस पार चमकता है मन्दिर प्रियतम का। आकर इतना पास फिरे, वह सच्चा शूर नहीं है; थककर बैठ गये क्या भाई! मंज़िल दूर नहीं है। दिशा दीप्त हो उठी प्राप्त कर पुण्य-प्रकाश तुम्हारा, लिखा जा चुका अनल-अक्षरों में इतिहास तुम्हारा। जिस मिट्टी ने लहू पिया, वह फूल खिलाएगी ही, अम्बर पर घन बन छाएगा ही उच्छ्वास तुम्हारा। और अधिक ले जाँच, देवता इतन क्रूर नहीं है। थककर बैठ गये क्या भाई! मंज़िल दूर नहीं है।

Does your VPN gets disconnected and connected continuosly? One solution… May 1, 2014

Filed under: Uncategorized — whoami @ 14:46
Tags: , ,

Sometimes i work from home and one night i found that i am not able to work. Why? Its because my VPN ( Cisco anyconnect) was getting regularly connected and disconnected. Because of this i have to go office next day. It was happening when i am in my WIFI network and VPN was used. The problem does not seem to be there when WIFI was different. 

Then i found some solution w.r.t Cisco Anyconnect on forum and this solution was related to MTU(Maximum Transmission Unit). On windows after executing what suggested on the link. Got following outputs and as suggested look at the MTU value of one the interface and you can easily understand this can be problem root cause.

 

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\anurag>netsh interface ipv4 show subinterfaces

   MTU  MediaSenseState   Bytes In  Bytes Out  Interface
——  —————  ———  ———  ————-
4294967295                1          0     655724  Loopback Pseudo-Interface 1 ->>>>>> see the MTU value, its too large
  1500                1    4354621    1039320  Wireless Network Connection

C:\Users\anurag>netsh interface ipv4 show subinterfaces

   MTU  MediaSenseState   Bytes In  Bytes Out  Interface
——  —————  ———  ———  ————-
4294967295                1          0     655724  Loopback Pseudo-Interface 1
  1500                1    4356437    1040016  Wireless Network Connection

C:\Users\anurag>netsh interface ipv4 show subinterfaces

   MTU  MediaSenseState   Bytes In  Bytes Out  Interface
——  —————  ———  ———  ————-
4294967295                1          0     674120  Loopback Pseudo-Interface 1
  1500                1    4484953    1146481  Wireless Network Connection

C:\Users\anurag>netsh interface ipv4 set subinterface “loopback pseudo-interface 1″ mtu=1273 store=persistent
Ok.

C:\Users\anurag>netsh interface ipv4 show subinterfaces

   MTU  MediaSenseState   Bytes In  Bytes Out  Interface
——  —————  ———  ———  ————-
  1273                1          0     680252  Loopback Pseudo-Interface 1 ->>>>> reset to 1273.
  1500                1    4741288    1440863  Wireless Network Connection

So resetting the MTU value to some value say 1273 or even 1500 was the solution and this way i could work from home following day.

Thanks to the forum otherwise i would have needed to ask IT to help me !

Hope anyone facing similar problem try this solution. 🙂

 

 

Static Scoping vs Dynamic Scoping November 26, 2013

Filed under: C,C++ Programs,Coding,Compiler — whoami @ 12:33

Many C programmers might not have heard the term ‘Static Scoping’ and ‘Dynamic Scoping’. Since C only supports Static Scoping. Static Scoping is also called Lexical/Text Scoping.

Read the following article to understand what these two scoping mean in Computer Science.

1.  Article 1

2. Article 2

3. Article 3

The above things might confuse since this dynamic scoping is not supported in many languages including C and hence you might not be able to test the same. But the point is to understand it. What Dynamic Scoping means.

 

 

 

 

 

Shortcut icon problem in Windows7 and fix November 22, 2013

Filed under: Uncategorized — whoami @ 18:39
Tags: , ,

Shortcut icon problem in Windows7 and fix

Today, on my laptop with Windows7 found a weird problem.

I was trying to run any program. The windows7 asked on how/from which program to open it. I clicked wrongly ‘wireshark’ and forgot to uncheck the ‘Always use the selected program to open this kind of file‘ and suddenly all the shortcut and startmenu icons changed to of type ‘wireshark’. So my mozilla firefox icon was of wirehsark, notepad start menu icon was of type wireshark and so on…..When i tried opening mozilla firefox it couldn’t. This was a big headache for me. I Started trying many things by searching web what is the problem and solution but nothing helped. Some sight suggested to delete old iconcache.db and recreate it using explorer.exe, but it didn’t help.

After almost wasting 2-3 Hrs, finally raised IT case to my department.

But when i came to home, again started searching for any solution, since it’s Friday today and IT case will come into action on Monday. And yeah guess what , finally one of the web browsing resolved my issue 🙂

Here is the solution.

In the above line, 2nd part of solution helped me. It was a registry related program that i downloaded and ran and it may have modified registry to make everything working 🙂

Issue resolved before it could be read be read by IT guy. So Relaxed Now ( Since i was fearing this issue might lead to reinstalling windows and then reinstalling and reconfiguring many essential programs )

Thanks to Aibek ! who posted that solution.

Please be careful while selecting any program to open a file/program and check/uncheck to apply.

————————————————————–

Problem snaps

icon_problem

This was the real culprit – i forgot to uncheck it

culprit

 

Adobe Written Test Questions October 27, 2013

Today Adobe took written test for C++ development(1-3 Yrs) @Bangalore on 27th Oct 2013. This was taken  by Elitmus.The test consisted of 8 C++ questions(Objective) and 2 Algo/Ds questions. I am posting my approach of Algo/DS questions.
C++ questions were mostly of OOPS type e.g classes, constructor, inheritance, public, private, protected concepts. Some programs were given in c++ and we needed to identify the problem/aim and choose correct option. As i don’t remember those questions, i am not posting those.

Question1 – Ideone Link

Question2 – Ideone Link

Update

Ooops a friend suggested these questions were exactly from geeksforgeeks

Question1

Question2


/*Adobe Elitmus Written Test on 27-10-2013  @Bangalore - Algo/DS questions
 *Question 1
 */

/* Reverse every alterante K elements of a list
 * It is to be noted that Nodes have to reversed and not the values
 * e.g. if k=3 and list is
 * Input 1->2->3->4->5->6->7->8->9->NULL
 * Output 3->2->1->4->5->6->9->8->7->NULL
 *
*/

#include<stdio.h>
#include<stdlib.h>

typedef struct SLL{
	int element;
	struct SLL *next;
}Node;

Node *start=NULL;

void InsertNode(Node *start, int item)
{
	Node *tmp=start;

	if(tmp == NULL)
	{
		printf("Error : Start pointer is NUll\n");
		return;
	}

	//construct a node with item element
	Node *node=malloc(sizeof(Node));
	node->next=NULL;
	node->element=item;

	while(tmp->next !=NULL) tmp=tmp->next;

	tmp->next=node; //node inerted at end
}

void PrintListElements(Node *start)
{
	Node *tmp=start;

	while(tmp != NULL)
	{
		printf("%d->",tmp->element);
		tmp=tmp->next;
	}
		printf("NULL\n");
}

void ReverseAlternateKElements(Node *start, int K, Node **startPointer)
{
	int i,j,count;
	Node *current=start;
	Node *prev=NULL,*prev2=NULL;
	Node *save=NULL;
	Node *next=NULL;
	Node *tmp=NULL;

	for(i=0;;)
	{
		if(current == NULL) break;
		if(i%K==0)
		{
			count=0;
			save=current;
			//printf("current=%d\n",current->element);
			for(j=0;j<K;)
			{
				if(current == NULL) break;
				next=current->next;
				current->next=prev;
				prev=current;
				current=next;
				j++;
				++count;
			}
			if(i<K)
			{
				*startPointer=prev;
				tmp=prev;
			}
			save->next=current; // for last node of the series to point to next current;

			if(prev2 != NULL)
				prev2->next=prev;
			if(current == NULL) break;
			//PrintListElements(tmp);
			i=i+count+1;

		}
		else
		{

			current = current->next;
			if(current == NULL) break;
			i++;
			if(i%K==0)
			{
				prev=current;
				prev2=current;
				current=current->next;
			}
		}
	}

	printf("----Reversal Done!!! :)\n");
	//PrintListElements(tmp);

}

int main()
{
	int K=4; //Input
	start=malloc(sizeof(Node));

	start->next=NULL;
	start->element=1; // Inputs
	InsertNode(start,2); //Inputs .....
	InsertNode(start,3);
	InsertNode(start,4);
	InsertNode(start,5);
	InsertNode(start,6);
	InsertNode(start,7);
	InsertNode(start,8);
	InsertNode(start,9);
	InsertNode(start,10);
	InsertNode(start,11);
	InsertNode(start,12);
	InsertNode(start,13);
	InsertNode(start,14);

	printf("Element Before Reversal:\n");
	PrintListElements(start);

	ReverseAlternateKElements(start, K, &start);

	printf("Element After Reversal:\n");
	PrintListElements(start);

	return 0;
}

 

/*Adobe Elitmus Written Test on 27-10-2013  @Bangalore - Algo/DS questions
 *Question 2
 */

/*
 * In a given set of elements(any order), find  K largest numbers
 * e.g arr[]={1,4,5,108,10,14,90,43,100} let K=3
 * Ouput = {108,100,90}
 */

/*Approach: Modified Bubble Sort
 * As we know that bubble sort in Ith pass places the ith largest number at last
 * After Kth pass, the results will be available and no need to sort further
 */

#include<stdio.h>
#include<stdlib.h>

//Its Modified Bubble sort
void FindKLargestNumbers(int arr[], int n, int K, int output[])
{
	int i,j,count,tmp;

	for(i=0;i<n;i++)
	{
		for(j=0;j<n-1;j++)
		{
			if(arr[j]>arr[j+1])
			{
				tmp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=tmp;
			}
		}
		if(i==K) break; // K largest numbers are bubbled at top(last indices)
	}

	count=0;
	for(i=n-1;i>=n-K;i--)
	{
		output[count++]=arr[i];
	}

}

void PrintKLargestNuners(int output[] ,int K)
{
	int i;

	printf("The K=%d largest numbers in the given array are\n",K);
	for(i=0;i<K;i++)
	{
		printf("%d ",output[i]);
	}
	printf("\n");
}

int main()
{
	int n=9;//no of elements in the array
	int K=3;//largest K numbers
	int arr[9]={1,4,5,108,10,14,90,43,100};
	int output[3];//output array

	FindKLargestNumbers(arr,n,K,output);

	PrintKLargestNuners(output,K);

	return 0;
}

 

Got amazed by the exppression &(((struct st *) 0) ->member_st). But finally found this is valid and C has similar implementation for ‘offsetof’ September 16, 2013

Filed under: C,C Library,C++ Programs,Coding,Compiler,Computers — whoami @ 20:23

Some day back i was going through some library code in C and found expression like

 &(((some_struct_type *) 0) -> some_element_of_some_struct_type);

And started wondering what will happen to access an element located at NULL. Thought something is wrong with the code then one of my senior suggested suggested this is Ok and then found lot of information on the web. Also the same feature is available in C as MACRO offsetof.

1. Stackoverflow

2. Stackoverflow

3. C wiki

4. C examples

 

Conclusion:

* The expression ((some_struct_type *) 0) -> some_element_of_some_struct_type is invalid/undefined because we are trying to access element of some structure which is not correctly located at defined address. But the expression prefix with & becomes valid because the whole expression of getting address of member element of struct (assumed to be located at NULL) is calculated at compile time and hence dereferencing is avoided.

 

 

Project Euler Problem 17 – Number letter counts August 8, 2013

Filed under: Project Euler — whoami @ 19:56
Tags: ,

/* Warning: Donot try extracting answer from this and sumbit. This will not help you in Long Run */


/* Project Euler Problem 17 */
/*

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
letters. The use of "and" when writing out numbers is in compliance with British usage.
*/

#include<stdio.h>
#include<string.h>

int main()
{

int i,j;
char str[100];

char str2[][100]={"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred",
"thousand"};
int tot=0;


for(i=1;i<1000;i++)
{
//one digit number 1-9
if(i/10 == 0)
{
tot+=strlen(str2[i-1]);
printf("%s\n",str2[i-1]);

}
//two digit number 10-99
else if(i/100 == 0)
{
if(i>=10 && i<=19)
{
tot+=strlen(str2[i-1]);
printf("%s\n",str2[i-1]);
}
else if( i%10 == 0 )
{
tot+=strlen(str2[18+i/10-1]);
printf("%s\n",str2[18+i/10-1]);
}
else
{
int unit=i%10;
int tens=(i/10)%10;
tot=tot+strlen(str2[unit-1]) + strlen(str2[18+tens-1]);
printf("%s-%s\n",str2[18+tens-1], str2[unit-1]);
}

}
//three digit number 100-999
else if(i/1000 == 0)
{
tot+=strlen("hundred");
if(i%100 == 0)
{
tot= tot + strlen(str2[i/100-1]);
printf("%s hundred\n",str2[i/100-1] );
}
else if(i%10 == 0)
{
int hundredth=i/100;
int tens=((i/10)%10);
if(tens == 1)
{
tot=tot+strlen(str2[hundredth-1]) + strlen("ten")+strlen("and");
printf("%s hundred and ten\n",str2[hundredth-1]);

}
else
{
tot=tot+strlen(str2[hundredth-1])+strlen(str2[18+tens-1])+strlen("and");
printf("%s hundred and %s\n",str2[hundredth-1],str2[18+tens-1]);
}
}
else
{
int hundredth=i/100;
int tens=(i/10)%10;
int units=i%10;

tot=tot+strlen(str2[hundredth-1])+strlen("and");
printf("%s hundred and ", str2[hundredth-1]);
if(tens == 1)
{
tot=tot+strlen(str2[9+units]);
printf("%s\n",str2[9+units]);
}
else if(tens == 0)
{
tot=tot+strlen(str2[units-1]);
printf("%s\n",str2[units-1]);
}
else
{
tot=tot+strlen(str2[18+tens-1]) + strlen(str2[units-1]);
printf("%s-%s\n",str2[18+tens-1], str2[units-1]);
}
}
}
//sleep(1);
}

tot=tot+strlen("onethousand");
printf("one thousand\n");
printf("%d",tot);
return 0;
}

 

Young programmer – Inspirational Video June 30, 2013

Filed under: Uncategorized — whoami @ 09:28
Tags: , , , ,

Source – Code.org