Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Written Assignment: Permutations • Write a program that produces ten random perm

ID: 3640264 • Letter: W

Question

Written Assignment: Permutations
• Write a program that produces ten random permutations of the numbers 1 to 10. To generate a random permutation, you need to fill a vector with the numbers 1 to 10 so that no two entries of the vector have the same contents. You could do it by brute force, by calling rand_int until it produces a value that is not yet in the vector. Instead, you should implement a smart method.
• Make a second array and fill it with the numbers 1 to 10. Then pick one of those at random, remove it, and append it to the permutation vector.
• Repeat ten times.

My code so far with multiple syntax errors -

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;

class PermutationGenerator
{
public:
PermutationGenerator(int lsize);
PermutationGenerator();

vector<int> nextPermutation();
private:
void int(lsize);

vector<int> randPermutation;
vector<int> intList;
int listSize;
}

PermutationGenerator:PermutationGenerator(int lsize)
{
int(lsize);
}

PermutationGenerator:PermutationGenerator()
{
int(10);
}

void PermutationGeneratorinit(int lsize)
{
listSize = lsize;
}

vectorint PermutationGenerator;nextPermutation()
{
// Fill the ArrayList with the numbers 1 to 10

intList.clear(); // loop starts with 1
for (int i = 1; i = listSize; i++)
intList.push_back(i);

randPermutation.clear();
for (int i = 1; i = listSize; i++)
{
// Pick a number at random, removing a number from the intList
int index = rand() % (intList.size());
int number = intList[index];
intList.erase(intList.begin() + index);

// Add and append the random number to the ArrayList, Repeated 10 times

randPermutation.push_back(number);
}

return randPermutation;
}

Explanation / Answer

I added an extra private method wait() to assist nextPermutation() method generate different permutations.

#include <iostream>
#include <vector>
#include <ctime>
using namespace std;

class PermutationGenerator
{
public:
   PermutationGenerator(int lsize = 10);
   ~PermutationGenerator();
   vector<int>& nextPermutation();
private:
   void wait(int sec = 0); //to support nextPermutation() method
private:
   vector<int> randPermutation;
   vector<int> intList;
   int listSize;
};

//------------------------------------------
PermutationGenerator::PermutationGenerator(int lsize)
:listSize(lsize)
{
   srand(time(NULL));
}
//------------------------------------------
PermutationGenerator::~PermutationGenerator()
{
   intList.clear();
   randPermutation.clear();
}
//------------------------------------------
vector<int>& PermutationGenerator::nextPermutation()
{
   // Fill the ArrayList with the numbers 1 to 10
   intList.clear(); // loop starts with 1
   for (int i = 1; i <= listSize; i++)
      intList.push_back(i);

   wait();
   randPermutation.clear();
   for (int i = 1; i <= listSize; i++)
   {
      // Pick a number at random, removing a number from the intList
      int index = rand() % intList.size();
      int number = intList[index];
      intList.erase(intList.begin() + index);

      // Add and append the random number to the ArrayList, Repeated 10 times
      randPermutation.push_back(number);
   }

   return randPermutation;
}
//------------------------------------------
void PermutationGenerator::wait(int sec)
{
   time_t starter = time(NULL);
   while (time(NULL) < starter + sec) {}
}


//MAIN (test driver)
int main()
{
   PermutationGenerator gen1;
   PermutationGenerator gen2(25);

   //random permutation of default generator (10 elements)
   vector<int> permu1 = gen1.nextPermutation();
   for (int i = 0; i < permu1.size(); i++)
      cout << permu1[i] << " ";
   cout << endl << endl;

   //next random permutation of default generator (10 elements)
   permu1 = gen1.nextPermutation();
   for (int i = 0; i < permu1.size(); i++)
      cout << permu1[i] << " ";
   cout << endl << endl;

   //next random permutation of default generator (10 elements)
   permu1 = gen1.nextPermutation();
   for (int i = 0; i < permu1.size(); i++)
      cout << permu1[i] << " ";
   cout << endl << endl;

   //random permutation of generator with 25 elements
   vector<int> permu2 = gen2.nextPermutation();
   for (int i = 0; i < permu2.size(); i++)
      cout << permu2[i] << " ";
   cout << endl << endl;

   //next random permutation of generator with 25 elements
   permu2 = gen2.nextPermutation();
   for (int i = 0; i < permu2.size(); i++)
      cout << permu2[i] << " ";
   cout << endl << endl;

   return 0;
}