I am trying to split a vector into n parts. I checked the following solution How to split a vector into n "almost equal" parts
I came out with the following code based on this comment : To get a base number for the size of each part, simply divide the total by the number of parts: 11/3 = 3. Obviously some of the parts will need to be bigger than that to get the proper total, but that's just the remainder: 11 % 3 = 2. So now you know that 2 of the parts will be size 3+1, and whatever's left over will be 3. (Mark Ransom)
int main()
{
std::vector<int> lines;
int size = 200;
for(int i = 0; i < size;i++)
{
lines.push_back(i);
}
int p = 6;
int right = round((double)size/(double)p);
for(int i = 0; i < p;i++)
{
if( i < size - left)
{
vector<int> v;
for(int j = 0; j < right; j++)
{
v.push_back(lines[j]);
}
cout << v.size() << endl;
}
else if (i > size - left)
{
vector<int> v;
for(int k = 0; k < right; k++)
{
v.push_back(lines[k]);
}
cout << v.size() << endl;
}
}
return 0;
}
Output with p = 6 and size = 200 is : 33,33,33,33,33,33 = 198
Output with p = 6 and size = 1000 is : 167,167,167,167,167,167 = 1002
both outputs are wrong. What am i missing?
After editing:
So Let me understand. We increment i by right which represents the size of a chunk or sub-vector. While i is less than the size-right we do nothing. When i becomes greater we have to deal with the Leftovers we change the size of the chunk by right = size - i.
int main()
{
std::vector<int> lines;
int size = 1000;
for(int i = 0; i < size;i++)
{
lines.push_back(i);
}
int p = 6;
int right = round((double)size/(double)p);
int left = size % p;
for(int i = 0; i < size; i+= right)
{
if(i < size - right)
{
vector<int> v;
//MAJOR CORRECTION
for(int j = i; j < (i+right); j++)
{
v.push_back(lines[j]);
}
cout << v.size() << endl;
}
else
{
right = size - i;
vector<int> v;
//Major Correction
for(int k =i; k < size; k++)
{
v.push_back(lines[k]);
}
cout << v.size() << endl;
}
return 0;
}
Thank you.
output: 33 33 33 33 33 33 2 = 200