Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Monday, 12 September 2016

Merging two sorted arrays in sorted manner in C


Hello friends !
Problem statement :
We are given two sorted arrays , we have to create a new array from these two arrays and we need to insert elements in such a way that the newly created array remains sorted .

So, lets understand the logic with the code :


#include<stdio.h>
#include<stdlib.h>
#define max 100

void sort(int a[],int n);
void print(int a[],int n);
void merge(int a1[],int n1,int a2[],int n2);

int full[max];

int main()
{
    int i,n1,a1[max];
    printf("Enter the number of elements of first array : ");
    scanf("%d",&n1);
    printf("Enter array elements :\n");
    for(i=0;i<n1;i++)
    {
        scanf("%d",&a1[i]);  
    }
   
    int j,n2,a2[max];
    printf("Enter the number of elements of second array : ");
    scanf("%d",&n2);
    printf("Enter array elements :\n");
    for(j=0;j<n2;j++)
    {
        scanf("%d",&a2[j]);  
    }
   
    //now we have two arrays
    //lets sort them individually first
   
    sort(a1,n1);
    sort(a2,n2);
   
    printf("First \n");
    print(a1,n1);
   
    printf("Second \n");
    print(a2,n2);
   
    merge(a1,n1,a2,n2);
    printf("The merged \n");
    print(full,n1+n2);
}

/* I have used insetion sort here for no reason,
one can use any random sorting algo for this
our prime aim is to merge two sorted arrays
such that resultant array is also sorted */
void sort(int a[],int n)
{
    int i,temp;
    int hole,value;
    for(i=0;i<n;i++)
   
    {   
        value = a[i];
        hole = i ;
      
        while(hole>0 && a[hole-1]>value)
        {
            a[hole] = a[hole-1];
            hole--;
        }
        a[hole] = value;
    }  
}

void merge(int l[],int nl,int r[],int nr)
{
    /*
    We have two sorted array, say l[] and r[].
    we have to fill array full[] such that
    it should contains all elements of both l[] and r[]
    and remain sorted*/
   
    int i,j,k;
    i=j=k=0;
    while(i<nl && j<nr)
    {
        if(l[i] < r[j])
        {
            full[k++] = l[i++];
        }
        else
        {
            full[k++] = r[j++];
        }
    }
    while(i<nl)
    {
        full[k++] = l[i++];
    }
    while(j<nr)
    {
        full[k++] = r[j++];
    }
}
void print(int a[],int n)
{
    int i;
    printf("Sorted Array : ");
    for(i=0;i<n;i++)
    {
        printf(" %d ",a[i]);
    }
    printf("\n");
}




Thanking you !

Saturday, 6 August 2016

Stack implementation using C !

Hello guys and girls !
Here is the implementation of Stack using C language .


So, what do I mean by implementation ??
When dealing with data structures I encountered 2-3 terms -
  1. Interface
  2. Implementation 
  3. ADT(Abstract Data Type).   
Normally we can assume that interface is something that we see/ user is allowed to see, implementation is something that is playing role behind the terminal in the source file and such hidden data type is referred as ADT.

I guess this is sufficient to start with STACK .

Stack is an ADT , stack in Data Structures is exactly same as in real life , i.e. we are allowed to add elements on the top only and we can remove the elements from the top only , so as LIFO ( Last in first out ).

In C , Stack can be implemented using array or linked list .
Dealing with array implementation is somewhat simpler for beginner so I would like to start with implement Stack using Array .

We need to declare globally , variable "top" of type int , and array[max].  

There are several standard function of Stack .

Initialize(); 
  • Initializing means assigning -1 to top .
Push();
  • Incrementing top , and assigning the value to array[top].
Pop();
  • Return/print the top most element and decrease top by 1.
Peep();
  • Print the top most element 
Traverse(); 
  • Print the whole stack from top to bottom . 
Now lets look at the code : 


//stack
//implementation using array


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

#define max 10

void pop();
void push();
void peep();
void traverse();
void initialize();

int stack[max];
int top;

void main()
{
int choice;

while(1){

    printf("\nChoose from the following :");
    printf("\n1.Initialize");
    printf("\n2.Push");
    printf("\n3.Pop");
    printf("\n4.Peep");
    printf("\n5.Traverse");
    printf("\n6.Exit\n");
    scanf("%d",&choice);
    switch(choice)
    {

    case 1:
    initialize();break;
    case 2:
    push();break;
    case 3:
    pop();break;
    case 4:
    peep();break;
    case 5:
    traverse();break;
    case 6:
    return ;
    default :
    printf("Thank you !");
    }
}
}
void initialize()
{
top = -1;
}
void push()
{
if(top==max-1)
    printf("\nStack is full : Overflow\n");
else{
printf("\nEnter an integer:\n");
top++;
scanf("%d",&stack[top]);}
}
void pop()
{
if(top==-1)
    printf("\nStack is empty : Underflow\n");
else{
printf("\npopped element : %d\n",stack[top]);
top--;}
}
void peep()
{
printf("\n%d\n",stack[top]);
}
void traverse()
{
printf("Here is the stack\n");
int i;
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}

Thanking you !

Wednesday, 27 July 2016

C program to find 2nd minimum and 2nd maximum

Hello friends !
This program sounds quite simple but its little confusing rather , the algorithms i had used before were appropriate for some cases only . I guess this program contains no error or if any just let me know .


#include<stdio.h>
#define largest_value 1000
int main()
{
int i,j,k,n;
int max,min,max2,min2,temp1,temp2;
printf("Enter size of array :\n");
scanf("%d",&n);
int array[n];
printf("Enter the elements of array:\n");

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

max=max2 = array[0];
min=min2= largest_value;
for(i=0;i<n;i++)
{
    if(array[i]<min)
    {
    min=array[i];
    }

    if(array[i]>max)
    {
    temp2=max;
    max=array[i];
    max2=temp2;
    }
}
for(i=0;i<n;i++)
{ if(array[i]<min2 && array[i]!=min)
    min2=array[i]; }

printf("max %d 2nd max %d min %d 2nd min %d \n",max,max2,min,min2);
}

 /*
accessing array by address
Q3 Find second largest and second smallest element from array.
*/

#include<stdio.h>
#define largest_value 1000
int minmax2();
int main()
{
int i,j,k,n;
int max,min,max2,min2,temp1,temp2;
printf("Enter size of array :\n");
scanf("%d",&n);
int array[n];
printf("Enter the elements of array:\n");

for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
minmax2(array,n,&min,&min2,&max,&max2);
}

//function

int minmax2(int *array,int n,int *min,int *min2,int *max,int *max2)
{
    int i,temp1,temp2;
    *max=*max2 = *array;
    *min=*min2= largest_value;
    for(i=0;i<n;i++)
    {
    if(*(array+i)<*min)
    {
    *min=*(array+i);
    }

    if(*(array+i)>*max)
    {
    temp2=*max;
    *max=*(array+i);
    *max2=temp2;
    }
    }
    for(i=0;i<n;i++)
    { if(*(array+i)<*min2 && *(array+i)!=*min)
    *min2=*(array+i); }

    printf("2nd max %d \n2nd min %d \n",*max2,*min2);
}




Thanking you !

Tuesday, 26 July 2016

Checking minimum and maximum of an array using functions : C program

/*
 Write a function which return min and max value from an array
*/
#include<stdio.h>
void minmax();
void main()
{
int i,n,minValue,maxValue;
printf("Enter the size of array:\n");
scanf("%d",&n);
int array[n];
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
minValue = array[0];maxValue=array[0];
minmax(array,n,&minValue,&maxValue);
printf("Minimum value is %d and maximum value is %d ",minValue,maxValue);
}

void minmax(int* array,int n,int* minValue,int* maxValue)
{
int i;
for(i=0;i<n;i++)
{
    if(*(array+i) < *minValue )
        *minValue = *(array+i);
    if(*(array+i) > *maxValue )
        *maxValue = *(array+i);
}
}