C++ Program to Read Infinite Numbers and Arrange Them in Ascending Order using Pointers

C++ Program to Read Infinite Numbers and Arrange Them in Ascending Order using Pointers: We are reading infinite numbers and then arranging them in ascending order.

C++ Program to Read Infinite Numbers and Arrange Them in Ascending Order using Pointers

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
   int *p,*q,i=1,j,k,temp;
   cout<<"Enter Infinite Numbers and (-1 To Stop Reading)\n";
   p=(int*)malloc(sizeof(int));
   cin>>p[0];
   while(p[i-1]!=-1)
   {
    i++;
    p=(int*)realloc(p,sizeof(int)*i);
    q=p;
    cin>>p[i-1];
   }
    p=q;
  
   for(j=1;j<i;++j)
   {
    for(k=0;k<i-j-1;++k)
    {
     if(p[k]>p[k+1])
     {
      temp=p[k];
      p[k]=p[k+1];
      p[k+1]=temp;
     }
    }
   }
    cout<<"\nAscending Order Is Given Below \n\n";
 for(j=0;j<i-1;++j)
   {
    cout<<" "<<p[j];
   }
}

Output Of Program

Enter Infinite Numbers and (-1 To Stop Reading)
1
2
7
-2
-1

Ascending Order Is Given Below

-2 1 2 7