Generate Random 20 Non-repeating numbers in C++ - c++

So I have a program where I generate a 8x8 matrix which are rooms. So we have 64 rooms. Now I have to generate 20 random rooms to be dirty. I'm trying to figure out how I can generate 20 non repeating numbers to use as the dirty rooms. This is my code so far:
//generate the 20 random dirty rooms
int col;
int row;
for (int i = 0; i < 20; i++)
{
col = ranNumber();
row = ranNumber();
cout << "\t" << col << " " << row << endl;
if (room[row][col] == 'D')
{
cout << "Duplicate" << endl;
col = ranNumber();
row = ranNumber();
cout << "New number " << row << col << endl;
room[row][col] = 'D';
}
else
//set the room as dirty
room[row][col] = 'D';
}
*ranNumber():
int ranNumber() {
return rand() % 8;
}

Since you're not dealing with a particularly large data set, I'd suggest using std::shuffle. You'll want to initialize your rooms with 20 dirty (the positions don't matter, so do whatever is easiest), then let std::shuffle rearrange the rooms. This avoids you having to write your own loop in case you get poor luck with your random generator, and better expresses your intent.
Sample code:
int main() {
char rooms[8][8];
for(auto i = 0; i < 8; ++i) {
for(auto j = 0; j < 8; ++j) {
rooms[i][j] = (i == 0) ? 'D' : ' ';
}
}
printRooms(rooms);
std::random_device rd{};
std::default_random_engine re{rd()};
auto b = (char *) rooms;
std::shuffle(b, b + (8 * 8), re);
std::cout << "----------------------\n";
printRooms(rooms);
return 0;
}

You can create an array of room numbers (0-63) and use as a picking basket. Whenever a room has been picked, you swap that room out of the selectable range.
Example:
#include <algorithm> // fill_n
#include <iostream>
#include <numeric> // iota
#include <random> // mt19937, random_device, uniform_int_distribution
int main() {
std::mt19937 prng(std::random_device{}());
char room[8][8];
constexpr int room_count = 8 * 8;
std::fill_n(&room[0][0], room_count, '.'); // fill rooms with an initial value.
char numbers[room_count];
std::iota(std::begin(numbers), std::end(numbers), 0); // 0 - 63
for(int i = 1; i <= 20; ++i) {
int last_selectable_room = room_count - i;
std::uniform_int_distribution<int> dist(0, last_selectable_room);
auto& selected = numbers[dist(prng)];
*(&room[0][0] + selected) = 'D';
// swap the selected room number with the last selecable room number
// to make sure that the selected room can't be selected again
std::swap(selected, numbers[last_selectable_room]);
}
}
Demo
This is likely going to be 2.3 - 2.4 times faster than the std::shuffle approach if you use g++ or clang++. Benchmark

If selecting the dirty rooms uniformly at random is truly important, you could build a random permutation of the rooms using Knuth's shuffle (be careful, it is easy to blunder!) and picking e.g. the first 20 ones of the result as dirty.

You could do it using std::shuffle() and a single-dimension array, exploiting the fact that an m x n matrix can be represented as an array containing m*n elements
#include <vector>
#include <random>
#include <algorithm>
class random_room_generator {
public:
random_room_generator(const size_t matrixRows, const size_t matrixColumns) :
m_matrixRows(matrixRows),
m_randomRoomList(),
m_nextRoom(0)
{
// Create a list of all the room numbers.
const size_t totalRooms = matrixRows * matrixColumns;
for (size_t i = 0; i < totalRooms; ++i) {
m_randomRoomList.push_back(i);
}
// Shuffle the list.
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(m_randomRoomList.begin(), m_randomRoomList.end(), g);
}
std::pair<size_t, size_t> next() {
// Get the room number:
const size_t roomNumber = m_randomRoomList[m_nextRoom++];
if (m_nextRoom == m_randomRoomList.size()) {
// Loop back round to the start if we get to the end of the shuffled list.
m_nextRoom = 0;
}
// Convert the room number into a row and column using the modulus/division operators:
return std::pair<size_t, size_t>(roomNumber % m_matrixRows, roomNumber / m_matrixRows);
}
private:
size_t m_matrixRows;
std::vector<size_t> m_randomRoomList;
size_t m_nextRoom;
};
Then, in your function, instead of calling ranNumber(), you can use an instance of random_room_generator to save a list of random rooms and then get 20 rooms from that list:
random_room_generator gen(8, 8);
for (int i = 0; i < 20; i++)
{
std::pair<size_t, size_t> roomCoords = gen.next();
const size_t row = roomCoords.first;
const size_t col = roomCoords.second;
cout << "\t" << col << " " << row << endl;
//set the room as dirty
room[row][col] = 'D';
}
You can find a working example here: https://godbolt.org/z/xKLmjm

Related

How to find a minimum and maximum value in a 2D array ROW?

I have a program that generates 10 rows and 5 columns and the user inputs data. My question is, how can I find the maximum and lowest values in each row? I have been working on this for a good hour but cannot figure this out. I have attempted to solve this many times; here is my current code.
#include <iostream>
#include <iomanip>
using namespace std;
int returnMax(int[][]);
int main()
{
double sales[10][5];
string name[10];
double highest;
double lowest;
double avg;
// Populating table
for (int row = 0; row < 1; row++)
{
cout << "Enter the salesman's name: " << endl;
cin >> name[row];
cout << "Enter the amount of sales for the five years" << endl;
for (int col = 0; col < 5; col++) {
cin >> sales[row][col];
}
}
cout << returnMax(sales[1][0]) << endl;
return 0;
}
int returnMax(int a[][])
{
int max;
for (int i = 0; i < 1; i++) {
max = a[i][0];
for (int j = 0; j < 5; j++) {
if (a[i][j] > max)
max = a[i][j];
}
}
return max;
}
Your logic here:
cout << returnMax(sales[1][0]) << endl;
is wrong. The sales[1][0] is only a single element of entire sales array. That means,
sales[1][0] = element of 1st row and 0th column in sales array in which you did not have any values initilized. Because you have initilzed only one single row in your entire array as you have the line:
for (int row = 0; row < 1; row++)
Remember in C++ indexing starts from 0 not from 1. That being said, the above result(uninitialized variables) will lead you to have undefined behavior.
Suggestions:
In modern C++ you have better options than using raw arrays. For
example, use of
std::vector<>
or std::array<>
makes your code both simpler and safer. In your case, you can either
have
std::vector<int> sales(50, 0) // one dimentional: with 10 * 5 entries
and manipulate the rows accordingly(see solution-1) or
std::vector<std::vector<int>> sales(10, std::vector<int>(5, 0));
// two dimensional: with 10 rows and 5 columns
and use range-based for loops so that, you never end up with
out of bounds problems(see solution-2).
Regarding finding the min and max of each row entries, you can
simply apply algorithm function called
std::minmax_element
from algorithm header.
Sample solution - 1
A sample solution using one-dimensional vector array would look like this: SEE LIVE
#include <iostream>
#include <vector> // std::vector
#include <algorithm> // std::minmax_element
#include <string>
int main()
{
constexpr std::size_t rawMax = 2;
constexpr std::size_t colMax = 5;
// one dimentional array with size = (rawMax * colMax)
std::vector<int> sales(rawMax * colMax, 0);
std::vector<std::string> name(rawMax);
// Populating table
for (std::size_t row = 0; row < rawMax; ++row)
{
std::cout << "Enter the salesman's name: "; std::cin >> name[row];
std::cout << "Enter the amount of sales for the five years: " ;
for (std::size_t col = 0; col < colMax; ++col)
std::cin >> sales[(row*colMax) + col]; // convert col and raw to 1D index.
}
/// get the begin and end of each row as iterators
auto rowBeginIter = sales.begin();
auto rowEndIter = sales.begin() + colMax - 1;
for (const std::string& str: name)
{
std::cout << "salesman's name: "; std::cout << str;
auto getMinMaxRow = std::minmax_element(rowBeginIter, rowEndIter + 1);
std::cout << " min: " << *getMinMaxRow.first
<< " max: " << *getMinMaxRow .second << std::endl;
rowBeginIter += colMax; // increment both iterator to the next raw
rowEndIter += colMax;
}
return 0;
}
Sample solution - 2
A sample solution using a vector of vectors(2D) would look like this: SEE LIVE
#include <iostream>
#include <vector> // std::vector
#include <algorithm> // std::minmax_element
#include <string>
int main()
{
constexpr std::size_t rawMax = 2; // to test
constexpr std::size_t colMax = 5;
// initilize thw 2D vector of vectors with (rawMax x colMax)
std::vector<std::vector<int>> sales(rawMax, std::vector<int>(colMax, 0));
// initilize with 0's with a size that of maximum number of rows.
std::vector<std::string> name(rawMax, "");
// Populating table
for (std::size_t row = 0; row < rawMax; row++)
{
std::cout << "Enter the salesman's name: "; std::cin >> name[row];
std::cout << "Enter the amount of sales for the five years: " ;
for (std::size_t col = 0; col < colMax; col++) {
std::cin >> sales[row][col];
}
}
/* print max and min of each person
* use range based for loops to loop through them
* (optional: index based loops can also be used like above)
*/
auto nameIter = name.cbegin();
for(const std::vector<int>& each_row: sales)
{
std::cout << "salesman's name: "; std::cout << *nameIter << "\t";
auto getMinMaxRow = std::minmax_element(each_row.cbegin(), each_row.cend());
std::cout << " min: " << *getMinMaxRow.first
<< " max: " << *getMinMaxRow.second << std::endl;
++nameIter; // increment the iterator of name-vector
}
return 0;
}
First of all, prepare your environment this way:
#define NROWS 10 //use a constant for number of rows
#define NCOLUMNS 5 // use a constant for number of columns
typedef int Matrix[NROWS][NCOLUMNS]; // declare the type Matrix which is 2d Array using NROWS and NCOLUMNS as size
int returnMaxForRow(int,Matrix); //If you want to know the max value of a row, you need to pass the row
so in the main you can do:
int main () {
Matrix sales; //You don't need to specify the size, which is done before
string name[10];
double highest;
double lowest;
double avg;
ecc....
Now your function should do this:
int returnMaxForRow (int row, Matrix a) {
int max = a[row][0];
for (int i = 0; i < NCOLUMNS; i++) {
if (a[row][i] > max){
max = a[row][i];
}
}
return max;
}
so you can call it this way:
cout<< returnMaxForRow(0,sales);
cout<< returnMaxForRow(1,sales);
cout<< returnMaxForRow(2,sales);
cout<< returnMaxForRow(3,sales);
Some Advices:
Use constants or variable to set array's index, such as define statement
When you do sales[1][0] you get a single value (row 1, column 0) and not all the row
Use typedef to declare custom array with different dimensions, it is easier to handle them this way
If you want, you can change the function to return all the max of all the rows.
If you want to get the max of the matrix, the approach is similar.

Copying elements from one array to another c++

I have looked and looked and am still lost on how to copy or get elements from an array and put them into new arrays ( divide and conquer is the goal).
I have an array that generates 100 random numbers. I need to split the random numbers into 4 smaller arrays obviously containing 25 elements and not have any duplicates. I have read about using pointers, but honestly I don't understand why even use a pointer. Why do I care about another variables address?
I don't know how to do this. Here is my code so far:
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
// Seed the random number generator
srand(time(NULL));
//create an array to store our random numbers in
int Orignumbers[100] = {};
// Arrays for the divide and conquer method
int NumbersA [25] = {};
int NumbersB [25] = {};
int NumbersC [25] = {};
int NumbersD [25] = {};
//Generate the random numbers
for(int i =0; i < 100; i++)
{
int SomeRandomNumber = rand() % 100 + 1;
// Throw random number into the array
Orignumbers[i] = SomeRandomNumber;
}
// for(int i = 0; i < ) started the for loop for the other arrays, this is where I am stuck!!
// Print out the random numbers
for(int i = 0; i < 100; i++)
{
cout << Orignumbers[i] << " , ";
}
}
"divide and conquer" is rather easy; when copying into NumbersA and so forth, you just have to access your Originnumbers with a proper offset, i.e. 0, 25, 50, and 75:
for(int i = 0; i < 25; i++) {
NumbersA[i] = Orignumbers[i];
NumbersB[i] = Orignumbers[i+25];
NumbersC[i] = Orignumbers[i+50];
NumbersD[i] = Orignumbers[i+75];
}
The thing about "no duplicates" is a little bit more tricky. Generating a random sequence of unique numbers is usually solved through "shuffling". Standard library provides functions for that:
#include <random>
#include <algorithm>
#include <iterator>
#include <vector>
int main()
{
std::random_device rd;
std::mt19937 g(rd());
int Orignumbers[100];
//Generate the random numbers without duplicates
for(int i =0; i < 100; i++) {
Orignumbers[i] = i+1;
}
std::shuffle(Orignumbers, Orignumbers+100, g);
// Arrays for the divide and conquer method
int NumbersA [25] = {};
int NumbersB [25] = {};
int NumbersC [25] = {};
int NumbersD [25] = {};
for(int i = 0; i < 25; i++) {
NumbersA[i] = Orignumbers[i];
NumbersB[i] = Orignumbers[i+25];
NumbersC[i] = Orignumbers[i+50];
NumbersD[i] = Orignumbers[i+75];
}
// Print out the random numbers
for(int i = 0; i < 100; i++)
{
cout << Orignumbers[i] << " , ";
}
}
Problem:
The program can't be guaranteed to have no duplicate value as the rand() function can generate any random sequence and that may include the decimal value of 99 for 99 times though probability is very low but chances are.
Example:
for(loop=0; loop<9; loop++)
printf("%d", Rand()%10);
If looped for 10 times, it may result some values like:
Output: 6,1,1,1,2,9,1,3,6,9
Compiled Successfully:
Hence, no certainity that values won't repeat
Possibly Solution:
There could be a solution where you can place the values in OriginalArray and compare the rand() generate values against the OriginalArray values.
For first iteration of loop, you can directly assign value to OriginalArray then from 2nd iteration of loop you've to compare rand() value against OriginalArray but insertion time consumption may be higher than O(NN) as rand() function may repeat values.
Possibly Solution:
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int Orignumbers[100] ;
int NumbersA [25] ,
NumbersB [25] ,
NumbersC [25] ,
NumbersD [25] ;
srand(time(NULL));
for(int i =0; i < 100; i++){
Orignumbers[i] = rand() % 100+1;
for(int loop=0; loop<i; loop++) {
if(Orignumber[loop] == Orignumber[i] ) {
i--;
break;
}
}
}
//Placing in four different arrays thats maybe needed.
for(int i = 0; i <25; i++ ) {
NumbersA[i] = Orignumbers[i];
NumbersB[i] = Orignumbers[i+25];
NumbersC[i] = Orignumbers[i+50];
NumbersD[i] = Orignumbers[i+75];
}
for(int i = 0; i < 99; i++)
cout << Orignumbers[i] << " , ";
}
As you tagged your question with C++ then forget about old-fashion arrays, let's do it C++ style.
You want to split your array into 4 arrays and they should not have duplicate numbers, so you can't have a number 5 times in your original array, because then surely one of your 4 arrays will have a duplicate one, So here is the way I propose to do it :
#include <set>
#include <ctime>
#include <vector>
int main() {
std::multiset<int> allNums;
std::srand(unsigned(std::time(0)));
for (int i = 0; i < 100; ++i) {
int SomeRandomNumber = std::rand() % 100 + 1;
if (allNums.count(SomeRandomNumber) < 4) {
allNums.insert(SomeRandomNumber);
}
else {
--i;
}
}
std::vector<int> vOne, vTwo, vThree, vFour;
for (auto iter = allNums.begin(); iter != allNums.end(); ++iter) {
vOne.push_back(*iter);
++iter;
vTwo.push_back(*iter);
++iter;
vThree.push_back(*iter);
++iter;
vFour.push_back(*iter);
}
system("pause");
return 0;
}
EDIT : As you mentioned in the comments, you just want to find a number in an array, so how about this :
for (int i = 0; i < 100; ++i) {
if (origArray[i] == magicNumber) {
cout << "magicNumber founded in index " << i << "of origArray";
}
}
On some situations, even on C++, the use of arrays might be preferable than vectors, for example, when dealing with multidimensional arrays (2D, 3D, etc) that needs to be continuous and ordered on the memory. (e.g. later access by other applications or faster exporting to file using formats such as HDF5.)
Like Jesper pointed out, you may use Copy and I would add MemCopy to copy the content of an array or memory block into another.
Don't underestimate the importance of pointers, they may solve your problem without the need doing any copy. A bit like Stephan solution but without the need of the index variable "i", just having the pointers initialized at different places on the array. For a very large number of elements, such strategy will save some relevant processing time.

c++ vector with random unique elements with constant sum

I have an std::vector with fixed size N = 5. I want every element of the vector to be randomly selected between two positive numbers, in particular 1 and 12. (zeros are not allowed).Each element should be unique on the vector.
How can I do this? The implementation so far allows elements to be zero and have duplicates in the vector. I want to improve in order not to allow zeros and duplicates
Code so far:
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <random>
int main() {
std::random_device rd;
std::mt19937 gen(rd());
constexpr int MAX = 20;
constexpr int LINES = 5;
int sum{};
int maxNum = 12;
int minNum = 1;
std::array<int, LINES> nums;
for (int i = 0; i < LINES; ++i) {
maxNum = std::min(maxNum, MAX - sum);
minNum = std::min(maxNum, std::max(minNum, MAX - maxNum * (LINES - i)));
std::cout << minNum << " " << maxNum << std::endl;
std::uniform_int_distribution<> dist(minNum, maxNum);
int num = dist(gen);
nums[i] = num;
sum += num;
}
std::shuffle(std::begin(nums), std::end(nums), gen);
std::copy(std::begin(nums), std::end(nums), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
Ok, there are three requirements:
Sum should be fixed
Non-repetitive numbers
Numbers should be in the range
For requirement #1 it is better sample from distribution which already have the property - Dirichlet distribution. For simplest case where all parameters are equal to 1, it is also known as simplex sampling, producing numbers uniformly distributed on N dimensional simplex. HEre is link to the C++ code.
To satisfy second&third requirement, just use acceptance/rejection
std::linear_congruential_engine<uint64_t, 2806196910506780709ULL, 1ULL, (1ULL<<63ULL)> ugen;
float MAX = 20.0f;
Simplex s(0.0f, 20.0f);
std::vector<float> v(5, 0.0f);
std::vector<int> r(5, 0);
for( ;; ) {
s.sample(v, ugen); // sampled Dirichlet
for(int k = 0; k != v.size(); ++k) {
r[k] = 1 + int(v[k]);
}
std::sort(r.begin(), r.end());
if (*r.rbegin() > 12) // array is sorted, just check last largest element
continue;
if (std::unique(r.begin(), r.end()) == r.end()) // no duplicates, good to go
break;
}
return r;
declare a boolean that starts at false, after that do a while that whill iterate until that flag is true, inside that while you will iterate until the sum of both of them = 5, you will check if the sum = 5 with an If statement, if they are then you will change the value of the flag to true. Easy as that.
I recommend you study a bit more, since you are probably new to programming (at least thats what the question makes me think, if you aren't then there is something clearly wrong).
Also, there ya go:
If statements
While loop

Using structs / genetic algorithm

As practice for myself I'm trying to create a genetic algorithm that will solve equations. So far my program can generate random "genes", fill up individuals with these "genes", and do some basic calculations with the genes (at the moment, simply summing the "genes").
However, I've realised now that I want to implement my fitness function that I would have been better off creating a struct for individual, since I need to keep the genes and the fitness outcome together to have the fittest genes reproduce again.
Anyway, here's my code:
// GA.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <random>
#include <string>
const int population_size = 10;
const int number_of_variables = 7;
struct one_individual
{
std::vector<std::vector<double>>individual;;
double evaluation = 0;
double fit = 0;
};
int main()
{
// Generate random number
std::random_device rd;
std::mt19937 rng(rd()); // random-number engine (Mersenne-Twister in this case)
std::uniform_real_distribution<double> dist(-10.0, 10.0);
// Create vector that holds vectors called individual and fill size it to the amount of individuals I want to have.
std::vector<std::vector<double>>individual;
for (int i = 0; i < population_size; i++)
{
std::vector<double>variables;
for (int j = 0; j < number_of_variables; j++)
{
variables.push_back(dist(rng));
}
individual.push_back(variables);
}
// Display entire population
for (auto &count : individual)
{
for (auto &count2 : count)
{
std::cout << count2 << " ";
}
std::cout << "\n";
}
// Do calculation with population. At the moment I just add up all the genes (sum) and display the sum for each individual.
for (int i = 0; i < population_size; i++)
{
int j = 0;
std::cout << "Organism "<< i;
double sum = individual[i].at(j) + individual[i].at(j + 1) + individual[i].at(j + 2) + individual[i].at(j + 3) + individual[i].at(j + 4) + individual[i].at(j + 5) + individual[i].at(j + 6);
std::cout << " is " << sum << "\n";
}
std::cout << "\n";
return 0;
}
What I think I should be doing is something like this:
for (int i = 0; i < population_size; i++)
{
one_individual individual;
std::vector<double>variables;
for (int j = 0; j < number_of_variables; j++)
{
variables.push_back(dist(rng));
}
one_individual.individual.push_back(variables);
}
The above code is not working. What happens when I try to compile is I get a list of errors, I just pasted it into pastebin since it's a pretty big list: www.pastebin.com/EVJaV0Ex. If I remove everything except the parts needed for the "creating individuals part" the errors that remain are: www.pastebin.com/djw6JmXZ. All errors are on line 41 which is the final line one_individual.individual.push_back(variables);
Edited for clarity, apologies that it was unclear.
Consider the instruction
one_individual.individual.push_back(variables);
where one_individual is a type (struct one_individual).
I suppose you should use the defined variable of type one_individual, so
individual.individual.push_back(variables);

Vector inside vector (creating chromosomes)

I'm attempting to build a genetic algorithm that can take a certain amount of variables (say 4), and use these in a way so that you could have 2a + 3b + c*c + d = 16. I realise there are more efficient ways to calculate this, but I want to try and build a genetic algorithm to expand later.
I'm starting by trying to create "organisms" that can compete later. What I've done is this:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <random>
// Set population size
const int population_size = 10;
const int number_of_variables = 4;
int main()
{
// Generate random number
std::random_device rd;
std::mt19937 rng(rd()); // random-number engine (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(-10, 10);
// Set gene values.
std::vector<int>chromosome;
std::vector<int>variables;
for (int i = 0; i < number_of_variables; ++i)
{
double rand_num = uni(rng);
variables.push_back (rand_num);
std::cout << variables[i] << "\n";
}
return 0;
}
What happens is it will fill up the number_of_variables vector, and output these just because that makes it clear for me that it's actually doing what I intend for it to do. What I want it to do however is to fill up each "chromosome" with one variables vector, so that for example chromosome 0 would have the values {1, 5, -5, 9} etc.
The following code obviously isn't working, but this is what I'd like it to do:
for (int j = 0; j < population_size; ++j)
{
for (int i = 0; i < number_of_variables; ++i)
{
double rand_num = uni(rng);
variables.push_back(rand_num);
}
chromosome.push_back(variables[j]);
std::cout << chromosome[j] << "\n";
}
Meaning it'd fill up the variables randomly, then chromosome1 would take those 4 values that "variables" took, and repeat. What actually happens is that (I think) it only takes the first value from "variables" and copies that into "chromosome" rather than all 4.
If anyone could help it'd be very much appreciated, I realise this might be simply a rookie mistake that is laughably simply in the eyes of someone more experienced with vectors (which would probably be 99% of the people on this website, hah).
Anyway, thanks :)
#include <iostream>
#include <vector>
#include <random>
// Set population size
const int population_size = 10;
const int number_of_variables = 4;
int main()
{
// Generate random number
std::random_device rd;
std::mt19937 rng(rd()); // random-number engine (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(-10, 10);
// Set gene values.
std::vector< std::vector<int>>chromosome;
for( int kp = 0; kp < population_size; kp++ )
{
std::vector<int>variables;
for (int i = 0; i < number_of_variables; ++i)
{
double rand_num = uni(rng);
variables.push_back (rand_num);
}
chromosome.push_back( variables );
}
// display entire population
for( auto c : chromosome )
{
for( auto v : c )
{
std::cout << v << " ";
}
std::cout << "\n";
}
// display 4th member of population
for( auto v : chromosone[ 3 ] )
{
std::cout << v << " ";
}
std::cout << "\n";
return 0;
}
http://ideone.com/2jastJ
You can place a vector inside a vector with the syntax:
std::vector<std::vector<int>>
but you will need to make the outer vector large enough for num_variables.
#include <vector>
#include <cstdlib>
using Individual = std::vector<int>;
using Population = std::vector<Individual>;
// short for std::vector<std::vector<int>>;
const size_t number_of_variables = 8;
int main() {
Population population(10);
for (auto& individual : population) {
individual.resize(number_of_variables);
for (size_t j = 0; j < number_of_variables; ++j) {
individual[j] = j; // replace with random number
}
}
}
Live demo: http://ideone.com/pfufGt