0% found this document useful (0 votes)
42 views2 pages

Practice Lab Activity 6

This C++ program sorts user-inputted numbers into ascending and descending order using vectors and functions. The user enters the number of values to sort and then those values. The values are stored in a vector and passed to sorting functions to sort in ascending and descending order, then printed. The sorting functions use nested for loops and swapping to sort the vectors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views2 pages

Practice Lab Activity 6

This C++ program sorts user-inputted numbers into ascending and descending order using vectors and functions. The user enters the number of values to sort and then those values. The values are stored in a vector and passed to sorting functions to sort in ascending and descending order, then printed. The sorting functions use nested for loops and swapping to sort the vectors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Alysza Ashley M.

Daep AMACC BIÑAN


20000511010 Computer Programming 1

Practice Lab Activity #6

#include <iostream>
#include <vector>
#include <utility>
using namespace std;
void sort_ascending(vector<int> & v);
void sort_descending(vector <int> & v);

int main()
{

size_t n;
cout << "enter n for the numbers you want to sort" << endl;
cin >> n;
cout << endl;

vector<int> v(n);

for (size_t i =0; i < n; ++i)


{
cout << "enter number " << (i+1) << " of " << n << endl;
cin >> v[i];
}
cout << "ascending:\n";
sort_ascending( v);
for (auto x : v)
cout << x << "\n";

cout << "descending:\n";


sort_descending(v);
for (auto x : v)
cout << x << "\n";
}

void sort_ascending( vector<int> & v)


{
for (size_t i=0; i < v.size()-1; ++i)
for (size_t j= i+1; j < v.size(); ++j)
if ( v[i] > v[j] ) swap(v[i], v[j]);
}

void sort_descending( vector<int> & v)


{
for (size_t i=0; i < v.size()-1; ++i)
for (size_t j= i+1; j < v.size(); ++j)
if ( v[i] < v[j] ) swap(v[i], v[j]);
}

You might also like