C++ program to allocate memory for n fractional numbers using new operator

//C++ program to allocate memory for n fractional numbers using new operator
//Store n number in allocated memory by reading from user
//and Display the numbers in ascending order

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int n,i,j;
    float *p;
    cout<<"How many numbers to enter?: ";
    cin>>n;
    p=new float[n];

    for(i=0;i<n;i++)
    {
        cout<<"Enter "<<i<<" number:";
        cin>>*(p+i); //cin>>p[i];
    }

    cout<<endl<<endl<<"Processing...."<<endl<<endl;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(*(p+i)>*(p+j))
            {
                float temp=*(p+i);
                *(p+i)=*(p+j);
                *(p+j)=temp;
            }
        }
    }

    cout<<"The ascending order is: "<<endl;
    for(i=0;i<n;i++)
    {
        cout<<*(p+i)<<setw(5);
    }
    
    delete []p;
    return 0;
}

No comments:

Post a Comment