Inserting in a sorted array through C Linux Code

/*Code for inserting in a sorted array*/

#include<stdio.h>

int insertSorted(int *a,int *size,int item);

int main()

{

int n,i;

int a[]={2,4,6,8,10};

printf("Original sorted array is:\n");

for(i=0;i<5;i++)

{

printf("%d ",a[i]);

}

printf("\nEnter element to insert:\n");

scanf("%d",&n);

int size=5;

insertSorted(a,&size,n);

printf("\nAfter Inserted sorted array is:\n");

for(i=0;i<=5;i++)

{

printf("%d ",a[i]);

}

printf("\n");

return 0;

}

int insertSorted(int *a,int *size,int item)

{

int i,j;//i is position

if(*size<0) {printf("Enter a positive array size.\n"); return;}

/*Determine the location to insert.*/

if(item<a[0])

{

i=0;

}

else

{

for(i=1;i<*size;i++)

{

if(( item<=a[i]) && (item>=a[i-1]))

{

break;//come out of the loop when right position

}

}

}

/*Shift the elemets after the position to insrt to right*/

for(j=*size;j>=i;j--)

a[j]=a[j-1];

/*Insert the lement at rigt postion*/

a[i]=item;

(*size)++;

}

/*

Original sorted array is:

2 4 6 8 10

Enter element to insert:

11

After Inserted sorted array is:

2 4 6 8 10 11

[user02@localhost ds]$ ./a.out

Original sorted array is:

2 4 6 8 10

Enter element to insert:

1

After Inserted sorted array is:

1 2 4 6 8 10

[user02@localhost ds]$ ./a.out

Original sorted array is:

2 4 6 8 10

Enter element to insert:

33

After Inserted sorted array is:

2 4 6 8 10 33

[user02@localhost ds]$ ./a.out

Original sorted array is:

2 4 6 8 10

Enter element to insert:

3

After Inserted sorted array is:

2 3 4 6 8 10

*/

Compress Array by Extracting Unique elements of an array in C on Linux Platform

   C Linux Code to extract the unique numbers only.
   0. assign the first elt to result array
   1.Traverse through the array
   2.compare first to next and if not equal the copy to the resultant array
   if input array is
   int arr[]={1,1,2,2,2,3,4,5,5};
   then output array should be
   1 2 3 4 5

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

int* zeroshift(int *, int);
int main()
{
    int size,i,j;
    int arr[]={1,1,2,2,2,3,4,5,5};
    size=sizeof(arr)/sizeof(int);
    int* ptr=zeroshift(arr,size);
    printf("After after elemenating the repeated elements, array is:\n");
    for(i=0;i<size;i++)
        printf("%d ",ptr[i]);
    printf("\n");
    return 0;
}

int* zeroshift(int *arr,int size)
{
    int i;
    int count=0;
    int *result=(int *)malloc(size*sizeof(int));
    result[count++]=arr[0];
    for(i=0;i<size-1;i++)/* 1.Traverse through the array*/
    {
        if(arr[i]!=arr[i+1])
        {
            result[count]=arr[i+1];/*2.copy the non equal elts to new array say result and increment the count*/
            count++;
        }

    }
    return result;
}
/*
   1 2 3 4 5
 */

Shifting all the Zeros to End of Array through C Code in Linux.

   C Linux Code to shift all the zeros to the end of an array.
   1.Traverse through the array
   2.copy the non zero element to new array say result and increment the count
   3.fill the remaining array index with zeros

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

int* zeroshift(int *, int);
int main()
{
    int size,i,j;
    int arr[]={1,2,0,3,4,0,0,6};
    size=sizeof(arr)/sizeof(int);
    int* ptr=zeroshift(arr,size);
    printf("After Shifting zeroes array is:\n");
    for(i=0;i<size;i++)
        printf("%d ",ptr[i]);
    printf("\n");
    return 0;
}

int* zeroshift(int *arr,int size)
{
    int i;
    int count=0;
    int *result=(int *)malloc(size*sizeof(int));
    for(i=0;i<size;i++)/* 1.Traverse through the array*/
    {
        if(arr[i]!=0)
        {
            result[count]=arr[i];/*2.copy the non zero element to new array say result and increment the count*/
                count++;
        }

    }
    //3.fill the rmaining array index with zeros
    for(i=count;i<size;i++)
    {
        result[i]=0;
        i++;
    }
    return result;
}
/*
   After Shifting zeroes array is:1
   2 3 4 6 0 0 0
 */

Why function pointer is used in C language?

C Linux code for : function pointer and callback function.
What is function Pointer?
->funtion pointer is pointer that holds the address of a function.
Why function pointers are used in C?
->function pointers are mainly used to implement callback functions.
What is callback function in C?
->callback functions are functions which are passed as argument to other functions and this passed callback function is invoked on some event.

->best example is sigaction system call. When given event/signal like alarm,SIGINT occurs,the function pointed by the pointer  is called through the function pointer in sigaction structure.example B

->another use of function pointer is generality like templates in c++.ie. with the same function pointer different
functions can be called.example A below.

/*Example A:
 same function pointer is used to call add and sub function*/
#include<stdio.h>
/*prototype here: funcPtr is function pointer taking two integers as arguments and returning an int
int (*funcPtr(int, int))
*/
/*prototype of registration function generally used to pass function pointer as argument*/
int regfunc(int (*funcPtr)(int, int), int,  int);
/*proto types of function called by function pointers*/
int add(int a, int b);
int sub(int a, int b);

/*definition of first function called through function pointer*/
int add(int a, int b)
{
        return a+b;
}
/*definition of second function called through function pointer*/
int sub(int a, int b)
{
        return b-a;
}
/*main for calling functions through function pointer */
int main()
{
        int a,b;
        a=regfunc(add,10,20);
        printf("sum is %d\n",a);

        b=regfunc(sub,10,20);
        printf("sub is %d\n",b);

        return 0;
}
/*definition of registration function that receives a funcPtr and two inta*/
int regfunc(int (*funcPtr)(int x,int y),  int x,  int y)
{
        return (*funcPtr)(x,y);
}
/*
sum is 30
sub is 10
*/

/*Example B:
->best example is sigaction system call. When given event/signal like alarm,SIGINT occurs,
the function pointed by the pointer act is called though the function pointer in sigaction structure.example B
*/
/*
struct sigaction
{
void (*sa_handler)()//address of sighandler or SIG_IGN or SIG_DEF
sigset_t sa_mask;//to block other signals
int sa_flags;
};
*/


/*
function pointer are reference to code that are passed as argument to different code. callback functions are function which are invoked on some event.
*/
#include<stdio.h>
#include<signal.h>
#include<unistd.h>


struct sigaction act;
/*call back function to be called by funcptr on alarm event*/
void sig_handler(int signo, siginfo_t *si, void *ucontext)
{
printf("Alarm signal received is:%d\n",signo);
}

int main()
{
/*intializig function pointer with func1*/
act.sa_handler=sig_handler;
act.sa_flags=SA_SIGINFO;
/*registering functon pointer to register function*/
/*ie passing functon pointer as argument to register function*/

/*Alarm event will cause call back function call through function pointer pointed by ptr act */
sigaction(SIGALRM,&act,NULL);

alarm(3);/*Now Alarm event will cause call back function call through function pointer pointed by ptr act */

/*waiting for any signal from kernel*/
pause();

/*after signal handler call back function executed*/
printf("in main again\n");
        return 0;
}

/*
Alarm signal received is:14
in main again
*/


/*
function pointer are reference to code that are passed as argument to different code. callback functions are function which are invoked on some event.
*/
#include<stdio.h>

typedef void (*funcPtr)(void);
void regfuncPtr(funcPtr myfp);
void func1();

int main()
{
/*intializig function pointer with func1*/
        funcPtr myfp=func1;
/*registering functon pointer to register function*/
/*ie passing functon pointer as argument to register function*/
        regfuncPtr(myfp);

        return 0;
}
/*defintion of register function*/
void regfuncPtr(funcPtr myfp)
{
        printf("inside regFuncPtr\n");
        (*myfp)();
}
/*definition of function to be called by function pointer*/
void func1()
{
        printf("inside func1\n");
}
/*
inside regFuncPtr
inside func1

*/

/*What is function pointer in C?
functon pointer are reference to code that are passed as argument to different code.
What is callback function in C?
callback functions are function which are invoked on some event.
*/
#include<stdio.h>
#include<signal.h>
#include<unistd.h>


/*Another example of call back function to be called by funcptr on alarm event in C language*/
void mysignalHandler(int signo)
{
printf("\n mysignalHAndler: Alarm signal received is:%d\n",signo);
}

int main()
{
signal(SIGINT,mysignalHandler);
/*create some delay*/
printf("sleeping for 10 seconds.\n");
printf("generate SIGINT by ctrl+c command: else normal exit\n");
sleep(10);

/*after signal handler call back function executed*/
printf("in main again\n");
        return 0;
}

/*
Alarm signal received is:14
in main again
*/
This page explained syntax and c linux code for :
pointer to function
function pointer
callback functions


What is Endianness Little Endian and Big Endian byte order in C

What is endianness ?
Endianness is the way bytes are stored in computer's memory address.
In a 32 bits machine an integer is 32 bits ie 4 bytes as bits are divided in group of 8 bits.so
For example,
suppose we have a 32 bit quantity, written as ox90AB12CD16, which is in hexadecimal.
So, the 4 bytes are: 90, AB, 12, CD where each byte requires 2 hex digits.

There are two ways to store this in memory(as memory is considered as array of bytes).

1. Big Endian
In big endian, the most significant byte is stored in the smallest address. Here's how it would look:

Address  Value    
1000     90                    you may also like linked list questions asked in recent interviews 2014
1001     AB
1002     12
1003     CD
ie 90, AB, 12, CD
If it is an integer say 1 then in a big endian machine it will be stored as 00  00  00  01

2. Little Endian
In little endian, you store the least significant byte in the smallest address. Here's how it would look:

Address  Value
1000     CD                      You may also like user defined string functions asked in recent walkins 2014
1001     12
1002     AB
1003     90
i.e. CD 12 AB 90
If it is an integer say 1 then in a little endian machine it will be stored as 01  00  00  00

How to find the endianness of a machine?
Below code tells how to know the Endianness of a machine.
#include<stdio.h>
int main()
{
   unsigned int i = 1;
   char *c = (char*)&i;/*a character pointer c is pointing to an integer i*/
   if (*c)    /*Since size of character is 1 byte when the character pointer is de-referenced it will contain only first byte of integer.*/
    printf("Your machine is Little endian.\n");//01 00 00 00 because 01 means 1 and true
   
else
       printf("It is a Big endian Machine.\n");//00 00 00 01
   
   return 0;
}
Explanation:
In the above program,
a character pointer c is pointing to an integer i.
Since size of character is 1 byte when the character pointer is de-referenced it will contain only first byte of integer.
So,
if machine is little endian then *c will be 1 (because last_byte/list_signifant_byte  is stored first/in lowes memory address in little endian. ) 00 00 00 01
and
if machine is big endian then *c will be 0 (because fisrt byte is stored firs) 01 00 00 00

Output on a ubuntu terminal is "little endian machine". means 1 was stored as 01 00 00 00
so if you print ox01234567 in little endian then it is stored as 67 45 23 01

Examples:
Generally Intel based processors are little endians. ARM processors were little endians.
 Current generation ARM processors are bi-endian.
Motorola 68K processors are big endians. PowerPC (by Motorola) and SPARK (by Sun)

thesse are most frequently asked interview questions in walk-ins and current interviews regarding eandian ness of a mchine.
Why endianness is used or why the ordering of bytes matter?
endian ness concept is must for network programs like socket programs as it is used by various types of machine and the program should work as desired in all the machines irrespective of it is a little or big endian machine.

There is also a concept of bi-endian hardware.