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";

Recommended Answers

All 2 Replies

>>for(int high = n-1; i= low; high-- )

Check the condition. The expression syntax is for(variable decleration,condition, operations) When you say "i = low" as a condition, you are saying "i = 0" since low is 0, which is false since 0 is considered false.

In other words, I need to rearrange all the elements of the vector

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.