how to select a range in multidimensional array in C++? - c++

Lets say i have:
int array[9][9]= {
{1 , 2 , 3 , 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27},
{28, 29, 30, 31, 32, 33, 34, 35, 36},
{37, 38, 39, 40, 41, 42, 43, 44, 45},
{46, 47, 48, 49, 50, 51, 52, 53, 54},
{55, 56, 57, 58, 59, 60, 61, 62, 63},
{64, 65, 66, 67, 68, 69, 70, 71, 72},
{73, 74, 75, 76, 77, 78, 79, 80, 81}
};
how can i only apply some function to the first row (value 1 to 9 ) or the first column only (like value 1 to 73). lets say i want to say index 0 to 9 shall all have value 0.
is it possible to save this range in a variable?

Try to do like this:
for (int i = 0; i<10; i++)
array[0][i] = 0;

There are no true multidimensional arrays in C.
In a true multidimensional array, all dimensions are on equal standing. Whatever you can do with rows, you can also do with columns.
This is not the case with C++. The third row of your array is just
array[3]
It's an array on its own in every regard. A range of rows, like any other range, can be represented as a (start, end) pair, e.g. make_pair(array[3], array[7]).
Nothingl like that can be done with columns. The third column, unlike the third row, is not an array, it's just a virtual collection of elements not sitting under any standard data structure umbrella.
The closest thing to a multidimensional array slices are custom iterators, such that ++i moves to either the next element to the right or to the next element below. While you're at it, consider moving away from C style arrays to STL style containers.

To isolate the rows of the array, you could take a reference to a row of the array:
int (&row)[9] = array[2];
For example the above line takes a reference to the 3rd row of the array.
Live Demo
For the columns, is more complicated.
Alternatevely, you could do the following construct that returns a vector of reference wrappers to either a column or a row of a 2D array.
// if flg == true you get row at idx else if flg == false you get column at idx
template<typename T, int N, int M>
std::vector<std::reference_wrapper<T>>
getRange(T (&arr)[N][M], std::size_t const idx, bool const flg = true) {
if(flg) {
return typename std::vector<std::reference_wrapper<T>>(std::begin(arr[idx]), std::end(arr[idx]));
} else {
typename std::vector<std::reference_wrapper<T>> out;
out.reserve(N);
for(int i(0); i < N; ++i) out.push_back(arr[i][idx]);
return out;
}
}
Live Demo

For rows it's easy, as you can pass them like:
void foo(int * row, int cols) {
for (int col = 0; col < cols; ++col) {
int * x = row + col;
}
}
...
foo(array[3], 9);
...
For columns it's more difficult but you can thought about every column as something that have specific offset in the array:
void boo(int * col, int rows, int cols) {
for (int row = 0; row < rows; ++row) {
int * x = col + row * cols;
}
}
....
// process fourth column:
boo(array[0]+4, 9, 9);
Of course using sizeof instead of '9' and C++ vectors/array instead of C-style int[][] will make life more easy and code more readable and supportable.
Another way is to use boost::matrix e.g.:
using namespace boost::numeric::ublas;
matrix<double> m(9, 9);
matrix_row<matrix <double> > row(m, 5);
matrix_column<matrix <double> > col(m, 4);

You can do it by specifying indices (start and end range) with your function and mention whether it should be applied on row or column. Since you are using plain C style array it's trickier to deal with pointers. I recommend you to use vectors and pairs (for ranges).
An example for C style array
void some_function(int array[][9], bool row_or_column, size_t major, size_t start, size_t end){
if (row_or_column = true) {
for (int i = start; i < end; i++) {
cout << array[major][i]; //perform your operation on row
}
}
else {
for (int i = start; i < end; i++) {
cout << array[i][major]; //perform your operation on column
}
}
}
Set row_or_column as either true for row or false for column, major should specify the column number or row number and the ranges in start and end. Note: end is exclusive
For processing second row with range start = 0 and end = 5 i.e 10 to 14
some_function(array, true, 1, 0, 5)
For processing second column with range start = 0 and end = 5 i.e 2 to 38
some_function(array, false, 1, 0, 5)

Related

rearrange array to other side c++

I'm trying to shuffle array.
{10, 64, 79, 6, 13, -14, 1, 0, 66, 2}
It should do it when elements are smaller than 3 should be put on the left side and bigger than 3 on the right side
{-14 1, 0, 2, 10, 64, 79, 6, 13, 66}
int indexes[1000], c;
for(int i = 0; i < n; i++)
{
if(array[i] < 3)
{
indexes[c] = array[i];
}
}
Based on your expected output, it looks like you want to preserve the original relative order of the elements. You can use the std::stable_partition algorithm for that:
std::stable_partition(array, array + n, [](int i) { return i < 3; });
If you don't care about the relative order within a partition, you can just use std::partition.
Note that both these algorithms will also return the "partition point" which allows you to tell how many elements were moved to the beginning of the range.

Arduino UNO loops Serial unexpectedly, when doing more complex programs

I have a piece of code like this:
#include<LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void leftShift(bool toShift[28], int noOfShifts, bool destination[28]) {
for (int i = 0; i < 28; i++) {
destination[(i - noOfShifts + 28) % 28] = toShift[i];
}
}
void divideBinary(bool binary[], size_t sizeOfBinary, bool LB[], bool RB[]) {
size_t half = sizeOfBinary / 2;
// LB - first half
size_t i = 0;
for (; i < half; i++) {
LB[i] = binary[i];
}
// RB - second half
for (; i < half * 2; i++) {
RB[i - half] = binary[i];
}
}
void createSubkeys(bool binaryKey[8 * 8], bool subkeys[16][48]) {
Serial.println("just entered subkeys"); Serial.flush();
int pc_1[56] = {
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
bool keyPermutation[56];
// according to pc_1 create from 64-bit key 56-bit keyPermutation
for (int i = 0; i < 56; i++) {
keyPermutation[i] = binaryKey[pc_1[i] - 1];
}
// C and D will be saved here: [C/D] [index] [28 bools]
bool CD[2][16 + 1][56 / 2];
Serial.println("CD ready"); Serial.flush();
// divide keyPermutation into halves to C0 a D0 - each consists of 28 bits
divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]);
// from C0, D0 and shifts make C1, D1 -> C16, D16
int shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
for (int i = 1; i < 17; i++) {
leftShift(CD[0][i - 1], shifts[i - 1], CD[0][i]);
leftShift(CD[1][i - 1], shifts[i - 1], CD[1][i]);
}
// each subKey out of 16 is made from one out of 16 CD with the use of pc_2
int pc_2[48] = {
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 48; j++) {
// find out which part of CD we should look at - that means C, or D? for C CorD is 0, for D 1
int where = pc_2[j] - 1;
bool CorD = 0;
if (where >= 56 / 2) {
CorD = 1;
where -= 56 / 2; // subtract 28, to start indexing from 0 again in case of D
}
subkeys[i][j] = CD[CorD][i + 1][where];
}
}
// Serial.println("subkeys ready");
}
void setup() {
// put your setup code here, to run once:
Serial.begin( 9600 );
lcd.begin(16, 2);
Serial.println("ready"); Serial.flush();
bool binaryKey[8 * 8];
bool subkeys[16][48];
createSubkeys(binaryKey, subkeys);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print("haf");
}
It is not really important what it does, it is just so you can roughly see its complexity.
Why won't this work on Arduino, even if it were to be much slower? Instead, when I run it, my Arduino UNO really behaves weirdly. In Serial it just repeats a sequence of characters "jready" in a loop. Forever. It never prints anything else and it never reaches the loop() function.
My Arduino and its Serial both work perfectly fine for smaller programs.
EDIT: If I attempt to locate the problem by commenting out blocks of code, it seems to occur here:
// from C0, D0 and shifts make C1, D1 -> C16, D16
int shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
for (int i = 1; i < 17; i++) {
leftShift(CD[0][i - 1], shifts[i - 1], CD[0][i]);
leftShift(CD[1][i - 1], shifts[i - 1], CD[1][i]);
}
But if I make my setup() function more complex, it happens earlier, on this line:
divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]);
You are passing variables to methods without correctly specifying whether they should return that value which you modify inside that method. Look at:
void leftShift(bool toShift[28], int noOfShifts, bool destination[28])
You modify destination in there but destination is boolean array copied to the method but when the method finishes that modified value is not returned to the caller. Change your declarations.
Here:
bool CD[2][16 + 1][56 / 2];
You have a third dimension to the array but never use it. The third dimension has 28 elements but you only ever use the second dimension. If you are trying to do pointer operations then you will have to change leftShift
Also consider the above points I made with divideBinary.

Get next number from range in lexicographical order (without container of all stringifications)

How have/would you design an function that on each call returns the next value in a nominated numeric range in lexicographical order of string representation...?
Example: range 8..203 --> 10, 100..109, 11, 110..119, 12, 120..129, 13, 130..139, ..., 19, 190..199, 20, 200..203, 30..99.
Constraints: indices 0..~INT_MAX, fixed space, O(range-length) performance, preferably "lazy" so if you stop iterating mid way you haven't wasted processing effort. Please don't post brute force "solutions" iterating numerically while generating strings that are then sorted.
Utility: if you're generating data that ultimately needs to be lexicographically presented or processed, a lexicographical series promises lazy generation as needed, reduces memory requirements and eliminates a sort.
Background: when answering this question today, my solution gave output in numeric order (i.e. 8, 9, 10, 11, 12), not lexicographical order (10, 11, 12, 8, 8) as illustrated in the question. I imagined it would be easy to write or find a solution, but my Google-foo let me down and it was trickier than I expected, so I figured I'd collect/contribute here....
(Tagged C++ as it's my main language and I'm personally particularly interested in C++ solutions, but anything's welcome)
Somebody voted to close this because I either didn't demonstrate a minimal understanding of the problem being solved (hmmmm!?! ;-P), or an attempted solution. My solution is posted as an answer as I'm happy for it to be commented on and regailed in the brutal winds of Stack Overflow wisdom.... O_o
This is actually quite easy. First an observation:
Theorem: if two numbers x and y such that x < y are in the series and these numbers have the same number of digits, then x comes before y.
Proof: let's view digits of x as xn..x0 and digits of y as yn...y0. Let's take the left most digit that these two differ in, assumed to be at index i. Therefore, we have:
y = yn...yiy(i-1)...y0
x = yn...yix(i-1)...x0
since all digits from n to i are the same in both numbers. If x < y, then mathematically:
x(i-1) < y(i-1)
Lexicographically, if the digit x(i-1) is smaller than the digit y(i-1), then x comes before y.
This theorem means that in your specified range of [a, b], you have numbers with different number of digits, but the ones that have the same number of digits are in their mathematical order.
Building on that, here's a simple algorithm. First, let's say a has m digits and b has n digits (n >= m)
1. create a heap with lexicographical order
2. initially, insert `a` and `10^i` for i in [n + 1, m]
3. while the heap is not exhausted
3.1. remove and yield the top of the heap (`next`) as next result
3.2. if `next + 1` is still in range `[a, b]` (and doesn't increase in digits), insert it in heap
Notes:
In step 2, you are inserting the starting numbers of each series of numbers that have the same number of digits.
To change to a function that returns a number on each call, step 3.1 should be changed to store the state of the algorithm and resume on next call. Pretty standard.
Step 3.2 is the part that exploits the above theorem and keeps only the next number in mathematical order in the heap.
Assuming N = b - a, The extra space used by this algorithm is O(log N) and it's time complexity is O(N * log log N).
Here's my attempt, in Python:
import math
#iterates through all numbers between start and end, that start with `cur`'s digits
def lex(start, end, cur=0):
if cur > end:
return
if cur >= start:
yield cur
for i in range(0,10):
#add 0-9 to the right of the current number
next_cur = cur * 10 + i
if next_cur == 0:
#we already yielded 0, no need to do it again
continue
for ret in lex(start, end, next_cur):
yield ret
print list(lex(8, 203))
Result:
[10, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 11, 110, 111, 112, 113,
114, 115, 116, 117, 118, 119, 12, 120, 121, 122, 123, 124, 125, 126, 127, 128,
129, 13, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 14, 140, 141, 142,
143, 144, 145, 146, 147, 148, 149, 15, 150, 151, 152, 153, 154, 155, 156, 157,
158, 159, 16, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 17, 170, 171,
172, 173, 174, 175, 176, 177, 178, 179, 18, 180, 181, 182, 183, 184, 185, 186,
187, 188, 189, 19, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 20, 200,
201, 202, 203, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 8, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99]
This uses O(log(end)) stack space, which is bounded by INT_MAX, so it won't go any deeper than five calls for your typical 16 bit int. It runs in O(end) time, since it has to iterate through numbers smaller than start before it can begin yielding valid numbers. This can be considerably worse than O(end-start) if start and end are large and close together.
Iterating through lex(0, 1000000) takes about six seconds on my machine, so it appears to be slower than Tony's method but faster than Shahbaz's. Of course, it's challenging to make a direct comparison since I'm using a different language.
This is a bit of a mess, so I'm curious to see how other people tackle it. There are so many edge cases explicitly handled in the increment operator!
For range low to high:
0 is followed by 1
numbers shorter than high are always followed by 0-appended versions (e.g. 12->120)
numbers other than high that end in 0-8 are followed by the next integer
when low has as many digits as high, you finish after high (return sentinel high + 1)
otherwise you finish at a number 999... with one less digit than high
other numbers ending in 9(s) have the part before the trailing 9s incremented, but if that results in trailing 0s they're removed providing the number's still more than low
template <typename T>
std::string str(const T& t)
{
std::ostringstream oss; oss << t; return oss.str();
}
template <typename T>
class Lex_Counter
{
public:
typedef T value_type;
Lex_Counter(T from, T to, T first = -1)
: from_(from), to_(to),
min_size_(str(from).size()), max_size_(str(to).size()),
n_(first != -1 ? first : get_first()),
max_unit_(pow(10, max_size_ - 1)), min_unit_(pow(10, min_size_ - 1))
{ }
operator T() { return n_; }
T& operator++()
{
if (n_ == 0)
return n_ = 1;
if (n_ < max_unit_ && n_ * 10 <= to_)
return n_ = n_ * 10; // e.g. 10 -> 100, 89 -> 890
if (n_ % 10 < 9 && n_ + 1 <= to_)
return ++n_; // e.g. 108 -> 109
if (min_size_ == max_size_
? n_ == to_
: (n_ == max_unit_ - 1 && to_ < 10 * max_unit_ - 10 || // 99/989
n_ == to_ && to_ >= 10 * max_unit_ - 10)) // eg. 993
return n_ = to_ + 1;
// increment the right-most non-9 digit
// note: all-9s case handled above (n_ == max_unit_ - 1 etc.)
// e.g. 109 -> 11, 19 -> 2, 239999->24, 2999->3
// comments below explain 230099 -> 230100
// search from the right until we have exactly non-9 digit
for (int k = 100; ; k *= 10)
if (n_ % k != k - 1)
{
int l = k / 10; // n_ 230099, k 1000, l 100
int r = ((n_ / l) + 1) * l; // 230100
if (r > to_ && r / 10 < from_)
return n_ = from_; // e.g. from_ 8, r 20...
while (r / 10 >= from_ && r % 10 == 0)
r /= 10; // e.g. 230100 -> 2301
return n_ = r <= from_ ? from_ : r;
}
assert(false);
}
private:
T get_first() const
{
if (min_size_ == max_size_ ||
from_ / min_unit_ < 2 && from_ % min_unit_ == 0)
return from_;
// can "fall" from e.g. 321 to 1000
return min_unit_ * 10;
}
T pow(T n, int exp)
{ return exp == 0 ? 1 : exp == 1 ? n : 10 * pow(n, exp - 1); }
T from_, to_;
size_t min_size_, max_size_;
T n_;
T max_unit_, min_unit_;
};
Performance numbers
I can count from 0 to 1 billion in under a second on a standard Intel machine / single threaded, MS compiler at -O2.
The same machine / harness running my attempt at Shahbaz's solution - below - takes over 3.5 second to count to 100,000. Maybe the std::set isn't a good heap/heap-substitute, or there's a better way to use it? Any optimisation suggestions welcome.
template <typename T>
struct Shahbaz
{
std::set<std::string> s;
Shahbaz(T from, T to)
: to_(to)
{
s.insert(str(from));
for (int n = 10; n < to_; n *= 10)
if (n > from) s.insert(str(n));
n_ = atoi(s.begin()->c_str());
}
operator T() const { return n_; }
Shahbaz& operator++()
{
if (s.empty())
n_ = to_ + 1;
else
{
s.erase(s.begin());
if (n_ + 1 <= to_)
{
s.insert(str(n_ + 1));
n_ = atoi(s.begin()->c_str());
}
}
return *this;
}
private:
T n_, to_;
};
Perf code for reference...
void perf()
{
DWORD start = GetTickCount();
int to = 1000 *1000;
// Lex_Counter<int> counter(0, to);
Shahbaz<int> counter(0, to);
while (counter <= to)
++counter;
DWORD elapsed = GetTickCount() - start;
std::cout << '~' << elapsed << "ms\n";
}
Some Java code (deriving C++ code from this should be trivial), very similar to Kevin's Python solution:
public static void generateLexicographical(int lower, int upper)
{
for (int i = 1; i < 10; i++)
generateLexicographical(lower, upper, i);
}
private static void generateLexicographical(int lower, int upper, int current)
{
if (lower <= current && current <= upper)
System.out.println(current);
if (current > upper)
return;
for (int i = 0; i < 10; i++)
generateLexicographical(lower, upper, 10*current + i);
}
public static void main(String[] args)
{
generateLexicographical(11, 1001);
}
The order of the if-statements are not important, and one can be made an else of the other, but changing them in any way strangely enough makes it take about 20% longer.
This just starts with each number from 1 to 10, then recursively appends each possible number from 0 to 10 to that number, until we get a number bigger than the upper limit.
It similarly uses O(log upper) space (every digit requires a stack frame) and O(upper) time (we go from 1 to upper).
I/O is obviously the most time-consuming part here. If that is removed and replaced by just incrementing a variable, generateLexicographical(0, 100_000_000); takes about 4 seconds, but by no means taken from a proper benchmark.

Storing values from STL vector to another vector

I stuck at one point and need some help.
I have a STL vector with the following values:
[1, 17, 2, 18, 3, 19, 1, 17, 2, 18, 3, 19, 1, 17, 2, 18, 3, 19].
note that first six values in a vector (i.e. 1, 17, 2, 18, 3, 19 ) can be considered as one block. So this vector has 3 blocks each with the values as described above.
Now, I want to organize this vector in a following way:
[1, 17, 1, 17, 1, 17, 2, 18, 2, 18, 2, 18, 3, 19, 3, 19, 3, 19]
.
So essentially I am picking first two values (i.e. 1, 17) from each block first and store them sequentially 3 times (basically # of blocks which in this case is 3). I then go on to pick next two values (i.e. 2, 18) and continue.
How do I achieve this..?
Any help will be greatly appreciated.
Thanks
Sound quite easy once you figure out the exact mapping. So external loop is the number of chunks in every block since that's the number of final groups, middle loop goes over each original block while inner loop just goes through every element of a chunk. Final result should be something like (untested):
std::vector organized;
organized.reserve(data.size());
const int blockSize = 6;
const int subBlockSize = 2;
assert(data.size()%blockCount == 0 && blockSize%subBlockSize == 0);
const int blockCount = data.size()/blockSize;
const int subBlockCount = blockSize/subBlockSize;
for (int i = 0; i < subBlockCount; ++i)
for (int j = 0; j < blockCount; ++j)
for (int k = 0; k < subBlockSize; ++k)
organized.push_back(subBlockSize*i + blockSize*j + k);
Just create a function shuffle(i) that takes an index into the new array, and returns an index from the original array:
#include <iostream>
#include <cstdlib>
using namespace std;
int list[] = { 1, 17, 2, 18, 3, 19, 1, 17, 2, 18, 3, 19, 1, 17, 2, 18, 3, 19 };
int shuffle( int i )
{
div_t a = div( i, 6 );
div_t b = div( a.rem, 2 );
return 2*a.quot + 6*b.quot + b.rem;
}
int main()
{
for( int i=0 ; i<18 ; ++i ) cout << list[shuffle(i)] << ' ';
cout << endl;
return 0;
}
This outputs:
1 17 1 17 1 17 2 18 2 18 2 18 3 19 3 19 3 19
Just allocate the new vector, and fill it from the old one:
new_vector[i] = list[shuffle(i)];

create array of arrays from one array of Series(in c++)

how i create an array of arrays, from One-dimensional array of Series, example:
I have an array like :
long int arr[20] = {23, 91, -71, -63, 22, 55, 51, 73, 17, -19,-65, 44, 95, 66, 82, 85, 97, 30, 54, -34};
and i want to create array of arrays in ascending order like: (in c++)
23, 91
-71, -63, 22, 55
51, 73
17
-19
-65, 44, 95
66, 82, 85, 97
30, 54
-34
already tried to now how many array there are
int sum=0;
for(int i=0;i<n-1;i++)
if(arr[i]>arr[i+1])sum++;
return sum;
int sum=0;
for(int i=0;i<n-1;i++)
if(arr[i]>arr[i+1])sum++;
return sum;
should be
int sum = 0;
if (n > 0)
{
for (int i = 0; i < n - 1; i++)
if (arr[i] > arr[i+1])
sum++;
sum++;
}
Your version doesn't count the last sequence of ascending numbers.
That's a start, what you have to do next is allocate enough memory for a pointer to each row. Then you go through the numbers again, count the length of each row, allocate the memory for that row, and then go through that row again, copying the numbers for that row. It's just loops (inside loops). Have a go and post the code if you get stuck.
How about creating vector of vector instead of array of array? In array you have to determine size which will cause you, either index out of bound exception or huge space lost. If you use vector, you are not going to determine size of vector, it will allocate more space as you fill the size of vector.
If your initial array is descending order, your double array size will be nx1, if it is ascending order, then 1xn so you have to make your double array nxn not to give exception which is inacceptible when n > 10^4 (approximately).
Some basic syntax of vector of vector is as follows:
vector<vector<int>> myvect; //initialization
myvect.at(i).at(j) = x; //reaching i_th row, j_th col element
myvect.at(0).push_back(1); //add element to the end of the row0.
This website looks nice about explaining vectors.
Here is sample code, I didn't test it so there might be small syntax erra,
vector<vector<int>> myvect; //initialization
int size = 20;
long int arr[size] = {23, 91, -71, -63, 22, 55, 51, 73, 17, -19,-65, 44, 95, 66, 82, 85, 97, 30, 54, -34};
int row = -1; int val = arr[0]+1;
for(int i = 0; i < size; i++){
if(arr[i] < val){
row++;
myvect.push_back(vector<int> () );
}
myvect.at(row).push_back(arr[i]);
}
The code is basically like this.
You could do
array[9][4] = {
{ 23, 91 },
{ -71, -63, 22, 55 },
{ 51, 73 },
{ 17 },
{ -19 },
{ -65, 44, 95 },
{ 66, 82, 85, 97 },
{ 30, 54 }
{ -34 }
};