Void and NULL pointer C progarm

This program demonstrates void pointer and null pointer in C. A pointer is a variable which stores the address of another variable. A null pointer does not point to anything and a void pointer can be used to point to a variable of any data type. In the program p is the null pointer and p1 is the void pointer, p is assigned with the value NULL and p1 is assigned with the address of the variable a, which is of integer data type. The statement (int *)p1; is to indicate that p1 refers to an integer data type.
 

Program


//C program to show the working of Void pointer and NULL pointer

#include<iostream.h> #include<conio.h> void main() { int *p=NULL,a; void *p1; cout<<"Enter the value for a"; cin>>a; p=&a; cout<<"Value of null pointer= "<<p; cout<<"Value of void pointer="<<(int *)p1; getch(); }

C++ program to implement linear search

This C++ program implements linear search. In this method all the elements in the list will be checked in a sequential order, to check for a match with the element to be searched. If a match is found the iteration will be stopped and the element will be returned with its position. The program reads the size of the array in the variable n, then the elements are read into the array a[] by using a for loop. Next the element to be searched is read into the variable elem. Then the program uses a for loop to check all the elements in the array with the value of elem to find a match. 

 

Program


#include<iostream.h>
#include<conio.h>
void main()
{
          int a[50],f=0,elem,*p,i,n;
          cout<<"Enter the size of the array: ";
          cin>>n;
          p=a;
          cout<<"Enter the elements: ";
          for(i=0;i<n;i++)
          {
                   cin>>a[i];
          }
          cout<<"Enter the element to be searched: ";
          cin>>elem;
          for(i=0;i<n;i++)
          {
                   if(*p==elem)
                             {
                             cout<<"\nItem Found at position "<<i+1;
                             f=1;
                             break;
                             }
                   p++;
          }
          if(f==0)
          cout<<"\nItem not found";
          getch();
}


C++ program for linear search
Linear search in C++

C++ program to delete an element from an array

This C++ program deletes an element from an array. The size of the array is read as input into the variable n. Then the elements are read into the array a[] using a for loop.  Then the position of the element to be deleted is read into the variable pos. The elements which lies after the element to be deleted is shifted one position backward, overwriting the element to be deleted. Then the new array is displayed to the output screen.


Program


//Delete element from array c++ program

#include<iostream.h>
#include<conio.h>
void main()
{
          int a[50],n,i,*p,pos;
          cout<<"Enter the value for n: ";
          cin>>n;
          cout<<"\nEnter the elements\n";
          for(i=0;i<n;i++)
          {
                   cin>>a[i];
          }
          cout<<"\nEnter the position of element to delete: ";
          cin>>pos;
          p=&a[pos];

          for(i=pos;i<n;i++)
          {
                   *(p)=*(p+1);
                   p++;
          }
          p=a;
          cout<<"\nArray after deletion\n\n";
          for(i=0;i<n-1;i++)
          {
                   cout<<*(p+i)<<"\t";
          }
          getch();
}



Delete an element from an array C++ progarm
C++ - delete an element from an array

C++ program to insert an element to an array

This c++ program inserts an element into an array in a given position. The variable n reads the limit of the array. Then the numbers are read one by one into the array a[]. Next the element to be inserted and the position of insertion is read to the variables elem and pos. Then the program uses a for loop to make space of the new element in the array and the new element is inserted into the given position and the new array is displayed to the output screen.   


Program


#include<iostream.h>
#include<conio.h>
void main()
{
          int a[50],n,i,*p,pos,elem;
          cout<<"Enter the value for n: ";
          cin>>n;
          cout<<"\nEnter the elements\n";
          for(i=0;i<n;i++)
          {
                   cin>>a[i];
          }
          cout<<"\nEnter the element and position to insert: ";
          cin>>elem>>pos;
          p=&a[n];
          for(i=n;i>pos;i--)
          {
                   *(p)=*(p-1);
                   p--;
          }
          a[pos]=elem;
          p=a;
          cout<<"\nArray after insertion\n\n";
          for(i=0;i<=n;i++)
          {
                   cout<<*(p+i)<<"\t";
          }
          getch();
}


Insert an element to an array in c++
C++: Insert an element into an array

C++ program to swap 2 arrays

This C++ program swaps two integer arrays using pointers. Two arrays a[] and b[] are declared which stores the two input arrays. The pointers p1 and p2 is initially set to point to the first index of both the arrays a[] and b[]. Then the program uses a for loop to swap values in the two arrays. After swapping is complected the new arrays are displayed to the output screen.


Program



//program to swap array's using c++

#include<iostream.h>
#include<conio.h>
void main()
{
int *p1,*p2,a[50],b[50],n1,n2,i=0,t;
cout<<"Enter the size of first array: ";
cin>>n1;
p1=a;
cout<<"Enter the elements:\n";
for(i=0;i<n1;i++)
{

cin>>a[i];
}
cout<<"Enter the size of second array: ";
cin>>n2;
p2=b;
cout<<"Enter the elements:\n";
for(i=0;i<n2;i++)
{
cin>>b[i];
}
for(i=0;i<n1|i<n2;i++)
{
t=*p1;
*p1=*p2;
*p2=t;
p1++;
p2++;
}
p1=a;
p2=b;
cout<<"Arrays after swapping\n";
cout<<"Array A\n";
for(i=0;i<n2;i++)
{
cout<<*p1<<" ";
p1++;
}
cout<<"Array B\n";
for(i=0;i<n1;i++)
{
cout<<*p2<<" ";
p2++;
}
getch();
}

Output



program in c++ to swap arrays
C++ program to swap two arrays

C++ Program to find the cube of a number using void pointer

This program finds the cube of a number in C++. The number is read as input and is stored is the variable n. The program uses a void pointer which points to n.  The statement int *pint= (int *)p; stores the value at p to the pointer variable pint. Then the program repeatedly multiples pint 3 times which gives the cube of the given number.



Program



#include<iostream.h>



#include<conio.h>



void main()



{



          int n;



          void *p;



          p=&n;



          cout<<"Enter the number";



          cin>>n;



          int *pint= (int *)p;



          n=(*pint)*(*pint)*(*pint);



          cout<<"Cube of the number is\n";



          cout<<n;



          getch();



}


C program to sort using quick sort

This program implements quick sort in C.  Quick sort is also known as partition exchange sort. In the best case quick sort makes O(n log n) comparisons and in the worst case it makes O(n2) comparisons. This algorithm selects a random number from the list as a pivot element, all the elements which are smaller than the pivot element will be shifted to the left of the pivot element and all the elements which are greater than the pivot element will be shifted to the right of the pivot element. Now the pivot element is in its final position, the algorithm repeatedly applies the above technique to the left part and the right part until the list is sorted.

 Program


//c program for sorting using quick sort
#include<stdio.h>
#include<conio.h>
int partition(int a[],int l,int r);
void quicksort(int a[],int l,int r);
void main()
{
int a[10],n,i,j,l,r,pivot;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
a[n]=1000;
l=0;
r=n;
quicksort(a,l,r);
printf("Array after sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
void quicksort(int a[10],int l,int r)
{
int pivot1;
if(l<r)
{
pivot1=partition(a,l,(r+1));
quicksort(a,l,pivot1-1);
quicksort(a,pivot1+1,r);
}
}
int partition(int a[10],int l,int r)
{
int i,j;
int pivot,t;
i=l+1;
j=r-1;
pivot=a[l];
do
{
while((a[i]<=pivot)&&(i<=r))
i++;
while((a[j]>pivot)&&(j>l))
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}}
while(i<=j);
t=a[l];
a[l]=a[j];
a[j]=t;
return j;
}

Output


Quick sort program in c
Quick Sort

C program to sort using merge sort

This program demonstrates merge sort in C.  Merge sort is based on divide and conquer algorithm. In the best case merge sort makes O(n log n) or O(n) comparisons and in the worst case is makes O(n log n) comparisons. The first step in merge sort is to divide the input list into sub lists which contains only one element. Then the algorithm repeatedly merges the sub lists into sorted list until there is only one list. Merge sort can be either bottom up or top down implementation.


Program



//Merge Sort c progarm 

#include<stdio.h>

#include<conio.h>

#include<math.h>

void mergesort(int a[],int p,int q);

void merge(int a[],int p,int q,int r);

void main()

{

int n,a[10],i;

printf("Enter the size of the array: ");

scanf("%d",&n);

printf("Enter the elements\n");

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

scanf("%d",&a[i]);

mergesort(a,0,n-1);

printf("Array after sorting\n");

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

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

getch();

}

void mergesort(int a[10],int p,int r)

{

int mid;

if(p<r)

{

mid=(floor(p+r))/2;

mergesort(a,p,mid);

mergesort(a,mid+1,r);

merge(a,p,mid,r);

}

}

void merge(int a[10],int p,int q,int r)

{

int i=p,j=q+1,k=p,b[10];

while((i<=q)&&(j<=r))

{

if(a[i]<=a[j])

b[k++]=a[i++];

else

b[k++]=a[j++];

}

while(i<=q)

b[k++]=a[i++];

while(j<=r)

b[k++]=a[j++];

for(k=p;k<=r;k++)

a[k]=b[k];

}


Output


c program for merge sort
Merge Sort