Answer to Question #259863 in C++ for ahmed

Question #259863

Write a program to sort an array consists

of 10 integer numbers in ascending order,

print the array before sorting and after

sorting.


1
Expert's answer
2021-11-01T18:48:32-0400


#include <iostream>


using namespace std;


void sort(int arr[],int n){
    int temp;
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            if(arr[j] < arr[i])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}
int main()
{
    int n;
    cout<<"\nEnter number of elements in the array: ";
    cin>>n;
    cout<<"\nEnter elements of the array: \n";
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    
    cout<<"\nElenemts of the array BEFORE sorting:\n";
    for(int i=0;i<n;i++){
        cout<<arr[i]<<"\t";
    }
    
    sort(arr,n);
    cout<<"\nElements of the array AFTER sorting:\n";
    for(int i=0;i<n;i++){
        cout<<arr[i]<<"\t";
    }


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS