Programming/Coding

Machine Code

Programming/Coding far or less involves the logic that one gains right from one’s childhood from schools, parents, friends and the environment. It is impossible to say that one doesn’t have a logical thinking capability because every one who is born as a human on the earth is endowed with logical thinking so as the programming capability.

The next important thing about the programming is, the weapon which you use to solve the complex and perplexed problems is, the “LANGUAGE”. The language is same as the language we speak in, it doesn’t matter in what language we express our thoughts but communication should happen, In the programming arena also the main thing is the “communication” that must happen not with the other human being as in the real world but with the un human and real machine which could only understand mysterious  1’s and 0’s.

In most of the interviews the interviewer just checks for the logic you. How much spontaneous you are  in writing the code and building the algorithm. Its always better striving to understand existing algorithms and immersing yourself in coding from some of the best programming contest sites such as http://www.codechef.com.

The bottom line here is that the whole life of  the programmer lies in arranging the mysterious yet only 2 digits(1 and 0). How well you arrange these mysterious 1’s and 0’s is the difference between the best and the mediocre, in highly abstraction form of language or in a low level language(where it is necessary to  dig deep into the machine code and to dirty your hand with bits) .

Before reading further please make sure that you have fundamental knowledge of C language because sometimes I may use C, C++ or Java sometimes just a simple pseudo code.

Let us quickly start some of the programs I was asked in many programming contests over past three years. Some of these questions also include interview questions and puzzle type questions. All the code is checked and verified for errors I strongly recommend you to once compile and check for errors if anything wrong please post a comment. If you have any doubts and want some programs to be done please post me in the comments.

1)Write a program to swap two numbers with out using temporary variable

#include <stdio.h>
#include <conio.h>
int main(void)
{
int a,b,i,j;
clrscr();
printf(“Enter two integers: “);
scanf(“%d%d”,&a,&b);
printf(“a=%d,b=%d\n”,a,b);
for(i=0;i<16;i++)
{
if((a&(1<<i))^(b&(1<<i)))
{
a=a^(1<<i);
b=b^(1<<i);
}
}
printf(“a=%d,b=%d”,a,b);
getch();
return 0;
}
compiled on :gcc  Logic : Using Bitwise operators
2)Write a program to swap two numbers with out using temporary variable with in one line
#include <stdio.h>
#include <conio.h>
int main(void)
{
int a , b;
printf(“Enter two integers: “);
scanf(“%d%d”,&a,&b);
printf(“Before swapping: a=%d,b=%d\n”,a,b);
a=(a+b)-(b=a);
printf(“After swapping: a=%d,b=%d”,a,b);
getch();
return 0;
}
compiled on :gcc logic :simple and tricky

3) Write a program to extract source code from comment lines
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *p;
char ch=’a’,ch1;
int count=1;
clrscr();
p=fopen(“cfile.c”,”r”);//this is the demonstration of comment line
if(p==NULL)
{
printf(“File not found”);
exit(1);
}
while(ch!=EOF)
{
ch=fgetc(p);
if(ch==’\n’)
count++;
else if(ch=='”‘) {
while((ch=fgetc(p))!=’\n’);
count++;
}
else if(ch==’/’)
{
if((ch1=fgetc(p))==’/’)
{
printf(“\nComment no %d:”,count);
while((ch=fgetc(p))!=’\n’&&ch!=EOF)
printf(“%c”,ch);
count++;
printf(“\n”);
continue;
}
else if(ch1==’*’)
{
printf(“\nComment %d :”,count);
while(ch!=EOF)
{
if((ch=fgetc(p))==’*’)
{
ch1=fgetc(p);
if(ch1==’/’)
break;
else
{
printf(“%c%c”,ch,ch1);
continue;
}
}
else if(ch==’\n’)
count++;
printf(“%c”,ch);
}
printf(“\n”);
}
}
}
getch();
}
Compiled on: Turbo C, gcc
Description : Extracts the program source code with out the comment lines in the given source file

One of the most common questions in many interviews is the sorting techniques. There are many sorting techniques, I am going to explain some of them in the coming posts.

We will start off with, the basic and simplest of all the sorting techniques, Bubble sort

Bubble Sort

Method: Bubble sort compares two adjacent elements and swaps if the first element is greater than the second one. After every pass the largest element in that pass bubbles up to the last position.

Example:

suppose the array elements are

5  7  8  3  2

pass1:

5  7 8  3  2  (compare 5 & 7)  (No Exchange)

5  7  8 3  2  (compare 7 & 8 )  (No Exchange)

5  7 8 3 2  (compare 8 & 3)  (Exchange)

5  7 3 8 2 (compare 8 & 2)  (Exchange)

5  7 3  2  8    (End of the pass1, the largest element is in its corresponding position the array)

pass2:

5 7 3 2 8 (compare 5 & 7)  (No Exchange)

5  7 3 2 8 (compare 7 & 3)  (Exchange)

5 3 7 2 8 (compare 7 & 2)  (Exchange)

5  3 2 7 8 (End of the pass2, the largest element in the pass2 is bubbled up to its corresponding position)

pass 3:

5  3 2 7 8 (compare 5 & 3)  (Exchange)

3 5  2 7 8 (compare 5 & 2)  (Exchange)

3  2 5 7 8 (End of the pass3, the largest element in this pass is bubbled up to its corresponding position)

pass 4:

3  2 5 7 8 (compare 3 &2 ) (Exchange)

2 3 5 7 8 (End of the pass4, all elements sorted)

Algorithm

#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[]={1,12,2,11,3},i,j,n=5,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf(“%d “,a[i]);
getch();
return 0;
}

compiled on: dev c++,  gcc, Turbo C

Worst Case: O(N2)        Average Case: O(N2)



What is the smallest number divisible by each of the numbers 1 to 21?

public class Projec {public static void main(String[] args) {

out:for (int i = 2520; ; i++) {
for (int j = 2; j < 21; j++) {
if(i%j==0) ;
else
continue out;
}
System.out.println(“”+i);
break out;
}
}

}

But the true way of finding the number is as follows

To find a number that is divided by 1 to 6

is 1*2*3*2((4)because one 2 is already included)*5*(no need(2*3) already included)=60

1*2*3*2*5=60

FOR 1 to 21

2 * 3 * 2 * 5 * 7 * 2 * 3 * 11 * 13 * 2 * 17 * 19 = 232792560

Here I want to share some of the simple & tricky C puzzles and interview questions I faced.

1) Write single line swapping function

Ans:  Check here for Python code

  Please check here for C code

2) Write single line macro to swap two numbers 


#include

#define swap(a,b) b=(a+b)-(a=b)

int main(void)
{
    int a=12;
    int b=-34;
    printf("Before swapping a=%d, b=%d\n",a,b);
    swap(a,b);
    printf("After swapping a=%d, b=%d\n",a,b);
    return 0;
}

3) Write a macro to check whether a given number is a power of two or not


#include

#define isPowOf2(x) ( ( x & ( x - 1 ) ) == 0) ? 1:0

int main(void)
{
    if(isPowOf2(4))
   	 printf("It is power of two\n");
    else
   	 printf("It is not power of two\n");
    if(isPowOf2(41))
   	 printf("It is power of two\n");
    else
   	 printf("It is not power of two\n");
    if(isPowOf2(64))
   	 printf("It is power of two\n");
    else
   	 printf("It is not power of two\n");
    if(isPowOf2(400))
   	 printf("It is power of two\n");
    else
   	 printf("It is not power of two\n");

    return 0;

}

4) Write code to swap two numbers with out using temporary variable and  using shifting operators

Ans: Please check here

5) Write a function to round off a given float/double to the nearest integer

#include

int Myround(double n)
{
    return (int)(n+0.5);
}
int main(void)
{
    printf("%d\n",Myround(25.7));
    printf("%d\n",Myround(25.2));
    printf("%d\n",Myround(25.6));
    printf("%d\n",Myround(25.9));
    return 0;
}

5) Write a one line macro to round off a given float/double to the nearest integer

Ans: same as above 
#define myround(a) (int)(a+0.5)

6) Write a function to print binary representation of a given integer

#include

int binaryRep(int n)
{
    int i=0;
    for(i = sizeof(int)*8 - 1;i >= 0; i--)
    {
   	 if( ( n & (1 << i ) ) > 0 )
   		 printf("%d", 1);
   	 else
   		 printf("%d", 0);
   	 if(i%4==0)
   		 printf(" ");
    }
}

int main(void)
{
    binaryRep(10);
    printf("\n");
    binaryRep(100);
}

7) Write a macro to check whether a specified bit in a number is on or off

#include <stdio.h>

#define onROff(n,i) ( (n & (1 << i-1)) > 0 ) ? 1 : 0

int main(void)
{
    if(onROff(15,1))
   	 printf("first bit is on\n");
    else
   	 printf("first bit is off\n");

    if(onROff(15,2))
   	 printf("second bit is on\n");
    else
   	 printf("second bit is off\n");

    if(onROff(15,3))
   	 printf("third bit is on\n");
    else
   	 printf("third bit is off\n");

   return 0;
}

8 ) Write a macro to switch On a bit in a given number

#include <stdio.h>

#define switchOn(n ,i) n|=(1 << (i-1))


int main(void)
{
    int n=10;
    switchOn(n,1);
    printf("After switching on first bit of n=%d\n",n);
    switchOn(n,3);
    printf("After switching on third bit of n=%d\n",n);
}
Advertisements
  1. harsha
    June 14, 2010 at 9:22 am

    it is simply superb

    • June 16, 2010 at 1:45 pm

      Thank you!!

      • durga
        July 17, 2012 at 2:46 pm

        Vasanth Raja :Thank you!!

        i want java coding max and min using divide and conquer for using recursion

  2. abhinav
    June 16, 2010 at 1:41 pm

    Hey vasanth, Great job.. continue the good work..

  3. March 16, 2011 at 5:25 pm

    Awesome blog, thanks for the wonderful tips.

  4. vinothini
    March 31, 2011 at 6:44 am

    hey.. can u help me??
    i need program for R or R* algorithm..
    or any program related to database system..!!

    • March 31, 2011 at 4:29 pm

      R or R* algorithms are Relational Quiry optimization techniques if you know any language(C, C++, Java etc.,) You can easily implement .. If you have any difficulties I can help you out.

  5. Kalpana Chaudhary
    August 30, 2011 at 6:15 pm

    Hi! You are really a brilliant programmer and I would like to appreciate your job….You are doing a great job so please keep it up…!!!

  6. agalya
    October 29, 2011 at 4:51 pm

    hi.. grt job. wish u all the best

  7. agalya
    October 29, 2011 at 4:52 pm

    hi., can u tell some idea for my Data Mining mini project

    • November 1, 2011 at 10:14 am

      There are many areas in which you can work on!! Some of the areas you can work on are as follows
      Stock predictions
      Fraud detections
      Bug detections and classification
      Software agining

      ….
      etc.,

      Try exploring some papers in this areas this will help you.

  8. February 25, 2012 at 7:33 pm

    you are really a coding master(mind)

  9. February 25, 2012 at 7:34 pm

    can you help me programming in sudoku

  10. February 25, 2012 at 7:36 pm

    Hemant Jangid :
    can you help to make sudoku in java

  11. Ammu
    September 26, 2012 at 6:16 am

    Hai Sir,
    I am using matlab for video compression…..I tried one of the code which i was download from the net…I cant able to run the code also i cant identify the error.If you are interested i will send u the code.Please send me ur mail id and reply to this id angelicammu90@gmail.com

  12. vaina
    October 20, 2012 at 10:34 am

    hi vasanth
    thank u for ur code. I have an doubt in the insertdb.m file in that file in the first line, Name=? Imgpath=? ICC=?
    kindly reply my mail id is – vainavarshni@gmail.com

  1. January 2, 2011 at 1:57 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: