I've a vector of size 'n', I need to do random couples with the n numbers (integers) and make a swap between them. Any ideas how?
I had this idea
int low = 0;
int temp;
for (int high = n-1; i= low; high-- )
{
temp = v[high];
v[low] = v[high];
v[high] = v[temp];
}
But when I print my vector, it steel show me the original order of the vector.
Here is the rest of my code:
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int n = 100;
vector<int> v(n);
int i, high, low=0, temp;
// Assign the elements of the vector some values
for(i=0; i<n; i++) v[i] = i;
// Display contents of vector
cout << "Current Contents:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << "\n\n";
// Swaps
for (int high = n-1; i= low; high-- )
{
temp = v[high];
v[low] = v[high];
v[high] = v[temp];
}
// Display contents of vector
cout << "New order of Contents:\n";
for(i=0; i<v.size(); i++) cout << v[i] << " ";
cout << "\n\n";