//Permutation program
#include <iostream>
#include <vector>
#include<time.h>
using namespace std;
void main()
{
int len; //set asside space for input
vector <int> perm; //set asside space for permutation
srand(time(NULL));
cout <<"Welcome to the Random Permutation Generator Program\n\n Please enter your length for your permutation: ";
cin >> len; //sets the length for the permutation
cout << "\n\n";
for(int i=0; i<len;i++) //sets a generic sequence for the code
{
perm.push_back(i+1);
}
for(int i=0; i<len; i++)
{
int temp = perm[i]; //stores integer i in a temporary place
int j = rand()%(i+1); //selects a random digit
perm[i] = perm[j];
perm[j] = temp;
}
cout << "Here's your Random Permutation of length " << len << ":\n";
for(int i=0; i<(len-1); i++) //displays each position except the last with a comma and a space in between
{
cout << perm[i] << ", ";
}
cout << perm[len-1] << "\n\n"; //this displays the last position
system("pause");
}
int main() {
int len;
vector<int> perm;
srand(static_cast<int>(time(NULL)));
std::cout << "Welcome to the Random Permutation Generator Program\n\n Please enter your length for your permutation: ";
std::cin >> len; std::cout << "\n\n";
for( int i = 0; i < len; i++ )
perm.push_back(i+1);
for( int i=0; i<len; i++ ) {
int temp = perm[i];
int j = rand()%( i + 1);
perm[i] = perm[j]; perm[j] = temp;
}
std::cout << "Here's your Random Permutation of length " << len << ":\n";
for(int i = 0; i < (len-1); i++ )
std::cout << perm[i] << ", ";
std::cout << perm[len-1] << "\n\n"; //this displays the last position
// system("pause"); Do not use this. EVER
cin.ingore();
return 0;
}
//Permutation program
#include <iostream>
#include <vector>
#include<time.h>
usingnamespace std; //try not to include entire namespaces--ok for now
int main() //the standard--main should return an integer
{
srand(time(NULL));
int len;
vector <int> perm;
cout <<"Welcome to the Random Permutation Generator Program.\n\nPlease enter your length for your permutation: ";
cin >> len;
for(int i=0; i<len;i++) perm.push_back(i+1); //for-loops don't need brackets if they're doing 1 thing
for(int i=0; i<len; i++) { //randomly swap numbers--how about 'random_shuffle(perm.begin(), perm.end())' instead?
int j = rand()%(i+1);
int temp = perm[i];
perm[i] = perm[j];
perm[j] = temp;
}
cout << "\n\nHere's your Random Permutation of length " << len << ":\n"; //moved the first two endline characters here--same output
for(int i=0; i<(len-1); i++) cout << perm[i] << ", "; //as above, no brackets needed
cout << perm[len-1] << "\n\n";
system("pause"); //Look into cin.ignore(numeric_limits<streamsize>::max(), '\n') with cin.get()
return 0; //the standard
}