Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Write C++ program to sort an array in ascending order

#include <iostream>
using namespace std;
 
int main()
{
    int arr[100];
    int size, i, j, temp;
 
    // Reading the size of the array
    cout<<"Enter size of array: ";
    cin>>size;
 
    //Reading elements of array
    cout<<"Enter elements in array: ";
    for(i=0; i<size; i++)
    {
        cin>>arr[i];
    }
    //Sorting an array in ascending order
    for(i=0; i<size; i++)
    {
        for(j=i+1; j<size; j++)
        {
            //If there is a smaller element found on right of the array then swap it.
            if(arr[j] < arr[i])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    //Printing the sorted array in ascending order
    cout<<"Elements of array in sorted ascending order:"<<endl;
    for(i=0; i<size; i++)
    {
        cout<<arr[i]<<endl;
    }
 
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: file open cpp 
Cpp :: abs in c++ 
Cpp :: opencv c++ image write 
Cpp :: c++ fizzbuzz 
Cpp :: change integer to string c++ 
Cpp :: c++ init multidimensional vector 
Cpp :: c++ prime sieve 
Cpp :: less than operator overloading in c++ 
Cpp :: C++ array sort method 
Cpp :: why are inline keyword in header c++ 
Cpp :: why we use iostream in C++ programming 
Cpp :: how to add an element to std::map 
Cpp :: delete specific row from dynamic 2d array c++ 
Cpp :: how to create a vector in c++ 
Cpp :: struct and pointer c++ 
Cpp :: sort index c++ 
Cpp :: cout hex c++ 
Cpp :: cpp pushfront vector 
Cpp :: c++ array size 
Cpp :: print octal number in c++ 
Cpp :: c++ fstream create if not exists 
Cpp :: how to print a text in c++ 
Cpp :: panic: assignment to entry in nil map 
Cpp :: cpp func as const 
Cpp :: string search c++ 
Cpp :: c++ squaroot 
Cpp :: c++ program to convert kelvin to fahrenheit 
Cpp :: find in unordered_map c++ 
Cpp :: pointer cpp 
Cpp :: c++ class template 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =