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

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

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.

 

 

 

 

 

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.

 

 

Modular Exponentiation September 4, 2010

Filed under: C,C++ Programs — whoami @ 18:05
Tags: ,

Problem5

CodeChef

ACCEPTED


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int  main()
{
 int A,B;
 while(1)
 {
 scanf("%d%d",&A,&B);
 if(A==0&&B==0) break;

 int base,power,modulus;
 modulus=1000000;
 base=A;power=B;
 long long result = 1;
 for (int i = 31; i >= 0; i--) {
 result = (result*result) % modulus;
 if ((power & (1 << i)) != 0) {
 result = (result*base) % modulus;
 }
 }

 int ans=(int) result;

 printf("%d\n",ans);
 }


return 0;
}

 

nfa to dfa conversion best explained July 26, 2010

Filed under: C,C++ Programs,Compiler — whoami @ 16:29
Tags: ,

Here is the example from ullman sethi

link

other links of programs

 

Programming Quest June 29, 2010

Filed under: C,C++ Programs — whoami @ 09:02
Tags: , , ,

From YoungProgrammer.com

LCM-GCD based question

Question:

We all interested in mathematics and world are divided in to two parts.
One who are interested in mathematics and other who are afraid of mathematics.
Here is a equation:
( X * N ) % Y = 0

Given two number X & Y you have to find minimum N that satisfies the equation.

Input:

Input consists of two positive integer X & Y . (1<=X,Y<=2000000000)

Output:

You have to output minimum N.

Sample Input:

1 5
6 7

Sample Output:

5
7

Note: You have to give the total of all output for each input. 
For the sample input set, you have to give 
7+5 = 12. So answer is 12.

Download data sets

ANS-562252
my solution
#include<cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>
 
using namespace std;
 
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define SZ(x) ((int) (x).size())
#define FE(i,x) for(typedef((x).begin() i=(x).begin();i!=(x).end();i++) 

typedef long long int  int64;
typedef unsigned long long int uint64;
typedef long double ldb;
typedef pair <int, int> pii;
typedef vector<int>vi;
typedef vector<string>vs;
 

 
template <class T> T sqr (T x) {return x * x;}

int gcd(int a,int b)
{
 if(b==0) return a;
 return gcd(b,a%b);
}

int main()
{
 int X,Y;
 int total=0;
 while(1)
 {
 cin>>X>>Y;
 int hcf=gcd(X,Y);
 int lcm=(X*Y)/hcf;
 total+=(lcm/X);
 cout<<total<<endl;
 }




return 0;
}


 

Programming Quest

Filed under: C,C++ Programs — whoami @ 08:43
Tags: ,
If a number is the only number between a prime number and a square number it is 
beprisque.
Such few numbers are: 2 3 8 10
2 is beprisque, because, 1 is a square and 3 is a prime number
3 is beprisque, because, 2 is prime and 4 is a square number
What's the 100th beprisque number ?

ANS-119026
From Young programmer.com

my solution (not optimized)

#include<cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>
 
using namespace std;
 
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define SZ(x) ((int) (x).size())
#define FE(i,x) for(typedef((x).begin() i=(x).begin();i!=(x).end();i++) 

typedef long long int  int64;
typedef unsigned long long int uint64;
typedef long double ldb;
typedef pair <int, int> pii;
typedef vector<int>vi;
typedef vector<string>vs;
 

 
template <class T> T sqr (T x) {return x * x;}

int main()
{
 static int prime[1000009];
 static int sqr[1000009];
 for(int i=1;i<=1000;i++)
 sqr[i*i]=1;
 
 prime[1]=0;prime[2]=1;

 for(int i=3;i<=1000000;i=i+2)
 {
 int flag=1;
 for(int j=3;j<=sqrt(i);j++)
 {
 if(i%j==0) { flag=0;break;}
 else continue;
 }
 if(flag==1) prime[i]=1;
 }

 int count=0;
 for(int i=2;i<=120000;i++)
 {
 if((sqr[i-1]==1&&prime[i+1]==1)||(sqr[i+1]==1&&prime[i-1]==1)){cout<<i<<endl; ++count;}
 }

 cout<<count<<endl;
 


return 0;
}

here is the few BEPRISQUE number
2
3
8
10
24
48
80
82
168
224
226
360
440
442
728
840
1088
1090
1224
1368
1522
1848
2026
2208
2400
3024
3250
3720
3968
4760
5040
5624
5928
6562
7920
8648
9802
10608
11026
11448
12322
13688
13690
14160
14640
15130
16128
17160
18224
19320
21024
21610
24024
25920
28560
29242
29928
31328
33488
36480
42024
44520
47088
47962
49728
50626
53360
54288
56168
56170
57120
59050
61008
62002
64008
65026
66048
67080
70224
71288
74528
74530
77840
81224
85848
88210
89400
90600
91808
91810
95480
95482
97968
99224
103042
104328
112224
113568
116280
119026
100