Friday, 16 June 2017

Array program in C

/*
    Write a C program using function to
    i)    insert elements into an array.
    ii)    display array elements
    iii)    return maximum element of a array
    iv)    search a element given by the user and return the location of the element. return -1 if element is not found in the array.
    v)    sort the elements in ascending order using "bubble sort".
    vi)    Delete an element from the end of the array.
    vi)    Delete an element from the front of the array.
    vii)    Delete an element from a given position. If given position is not valid return give error message.

    viii)    Add element at the end.
    ix)    Add element at the bigining
    x)    Add an element in a given position. If given position is not valid return give error message.
    xi)    Reverse the elements of the array.
    xii)    Create two arrays from the given array. In the new arrays, one will contain the even elements and other will contain odd elements.
    xiii)    Create two arrays from the given array. In the new arrays, one will contain the positive elements and other will contain negative elements.

*/
#include<stdio.h>
void insert(int [], int );
void show(int [], int );
int max(int [], int);
int search(int [], int,int);
void bubble(int [],int);
void del_end(int [],int *);
void del_front(int [],int *);
void del_pos(int [], int *, int);

void add_end(int [],int *);
void add_beg(int [],int *);
void add_pos(int [],int *);
void reverse(int [],int);
void create_odd_even(int [],int ,int [],int *,int [], int *);
void create_pos_neg(int [],int ,int [],int *,int [], int *);

int main()
{
    int a[100]={1,2,3,4,5,6,7,8},n=8,ele,temp;
    /*printf("\nEnter number of elements=");
    scanf("%d",&n);
    printf("\nEnter the elements=");
    insert(a,n);
    printf("\nThe array is\n");
    show(a,n);*/
    printf("\nThe maximum element=%d",max(a,n));
/*
    printf("\n Enter an element to be searched=");
    scanf("%d",&ele);
    temp=search(a,n,ele);
    if(temp==-1)
        printf("\n%d is not present in the array\n",ele);
    else
        printf("\n%d is present at %d location in  the array\n",ele,temp);
    */
    printf("\nBefore sorting\n");
    show(a,n);
    bubble(a,n);
    printf("\nAfter sorting\n");
    show(a,n);

    del_pos(a,&n,4);
    show(a,n);
    del_pos(a,&n,3);
    show(a,n);
    return 0;
}

void insert(int a[], int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}

void show(int a[], int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("%10d",a[i]);
    }
    printf("\n");
}

int max(int a[], int n)
{
    int mx,i;
    mx=a[0];
    for(i=1;i<n;i++)
    {
        if(mx<a[i])
            mx=a[i];
    }
    return mx;
}

int search(int a[], int n,int ele)
{
    int i,found=0;
    for(i=0;i<n;i++)
    {
        if(ele==a[i])
        {
            found=1;
            break;
        }
    }
    if(found==0)
        return -1;
    else
        return i;
}

void bubble(int a[],int n)
{
    int i,j,temp;
    for(i=0;i<n;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;
            }
        }
    }
}

void del_end(int a[],int *n)
{
   
    (*n)--;
}

void del_front(int a[],int *n)
{
    int i;
    for(i=0;i<(*n)-1;i++)
    {
        a[i]=a[i+1];
    }
   
    (*n)--;
}

void del_pos(int a[], int *n, int pos)
{
    int i=pos;
   
    if(pos<0 || pos>=(*n))
    {
        printf("\nEnter a valid position");
        return;   
    }
    if(pos==0)
        del_front(a,n);
    else if(pos==(*n)-1)
        del_end(a,n);
    else
    {
        while(i<(*n)-1)
        {
            a[i]=a[i+1];
            i++;
        }
        (*n)--;
    }
}