0% found this document useful (0 votes)
3 views3 pages

Chapter 1 c programming

This document provides a C++ program that sorts a user-defined number of integer elements in both ascending and descending order. It utilizes a nested loop for sorting, where elements are compared and swapped accordingly. The program prompts the user for input and displays the sorted results after each sorting operation.

Uploaded by

Nidhi Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Chapter 1 c programming

This document provides a C++ program that sorts a user-defined number of integer elements in both ascending and descending order. It utilizes a nested loop for sorting, where elements are compared and swapped accordingly. The program prompts the user for input and displays the sorted results after each sorting operation.

Uploaded by

Nidhi Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter 1

Sorting
Write a C++ program to sort the elements in ascending and descending order.

#include<iostream>

#include<iomanip>

#include<vector>

using namespace std;

int main()

vector<int> values;

int iNum, iElem;

cout << "Enter the number of elements : " ;

cin >> iNum;

cout << "Enter " << iNum << " values :";

for(int i=0;i<iNum;i++)

cin >> iElem;

values.push_back(iElem);

for(int i=0;i<iNum;i++)

cout << values[i] << " ";

cout << endl;

//sorting in ascending order

cout << "Sorting in ascending order" << endl;


for(int i=0;i<iNum;i++)

for(int j=0;j<iNum;j++)

if(values[i] < values[j])

int temp = values[i];

values[i] = values[j];

values[j] = temp;

for(int i=0;i<iNum;i++)

cout << values[i] << " ";

cout << endl;

//sorting in descending order

cout << "Sorting in descending order"<< endl;

for(int i=0;i<iNum;i++)

for(int j=0;j<iNum;j++)

if(values[i] > values[j])

int temp = values[i];

values[i] = values[j];

values[j] = temp;

}
}

for(int i=0;i<iNum;i++)

cout << values[i] << " ";

cout << endl;

return 0;

You might also like