Tuesday, December 22, 2015

Plugin for VIM

Simple steps to have a plugin for for vim editor.

go to ~/.vimrc and put the below two lines in this file

filetype plugin indent on
syntax on

create .vim folder in home  and plugin directory under this .. something like below
/home/ravi/.vim/plugin

copy the plugin files to this folder with vim extension.

Example:
copy yang.vim from yang central

source the .vimrc file.  - Good to go now.

==============================
ctags

get taglist.vim from http://www.vim.org/scripts/download_script.php?src_id=7701 and put it in plugin dir
get
apt-get install exuberant-ctags

go to source dir and execute
ctags *.c or *.cc

for function list in vim
:TlistOpen
:TlistClose



Saturday, December 12, 2015

Google Test Framework

The place you get your stuff :
http://www.yolinux.com/TUTORIALS/Cpp-GoogleTest.html

Couple of the things one should remember for compilation:

If you make cribs for
makefile:7: *** missing separator.  Stop

Go to the makefile line in which there is a problem and split the line like below.
Original:
testAll: $(OBJS)
    $(CXX) Main_TestAll.cpp $(CXXFLAGS) $(INCS) -o testAll $(OBJS)

Fixed :

testAll: $(OBJS);\
$(CXX) Main_TestAll.cpp $(CXXFLAGS) $(INCS) -o testAll $(OBJS)

Again on compilation of the test framwork, if it cribs for

vagrant@guca-csa-dev:~/gen_work/gtf/gtest-1.7.0/test/src$ make
\
g++ -g -L/opt/gtest/lib -lgtest -lgtest_main -lpthread -I./ -I../../src -I/opt/gtest/include -o testAll  Main_TestAll.cpp ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o
/usr/bin/ld: Addition_Test.o: undefined reference to symbol '_ZN7testing8internal9EqFailureEPKcS2_RKSsS4_b'
/opt/gtest/lib/libgtest.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [testAll] Error 1

Solution:
Go to makefile and place the .cpp file before header and library inclusion.

something like this below.
testAll: $(OBJS);\
$(CXX) Main_TestAll.cpp $(CXXFLAGS) $(INCS) -o testAll $(OBJS


Tuesday, March 17, 2015

Single Linked List : Operations

#include
#include

struct node {
    int data;
    struct node *link;
};


/* Function Prototypes */

void append (struct node **q, int data);
void display(struct node *q);
void addAtBegin(struct node **q, int data);
void addAtLocation(struct node **q, int pos, int data);
void count(struct node *q);
void delete(struct node **q, int data);


int main ()

{
    struct node *p = NULL;

    append(&p, 10);
    append(&p, 20);
    append(&p, 30);
    append(&p, 40);
    display(p);

    addAtBegin(&p, 3);
    addAtBegin(&p, 1);
    display(p);

    addAtLocation(&p, 4, 12);
    addAtLocation(&p, 6, 13);
    display(p);

    delete(&p, 40);
    display(p);
    //count(p);

}

void append (struct node **q, int data) /* Appends at the end of the list */
{
    struct node *temp;
    struct node *r;

    if (*q == NULL){ /* First node in the link */
        temp = (struct node *)(malloc(sizeof(struct node)));
        temp->data = data;
        temp->link = NULL;
        *q = temp;
    }else{
        r = *q;
        while(r->link != NULL){
            r = r->link;
        }
        temp = (struct node *)malloc (sizeof(struct node));
        temp->data = data;
        temp->link = NULL;
        r->link = temp;

    }
    return;
}
void display(struct node *q)
{
    if (q == NULL){
        printf("List is empty \n");
    }
    while(q != NULL){
        printf("%d\n", q->data);
        q = q->link;
    }
    printf("===============================\n");
}

void addAtBegin(struct node **q, int data)

{
    struct node *temp;

    if (*q == NULL){ /* This is the first node */
        temp = (struct node *)malloc(sizeof(struct node));
        temp->data = data;
        temp->link = NULL;
        *q = temp;
    }else{
        temp = (struct node *)malloc(sizeof(struct node));
        temp->data = data;
        temp->link = *q;
        *q = temp;
    }
    return;
}
void addAtLocation(struct node **q, int pos, int data)
{
    struct node *temp, *r;
    r = *q;
    int i;
    for (i = 1; i        r = r->link;
        if (r == NULL){
            printf("It is exceeded the no.of the links in the list\n");
            return;
        }
    }
        temp = (struct node *)malloc(sizeof(struct node));
        temp->data = data;
        temp->link = r->link;
        r->link = temp;

   
}
void count(struct node *q)
{
    int count=0;
    while (q != NULL){
        count++;
        q = q->link;
    }
    printf("Count = %d\n", count);
}

void delete(struct node **q, int data)
{
    struct node *temp, *old;
    temp = *q;

    while (temp != NULL){

        if(temp->data == data)
        {
            if (temp == *q){
                *q = temp->link;
            }else{
                old->link = temp->link;
            }
            free(temp);
            return;
        }
        else
        {
            old = temp;
            temp = temp->link;
        }
    }
    printf("Data is not found in the list\n");
}

Friday, June 10, 2011

FAQ Interview

simple programs to ask
================
#STRCAT
#STRCPY
#SWAP
#SUM of Digits


Macros
======
#No of Seconds in a year
#Server IP address
#MIN/MAX of two
#Allocation and deallocation of mem
#FOREVER loop


Declarations
=========
Pointer to int
Array of pointers
Pointer to an array
Struct and Union
Function Pointers

BitWise Operators
=============
how bitwise operators are used in error status flags. ( check declaring bits for particular err, setting more than one error, reading what error occured, clearing the error)



Misc Quetions
==========
#Static, Extern vars and functions scopes
#const
#How do u copy two structs
#Linked lists


===================================================================================================
If he answers 3 out 5 for above questions(except link list) then ask below to judge the depth

#Program to eliminate the duplicate elements in array. (Chk array init syntax, loops, function fomal args)
#Program to reverse the array without using the temp storage array
#Sort array using bubble sort and search using binary.


#Fibonacci Or factorial using recursion. ( Good if he explains the stack flow b/w normal function and recursion)
===================================================================================================

If he answers 1 of above good to go deeper

#List list - Create, add the end, add at the begining, delete, sort

=================================================================================================

if he is 5+ then ask classical typical interview question like

Dictionary creation - Chk the approach, Data structure building, tree formation, binary search on trees. ( This is good enough to discuss for 2 hrs)

=================================================================================================

C++ Questions

# what is constructor and destructor - usage , declaration

# Can we overload cons and desc - why do we do so ?

# What are virtual function - usage ?

# Is it possible to ve virtual cons/dest - If no why ?

#scope - public, private and protected.

# Friend function and usage ..

# Different types of inheritance.

# Define a class and member functions to add and ask add to overload for any data type . (Chk class , object, functions, logic)

#what r pure virtual class .. - usage.

==============================================================================================


Operating system

# Memory Laypout of a program

# Job of OS. - how in between

# Difference b/w process and thread.

# IPC ( pipe, msg queques, shared memory, sockets )

# Socket programming : client side calls and server side calls ( Y for the difference)

# Virtual Memory concepts

=============================================================================================


Network questions

# TCP/IP 3 way hand shake
# MAC / IP address conversion

Wednesday, November 4, 2009

Check whether given number is Fibonacci OR not

Algo : A given number is said to be fibonacci, if 5*N*N -4 or 5*N*N +4 is a square

#include
int isSquare(int num);
main()
{
int num, temp;
printf("Enter the number\n");
scanf("%d", &num);

temp = 5 *(num * num) - 4;

if(isSquare(temp) || isSquare(temp+8)){
printf("It is finonacci number\n");
}else {
printf("It is NOT finonacci number\n");
}
}
int isSquare(int num)
{
int i=1;
while(num>0)
{
num = num - i;
i = i+2;
}
if(num==0)
return 1;
else
return 0;
}

Monday, November 2, 2009

Fibonacci Numbers

C program to generate Fibonacci numbers till the user entered value.

#include
main()
{
int num;
int *a, i;;
printf("Enter the number\n");
scanf("%d", &num);

a = (int *)malloc(sizeof(int)*num);

a[0] = 0;
a[1] = 1;
printf("FIBONACCI Numbers are\n");
printf("%d\n%d\n", a[0],a[1]);

for(i = 2; i
{
a[i] = a[i-1] + a[i-2];
printf("%d\n", a[i]);
}


}

Tuesday, May 6, 2008

Basic C programming answers

1)Program to find number of 1s and zeros in given integer.
Find the answer here http://gucatheprogrammer.blogspot.com/2007/10/simple-yet-useful-c-programs.html

2)Write a C program to check whether machine is Big endian OR little endian
#include

int main()
{
unsigned int n = 1;
char *p;

p = (char*)&n;
if (*p == 1)
printf("Little Endian\n");
else if (*(p + sizeof(int) - 1) == 1)
printf("Big Endian\n");
else
printf("What the crap?\n");

return 0;
}
3)C program to Convert decimal to binary
Find the answer here http://gucatheprogrammer.blogspot.com/2007/10/simple-yet-useful-c-programs.html

4)C program to do STRCPY without using strcpy
I will write just the function :
char * mystrcpy(char * dest,const char *src)
{
char *temp = dest;
while ((*dest++ = *src++) != '\0')
return temp;
}

5)C program to STRCAT without using strcat
char * mystrcat(char * dest, const char * src)
{
char *temp = dest;
while (*dest)
dest++;
while ((*dest++ = *src++) != '\0') ;
return temp;
}

6)Write a C macro which returns number of seconds in year
This is too general. This is just to check guy knows about the MACRO and its syntax.
#define NUMER_OF_SECONDS_YEAR (365*24*60*60)

7)C program to find sum of digits of any given integer number .
#include
int add_digits(int a);
main()
{
int a ;
printf("Enter the number\n");
scanf("%d", &a);
printf("Sum of digits = %d\n", add_digits(a));
}
int add_digits(int num)
{
int sum = 0;
while(num)
{
sum += num%10;
num /=10;
}
return sum;
}

8)C program to fine leap year or not
This is just to check how quick he can write ..
Better would be macro or function.
#include
main()
{
int year;
printf("Enter the year\n");
scanf("%d", &year);
if(year % 4 == 0){
printf("Given year is leap\n");
} else {
printf("Given year is NOT leap\n");
}
}

9)C program to swap two variables (using pointers), without using temp variable.
#include
void swap(int *p1, int *p2);
main()
{
int a, b;
printf("Enter the values for a and b\n");
scanf("%d%d",&a,&b);
swap(&a, &b);

printf("a = %d\n and b = %d\n", a, b);
}

void swap(int *p1, int *p2)
{
*p1 = *p1 + *p2;
*p2 = *p1 - *p2;
*p1 = *p1 - *p2;
return;

}

10)C program to find sum of given Array.
#include
int arr_sum(int *a, int size);
main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int sum = arr_sum(a,10);
printf("SUM = %d\n", sum);
}
int arr_sum(int *a, int size)
{
int sum = 0, i;
for(i =0; i
#define MIN(a,b) ((a>b)?b:a)
main()
{
int a=100, b=20;
printf("MIN = %d\n", MIN(a,b));

}

The below ones are left as exercise to students :) .. Actually I got bored to write :)
12)C program to find largest of three numbers
13)C program to store data for 10 student info
14)Write all infinite loops u can think off