Write a program that gets five inputs from the user in an array and then sorts this array in ascending order.





#include <iostream>
using namespace std;
int main (){
    int arr[5],i,j,min,temp;
    for(i=0; i<5; i++)
    {
        cout<<"enter value:";
        cin>>arr[i];
    }
    cout<<"the original values in array:\n";
    for(i=0;i<5;i++)
        cout<<arr[i]<<" ";
    for (i=0;i<4;i++)
    {
        min=i;
for(j=i+1;j<5;j++)
if(arr[j] < arr[min])

              min=j;
      if (min !=i)
      {
          temp= arr[i];
          arr[i]=arr[min];
          arr[min]=temp;
      }
     }
     cout<<"\n The sorted array;\n";
     for(i=0;i<5;i++)
       cout<<arr[i]<<" ";
       
    }

        

Output:

Enter value:56
Enter value:78
Enter value:33
Enter value:81
Enter value:12
The original values in array;
56 78 33 81 12
The sorted array:
12 33 56 78 81

Comments

Popular posts from this blog

C++ Templates ?

Function Overriding in C++ [With Example]

C++ Program to Print Number Entered by User