-1

i have an array of 9 numbers and my functions in my number class are used to reorder the 9 numbers in the array without any duplicate of numbers and then to list the number of times the rand() function was called. I now need to generate ten lists of the numbers and store them into a vector. here is the code:

class numbers{
    private:
        int indexCount;


    public:
        void swap (int *a, int *b)  
{  
    int temp = *a;  
    *a = *b;  
    *b = temp;  
}  
void printArray (int arr[], int n)  
{  
    for (int i = 0; i < n; i++)  
        cout << arr[i] << " ";  
    cout << "random calls: " << indexCount <<endl; 

}  
  void randomize (int arr[], int n)  
{  
   indexCount=0;
    srand (time(NULL));  


    for (int i = n - 1; i > 0; i--)  
    {  

        int j = rand() % (i + 1);  
        indexCount++;



        swap(&arr[i], &arr[j]);  
    }  
}


};

class numbersVector{
    private:

            vector <int>numberList;
    public:
        numbers num;
        void storeInVector(int arr[], int n){
            for (int i=0; i <10; i++){

                num.randomize(arr,n);
                num.printArray(arr,n);
                numberList.push_back(arr[i]);

            }
            cout <<endl <<endl;


        }
};
int main(){
    numbers numbers;
    numbersVector nv;
     int arr[] = {1, 2, 3, 4, 5, 6, 0, 0, 0};  
    int n = sizeof(arr) / sizeof(arr[0]);  
    //numbers.randomize(arr, n);  
   // numbers.printArray(arr, n);  
    nv.storeInVector(arr,n);
    return 0;
}

in my second class i loop over my functions in the first class to generate 10 list, i am now stuck on storing the randomised list into a vector. My problem is, i can only store numbers from each list into the vector but i would like to store the 10 lists inside the vector. EG

for (int i = 0; i <numberList.size(); i++)
{
cout <<numberList[i] <<endl;
}

i would want my output to be:

123456000 random calls: 8
02103654 random calls:8

and so on 10 times.

EDITING POST TO BE MORE CLEAR: so i have an array

arr[] = {1,2,3,4,5,6,0,0,0}

after my randomise function i get a output like this {2,1,0,0,6,0,4,3,5} i then create a loop to run my randomise function 10 times. ie 1)1,0,0,0,2,5,4,3,6 2)6,0,5,0,4,0,3,2,1 3)5,1,0,0,2,0,3,6,4 .... 10)2,1,0,0,6,0,4,3,5

i would then like to store each generated list into a vector IE

vector <int> numlist;
numList[1] = {1,0,0,0,2,5,4,3,6}
numList[2] = {6,0,5,0,4,0,3,2,1}

and so on

willzz100
  • 63
  • 6
  • 1
    Do you want a vector of vectors? If so, what's stopping you from creating and using one exactly? – David Schwartz Mar 05 '20 at 21:49
  • 1
    Regarding `randomize`, note that there's a simple built-in way to shuffle a vector (that will likely be better at being random than anything you could come up with): [How to shuffle a std::vector?](https://stackoverflow.com/a/6926473/2602718) – scohe001 Mar 05 '20 at 21:51
  • @scohe001 thanks for that, i am aware of that but the task did not want us to use shuffle functions – willzz100 Mar 05 '20 at 21:53
  • @willzz100 This question is still listed as unanswered. I wonder, did I leave anything out in my answer? – Ted Lyngmo Mar 08 '20 at 10:01

1 Answers1

0

Lists aren't flattened in C++ as they are in some languages (notably perl). You need to specify where you want the elements in your new list inserted and which range you want to insert:

numberList.insert(numberList.end(), std::begin(new_list), std::end(new_list);

This would insert the full range of new_list at the end of your numberList.

Also: As @scohe001 mentioned, use <random> supported functions to get good randomization.

Example:

#include <random>

inline std::mt19937& generator() {
    static thread_local std::mt19937 gen(std::random_device{}());
    return gen;
}

//...

    std::shuffle( numberList.begin(), numberList.end(), generator() );

Example after comments:

#include <algorithm>
#include <iostream>
#include <random>
#include <vector>

inline std::mt19937& generator() {
    static thread_local std::mt19937 gen(std::random_device{}());
    return gen;
}

int main() {
    // a vector of vectors (not the most optimal but for this purpose, good enough)
    std::vector<std::vector<int>> result;

    std::vector<int> array{1, 2, 3, 4, 5, 6, 7, 8, 9};

    for(int i = 0; i < 10; ++i) {
        std::shuffle(array.begin(), array.end(), generator());

        // pushing back an array (or better, a vector) into the vector
        result.push_back(array);
    }

    //-- present the result
    std::cout << result.size() << " random permutations (possibly duplicates):\n";
    for(const auto& inner : result) {
        for(int value : inner) {
            std::cout << value << ' ';
        }
        std::cout << '\n';
    }
}

Demo

If you can't use std::shuffle for some reason, you can make your own by adding a function to generate random numbers and then one to loop over the vector and swap random positions in it.

Example:

// generate a random number in the range [min, max]
template<typename T, std::enable_if_t<std::is_integral_v<T>>* = nullptr>
T my_rand(T min, T max) {
    std::uniform_int_distribution<T> dist(min, max);
    return dist(generator());
}

// a shuffle-like function
void shake_it(std::vector<int>& c) {
    for(size_t i = 0; i < c.size(); ++i) {
        std::swap(c[i], c[my_rand(0ul, c.size() - 1)]);
    }
}

Then call it with shake_it(array); and it should be properly randomized.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Im not too sure if this answers my question. So i have an array which has 9 elements placed in random order. I would like to display the same array 10 times all with different orders ( which i have managed to do) and then store which array in a vector – willzz100 Mar 05 '20 at 22:41
  • @willzz100 Ok, that's not what I read into the question, but sure. if you are willing to use `std::shuffle` that's easy. I'll add an example for that case if that's what you asked for. – Ted Lyngmo Mar 05 '20 at 22:47
  • i have edited my post, hopefully it makes more sense – willzz100 Mar 05 '20 at 23:05
  • @willzz100 Ok, but that's _exactly_ what my added example does, isn't it? Did you click the [Demo](https://godbolt.org/z/P-AHN6) link? (given, I did not use the numbers you used - because I did not know them) – Ted Lyngmo Mar 05 '20 at 23:12
  • i understand that, i am just unable to use the shuffle function – willzz100 Mar 05 '20 at 23:15
  • @willzz100 I added a `std::shuffle` replacement. I hope that'll work. – Ted Lyngmo Mar 06 '20 at 13:51