Q:

C++ Program To Read Infinite Number And Sort In Ascending Order Using Pointer

-1
Logic:-
 We are using Malloc and realloc (reallocated memory during runtime ) after getting -1 stop inserting numbers and then use an any sorting algorithm and print the element of the array. if you don't know any sorting algorithm then you have to check here 

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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:

Enter Infinite Numbers and (-1 To Stop Reading)

12

09

23

78

45

56

25

58

96

52

-1

Ascending Order Is Given Below 

 9 12 23 25 45 52 56 58 78 96

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program To Find Average of An Array Elements U... >>
<< C++ Program To Check Even Or Odd Using Pointer...