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.

 

 

 

 

Advertisements
 

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.

 

 

SPOJ 5676. STONE GAME January 29, 2010

Filed under: C,C++ Programs,CODECHEF,Coding,SPOJ — whoami @ 17:21
Tags: , ,

SPOJ 5676. STONE GAME
Problem code: RESN04

–AC–

#include

int main()
{
int T,n,i,j,count;
scanf(“%d”,&T);
while(T–){
scanf(“%d”,&n);
count=0;
for(i=1;i<=n;i++)
{
scanf("%d",&j);
if(i==j) ++count;
}
if(count%2==0) printf("BOB\n");
else printf("ALICE\n");
}

return 0;
}

 

TJU 2498. Uncle Jack January 20, 2010

Filed under: C,C++ Programs,Coding,PKU,TJU,TODOLIST,WA — whoami @ 20:41
Tags: , , ,

TJU 2498. Uncle Jack
TODOLIST
WRONG ANSWER
{big multiplication based question}

#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;

class A{
   long long i,j,N,D,R;
    
   public:
       void input(){
         cin>>N>>D;
         if(N==0&&D==0) exit(0);
       }
       void output(){
        R=powf(N,D);
        cout<<R<<endl;
       }
};

int main()
{
 A a;
 while(1){
   a.input();
   a.output();
 }

return 0;
}
 

TJU 2990. Simple Task January 19, 2010

Filed under: C,C++ Programs,Coding,TJU — whoami @ 11:49
Tags: ,

TJU 2990. Simple Task


#include<iostream>
using namespace std;

class A{
    int i,j,l,n,m,a[201],b[201];

 public:
   void input(){
    cin>>n;
    for(i=0;i<n;i++)
      cin>>a[i];
    cin>>m;
    for(i=0;i<m;i++)
      cin>>b[i];
    l=0;
    for(i=0;i<n;i++)
      for(j=0;j<m;j++)
        if(a[i]+b[j]==0)
         ++l;
   }

  void output(){
    cout<<l<<endl;
  }
};
 
int main()
{
  int cases;
  cin>>cases;
  A obj;
  while(cases--)
  {
    obj.input();
    obj.output();
  }

return 0;
}

 

TJU 2994. Vector

Filed under: C,C++ Programs,Coding,TJU — whoami @ 11:13
Tags: ,

TJU 2994. Vector


#include<iostream>
using namespace std;

class A{
     int a[11],b[11],n,i,j;

   public:
      void input(){
       cin>>n;
       j=0;
       for(i=0;i<n;i++)
         cin>>a[i];
       for(i=0;i<n;i++){
         cin>>b[i];
         j=j+a[i]*b[i];
         }
      }

     void output(){
       cout<<j<<endl;
     }
};

int main()
{
  A obj;
  int cases;
  cin>>cases;
  while(cases--)
  {
    obj.input();
    obj.output();
  }

return 0;
}

 

SPOJ 5872. Anagram January 16, 2010

Filed under: C,C++ Programs,Coding,OOPS,SPOJ — whoami @ 11:31
Tags: , ,

SPOJ 5872. Anagram
Problem code: ANAG

c++/oops implemented

#include<iostream>
#include<string.h>
#include<stdlib.h>

using namespace  std;
class A{
   char s1[100],s2[100];
   
   int i,j,k;
   
  public:
   void input(){
    cin>>s1>>s2;
   }
   void cal(){
    int a[26],b[26];
    for(i=0;i<26;i++)
     a[i]=b[i]=0;
   for(i=0;s1[i]!='\0';i++)
   {
     a[s1[i]-'a']++;
   }
   for(i=0;s2[i]!='\0';i++)
   {
     b[s2[i]-'a']++;
   }
   output(a,b);
  }

  void output(int a[],int b[]){
   int flag=0;
   
   for(i=0;i<26;i++)
   {
     if(a[i]==b[i]){
      flag=1;
      continue;
     }
     else{
      flag=0;
      break;
     }
   }
    if(flag==1) cout<<"YES\n";
    else cout<<"NO\n";
  }

};

int main()
{
  A obj;
  int cases;
  cin>>cases;
  while(cases--)
  {
    obj.input();
    obj.cal();

   
  }

return 0;  
 }