Print all combinations of a 3D vector - c++

Suppose I have a vector<vector<vector<int> > > result.
The only size I know before hand is the inner and outer vector, which are of size k.
if I print result I get this (for k = 3):
i = 0
0 1 2
3 4 5
i = 1
6 7 8
9 10 11
12 13 14
i = 2
15 16 17
18 19 20
What I need to do is to print every combinations of k rows from each of vector of vectors of i's. In other words, what I need is the following output:
0 1 2
6 7 8
15 16 17
0 1 2
6 7 8
18 19 20
0 1 2
9 10 11
15 16 17
...
3 4 5
12 13 14
18 19 20
Hope I was clear about the desired output. I have tried a thousand of different loops, trying to save in another vector<vector<int> > but no success so far. I really am lost and any help would be greatly appreaciated.
The code to generate the above output is here:
(I'm sorry, I know it is an ugly code but it was the closest I could get to demonstrate my problem in a MCVE code)
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<vector<vector<int> > > result;
int k = 3;
vector<vector<int> > randomVectors;
//I'll create seven random vectors
//In my original problem, I don't have this number beforehand
int number = 0;
for(int i = 0; i < 7; i++){
vector<int> temp;
for(int j = 0; j < k; j++){
temp.push_back(number);
number++;
}
randomVectors.push_back(temp);
}
//Vector of vector to assign to "result"
vector<vector<int> > randomVectors_0;
randomVectors_0.push_back(randomVectors[0]);
randomVectors_0.push_back(randomVectors[1]);
vector<vector<int> > randomVectors_1;
randomVectors_1.push_back(randomVectors[2]);
randomVectors_1.push_back(randomVectors[3]);
randomVectors_1.push_back(randomVectors[4]);
vector<vector<int> > randomVectors_2;
randomVectors_2.push_back(randomVectors[5]);
randomVectors_2.push_back(randomVectors[6]);
result.push_back(randomVectors_0);
result.push_back(randomVectors_1);
result.push_back(randomVectors_2);
cout << "Printing the 3D vector" << endl;
for(int i = 0; i < k; i++){
cout << "i = " << i << endl << endl;
for(int j = 0; j < result[i].size(); j++){
for(int m = 0; m < k; m++){
cout << result[i][j][m] << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}
Compiler version: gcc (tdm-1) 4.7.1

I would make a rows_to_print vector that starts with all 0's. Then once a loop, it'll increment the last value by 1. If that value is greater than the size of the last vector, then reset it to 0 and increment the next value up the list, etc... you're done looping when every value in rows_to_print is greater than the size of each of the vectors:
void print_rows(std::vector<size_t> rows, std::vector<std::vector<std::vector<int>>> v) {
for(size_t x = 0; x < v.size(); x++) {
for(size_t y = 0; y < v.at(x).at(rows.at(x)).size(); y++) {
std::cout << v.at(x).at(rows.at(x)).at(y) << ' ';
}
std::cout << std::endl;
}
}
bool increment_rows(std::vector<size_t> &rows, std::vector<std::vector<std::vector<int>>> v) {
if(!rows.size()) return false; //empty rows, BAD
rows.at(rows.size() - 1)++;
for(int x = rows.size() - 1; x >= 0; x--) {
if(rows.at(x) >= v.at(x).size()) {
if(x <= 0) { return false; } //first row is done, then we're done!
rows.at(x-1)++; //increment previous row and set us back to 0 (overflow)
rows.at(x) %= v.at(x).size();
}
}
return true;
}
int main() {
...
std::vector<size_t> rows_to_print(k, 0);
print_rows(rows_to_print, result);
while(increment_rows(rows_to_print, result)) {
print_rows(rows_to_print, result);
}
}
See it in action here: ideone

Related

Sum the elements from a vector using a nested for loop

I'm trying to sum the values of a vector but I have a problem with that.
The size of the vector is 20 elements and I'm trying to do a sum of 5 elements from the current position.
Something like: sum the elements from 1 to 5, 2 to 6, 3 to 7 and so on.
I thought that I could do a for nested loop, like this one below:
for (int a = 0; a < numVec.size(); a++) {
for (int b = a; b < numVec.size(); b++)
{
if (aux < 5) {
cout << "B: " << b << endl;
sum += numVec[b].num;
}
if (aux > 4) {
aux = 0;
sumAux= sum;
sum= 0;
break;
}
aux++;
}
cout << "Sum: " << sumAux<< endl;
}
But I'm having some problems when I get the 15th position, everything goes wrong and I can't figure out why.
If you can help me, I thank you very much.
It will help you a lot, if you think for a longer time before start to code something. Maybe you can take a piece of paper and write something down.
Then, it will very much help you, if you choose long and speaking variable names.
So, let us make a picuture. We write some test values and their index in the vector, where they are stored. Please remeber. Indices start with 0 in C++.
Value: 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
So, and if we now want to build the sums for 5 values each, then we need to add
Index 0 1 2 3 4 Value: 21 22 23 24 25
Index 1 2 3 4 5 Value: 22 23 24 25 26
Index 2 3 4 5 6 Value: 23 24 25 26 27
. . .
Index 14 15 16 17 18 Value: 35 36 37 38 39
Index 15 16 17 18 19 Value: 36 37 38 39 40
So, you can see. We have a start index that always will be incremented by 1. Beginning with this start index, we will always add up 5 values. But we must end this process, as you can see above at index 15, so 20 - 5.So, always, size of the whole array - the size of the subarray.
So, let us first solve this problem we can do it strigh forward:
#include <iostream>
#include <vector>
int main() {
// Our test data to play with
std::vector<int> data = { 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40 };
// This is the size of the subarray. So the number of values that we want to sum up
int sizeOfSubarray = 5;
// And because we have a subarray size, the last summation starts at this index
int lastIndex = data.size() - sizeOfSubarray;
// So, now iterate over all data that needs to be summed up
for (int startIndex = 0; startIndex <= lastIndex; ++startIndex) {
// Because we have a new start index now, we start also with a 0 sum
int sum = 0;
// Calculate the end index of the sub array
int endIndexOfSubarray = startIndex + sizeOfSubarray;
for (int sumIndex = startIndex; sumIndex < endIndexOfSubarray; ++sumIndex) {
// Some debug output
std::cout << "Startindex: " << startIndex << "\tSumindex: " << sumIndex << "\tValue: " << data[sumIndex] << '\n';
// Calculate the subarray sum
sum = sum + data[sumIndex];
}
// Show the subarray sum
std::cout << "Sum: " << sum << '\n';
}
}
OK, understood. What, if we want also to add up the end of the values of the array? So, what if the startindex will rund over the complete array. Let us look at this.
Index 16 17 18 19 ? Value: 37 38 39 40 ?
Index 17 18 19 ? ? Value: 38 39 40 ? ?
Index 18 19 ? ? ? Value: 39 40 ? ? ?
Index 19 ? ? ? ? Value: 40 ? ? ? ?
You can see, that the start index runs until < 20. So < size of vector.
And if the is the end index of the summation is > 19, so >= the sizeof the vector, we can limit it to 19,
This we can either calculate or use a simple if statement.
Then the code would look like that
#include <iostream>
#include <vector>
int main() {
// Our test data to play with
std::vector<int> data = { 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40 };
// This is the size of the subarray. So the number of values that we want to sum up
int sizeOfSubarray = 5;
// So, now iterate over all data that needs to be summed up
for (int startIndex = 0; startIndex < data.size(); ++startIndex) {
// Because we have a new start index now, we start also with a 0 sum
int sum = 0;
// Calculate the end index of the sub array
int endIndexOfSubarray = startIndex + sizeOfSubarray;
// If this index is too big ( > 20) then limit it to 20
if (endIndexOfSubarray > data.size()) {
endIndexOfSubarray = data.size();
}
// Claculate sum of sub array
for (int sumIndex = startIndex; sumIndex < endIndexOfSubarray; ++sumIndex) {
// Some debug output
std::cout << "Startindex: " << startIndex << "\tSumindex: " << sumIndex << "\tValue: " << data[sumIndex] << '\n';
// Calculate the subarray sum
sum = sum + data[sumIndex];
}
// Show the subarray sum
std::cout << "Sum: " << sum << '\n';
}
}
I hope, this explanation helps
One option is to have the inner loop range from 0-5
for (int a = 0; a < numVec.size(); a++) {
int sum = 0;
for (int b = 0; b < 5 && a + b < numVec.size(); b++) {
sum += numVec[a + b];
}
std::cout << sum << "\n";
}
Another option is to use std::accumulate
for (auto a = numVec.begin(); a < numVec.end(); a++) {
std::cout << std::accumulate(a, std::min(a + 5, numVec.end()), 0) << '\n';
}
Also, mentioned in the comments by #Bathsheba is to keep a running total, which is O(n).
int sum = 0;
for (int a = 0; a < 5 && a < numVec.size(); a++) sum += numVec[a];
std::cout << sum << '\n';
for (int a = 5; a < numVec.size(); a++) {
sum = sum - numVec[a - 5] + numVec[a];
std::cout << sum << '\n';
}
This is considered to be the rolling sum etc. You could write a template that manipulates a binary function on the vector specifying the window:
# include <iostream>
# include <numeric>
# include <vector>
# include <functional>
using namespace std;
template<class T, class Lambda>
vector<T> roll_fun(vector<T> vec, int window, Lambda&& func, T init){
int final_size = vec.size() - window + 1;
vector<T> result(final_size);
for (int k = 0; k < final_size; k++)
result[k] = accumulate(vec.begin() + k, vec.begin() + k + window, init, func);
return result;
};
int main()
{ vector<double> myvec{1,2,2.3,3,4,5,6,7,8,9,1,2,3,4,5,6,7};
//rolling sum
vector<double> v = roll_fun<double>(myvec, 5,plus<double>(), 0.0);
for(auto i: v) cout<<i<<' ';
cout<<endl;
// rolling mean
vector<double> v1 = roll_fun<double>(myvec, 5,[](double x, double y){return x+y/5;}, 0);
for(auto i: v1) cout<<i<<' ';
cout<<endl;
//rolling max
vector<double> v2 = roll_fun<double>(myvec, 5,[](double x, double y){return x>y?x:y;}, 0.0);
for(auto i: v2) cout<<i<<' ';
cout<<endl;
return 0;
}
The whole aux and sumAux handling is making your logic more complicated than it needs to be.
Try something more like this:
#include <algorithm>
const size_t size = numVec.size();
const size_t increment = 5;
for (size_t a = 0; a < size; ++a)
{
size_t stop = a + std::min(size-a, increment);
sum = 0;
for (size_t b = a; b < stop; ++b)
sum += numVec[b].num;
cout << "Sum: " << sum << endl;
}
Online Demo
Alternatively:
#include <algorithm>
#include <numeric>
auto end = numVec.end();
decltype(numVec)::difference_type increment = 5;
for (auto start = numVec.begin(); start != end; ++start)
{
auto stop = start + std::min(end-start, increment);
sum = std::accumulate(start, stop, 0,
[](auto a, const auto &elem){ return a + elem.num; }
);
cout << "Sum: " << sum << endl;
}
Online Demo

Private test cases are not passing. What is the mistake here? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Given N three-digit numbers, your task is to find bit score of all N numbers and then print the number of pairs possible based on these calculated bit score.
Rule for calculating bit score from three digit number:
From the 3-digit number,
· extract largest digit and multiply by 11 then
· extract smallest digit multiply by 7 then
· add both the result for getting bit pairs.
Note: - Bit score should be of 2-digits, if above results in a 3-digit bit score, simply ignore most significant digit.
Consider following examples:
Say, number is 286
Largest digit is 8 and smallest digit is 2
So, 8*11+2*7 =102 so ignore most significant bit , So bit score = 02.
Say, Number is 123
Largest digit is 3 and smallest digit is 1
So, 3*11+7*1=40, so bit score is 40.
Rules for making pairs from above calculated bit scores
Condition for making pairs are
· Both bit scores should be in either odd position or even position to be eligible to form a pair.
· Pairs can be only made if most significant digit are same and at most two pair can be made for a given significant digit.
Constraints
N<=500
Input Format
First line contains an integer N, denoting the count of numbers.
Second line contains N 3-digit integers delimited by space
Output
One integer value denoting the number of bit pairs.
Test Case
Explanation
Example 1
Input
8 234 567 321 345 123 110 767 111
Output
3
Explanation
After getting the most and least significant digits of the numbers and applying the formula given in Rule 1 we get the bit scores of the numbers as:
58 12 40 76 40 11 19 18
No. of pair possible are 3:
40 appears twice at odd-indices 3 and 5 respectively. Hence, this is one pair.
12, 11, 18 are at even-indices. Hence, two pairs are possible from these three-bit scores.
Hence total pairs possible is 3
#include <iostream>
#include <vector>
using namespace std;
vector<int> correctBitScores(vector<int>);
vector<int> bitScore(vector<int>);
int findPairs(vector<int>);
int main() {
int a, b;
int pairs = 0;
vector<int> vec;
vector<int> bitscore;
cout << "\nEnter count of nos: ";
cin >> a;
for (int i = 0; i < a; i++) {
cin >> b;
vec.push_back(b);
}
bitscore = bitScore(vec);
pairs = findPairs(bitscore);
cout << "Max pairs = " << pairs;
return 0;
}
vector<int> correctBitScores(vector<int> bis) {
int temp = 0;
for (size_t i = 0; i < bis.size(); i++) {
temp = bis[i];
int count = 0;
while (temp > 0) {
temp = temp / 10;
count++;
}
if (count > 2)
bis[i] = abs(100 - bis[i]);
}
/*cout << "\nCorrected" << endl;
for (int i = 0; i < size(bis); i++) {
cout << bis[i] << endl;
}*/
return bis;
}
int findPairs(vector<int> vec) {
int count = 0;
vector<int> odd;
vector<int> even;
for (size_t i = 0; i < vec.size(); i++)
(i % 2 == 0 ? even.push_back(vec[i]) : odd.push_back(vec[i]));
for (size_t j = 0; j < odd.size(); j++)
for (size_t k = j + 1; k < odd.size(); k++) {
if (odd[j] / 10 == odd[k] / 10) {
count++;
odd.erase(odd.begin()+j);
}
}
for (size_t j = 0; j < even.size(); j++)
for (size_t k = j + 1; k < even.size(); k++) {
if (even[j] / 10 == even[k] / 10) {
count++;
even.erase(even.begin() + j);
}
}
return count;
}
vector<int> bitScore(vector<int> v) {
int temp = 0, rem = 0;
vector<int> bs;
for (size_t i = 0; i < v.size(); i++) {
int max = 0, min = 9;
temp = v[i];
while (temp > 0) {
rem = temp % 10;
if (min > rem)
min = rem;
if (max < rem)
max = rem;
temp = temp / 10;
}
int bscore = (max * 11) + (min * 7);
bs.push_back(bscore);
}
/*cout << "\nBit Scores = " << endl;
for (int i = 0; i < size(bs); i++) {
cout << bs[i] << endl;
}*/
bs = correctBitScores(bs);
return bs;
}
I tried doing it very simple c++ code as per my understanding of Que,can you just verify it more test cases.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,count=0;
cin>>n;
vector<int>v(n);
for(int i=0;i<n;i++){
cin>>v[i];
string s = to_string(v[i]);
sort(s.begin(),s.end());
int temp = (s[s.length()-1]-'0')*11 + (s[0] - '0')*7;
v[i] = temp%100;
}
unordered_map<int ,vector<int>>o,e;
for(int i=0;i<n;i=i+2){
o[v[i]/10].push_back(i+1);
}
for(int i=1;i<n;i=i+2){
e[v[i]/10].push_back(i+1);
}
count=0;
for(int i=0;i<10;i++){
int os=o[i].size(),es=e[i].size();
if(os==2)
count++;
if(es == 2)
count++;
if(os>2 || es>2)
count += 2;
}
cout<<count;
}

How Vector allocation using push_back() and pop_back operation gives garbage values

I am solving a problem in which I have to find those element from the array whose total gives maximum sum. But there is a condition that no two adjacent element can be the part of that max subarray. Here is my code using simple brute Force solution-
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t != 0)
{
int n, i, s, k = 0, m = -1001;
vector< int > a;
cin >> n;
a.resize(n, 0);
vector< int > b;
for (i = 0; i < n; i++)
{
cin >> a[i];
m = max(m, a[i]);
if (a[i] < 0)
{
a[i] = 0;
++k;
}
}
if (k == n)
cout << m;
else
{
k = 0;
s = a[0];
b.push_back(a[0]);
for (i = 1; i < n; i++)
{
if (i != k + 1)
{
if (a[i])
{
s += a[i];
b.push_back(a[i]);
k = i;
}
}
else
{
if (s - a[i - 1] + a[i] > s)
{
b.pop_back();
s -= a[i - 1];
s += a[i];
b.push_back(a[i]);
++k;
}
}
}
}
cout << endl;
for (i = n; i >= 0; i--)
{
if (b[i])
cout << b[i] << " ";
}
cout << endl;
--t;
}
return 0;
}
Here is input to code-
First line represent no. of test cases,
Second line represent size of array
And the next line shows array elements.m
5
5
-1 7 8 -5 4
4
3 2 1 -1
4
11 12 -2 -1
4
4 5 4 3
4
5 10 4 -1
Output-
4 8
32 32607 -787829912 1 3
32 32607 -787829912 12
3 5
10
Expected output-
4 8
1 3
12
3 5
10
So, there are 5 test cases. For the first test case and last two test case output is correct. But for second and third test case it is giving garbage value. What is the problem, that for some test cases it is giving garbage value, and for other not.
for (i = n; i >= 0; i--)
{
if (b[i])
cout << b[i] << " ";
}
This prints out n+1 values in b. But even in the best case, b only has n values (for n=1). And for n>1, b.size() is less than n, so you are reading garbage from outside the vector's storage (this is undefined behavior). Just use the correct bound:
for (i = b.size() - 1; i >= 0; ++i)
I think I found your (first) problem:
if(k==n)
cout<<m;
When all numbers are negative this outputs the largest of them.
But the empty array has a sum of 0 and is larger than a negative number and has no 2 adjacent members in it. So clearly the right answer should be 0, not m.

Executing print in standard output causing random assigning of value in array

I'm sorry if this is a silly question.
I have written the following piece of code for coin change problem.
#include <iostream>
using namespace std;
int coinChange(int coins[], int n, int amount)
{
int combinations[amount + 1];
combinations[0] = 1;
int start;
for(int i = 0; i < n; i++)
{
start = coins[i];
int coin = coins[i];
for(int j = start; j < amount + 1; j++)
{
if(j >= coin)
{
combinations[j] += combinations[j - coin];
}
}
for(int j = 0; j < amount + 1; j++)
cout << combinations[j] << " ";
cout << endl;
}
return combinations[amount];
}
int main(int argc, char const *argv[])
{
int coins[] = {1, 2, 5};
int n = sizeof(coins)/sizeof(coins[0]);
int amount = 12;
// cout << "Total combinations: ";
cout << coinChange(coins, n, amount) << endl;
return 0;
}
The code works fine and provides me the correct output as shown below.
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 2 2 3 3 4 4 5 5 6 6 7
1 1 2 2 3 4 5 6 7 8 10 11 13
13
However, If I uncomment the line cout << "Total combinations: "; just above the function calling in the main function, the program gives me bizzare outputs.
Total combinations: 1 32768 32768 32768 44137 44137 44137 44137 196418491 196418492 -790461916 -790429149 619621115
1 32768 32769 65536 76906 109673 121043 153810 196539534 196572302 -593922382 -593856847 25698733
1 32768 32769 65536 76906 109674 153811 186579 196605070 196649208 -593812708 -593703036 25885312
25885312
Is executing cout before function calling causing this random outputs? Or is this a problem for my version of compiler?
What about initialize (to zero?) combinations ?
Something like
int combinations[amount + 1] {};
Otherwise the initial values of combinations[i] are undefined indeterminate, so are is undefined the final values behavior of the program (correction from Shafik Yaghmour; thanks!)
Do this in your coinChange function.
int combinations[amount + 1]{};
combinations[0] = 1;
int start;
for(int i = 0; i < n; i++)
{
start = coins[i];
int coin = coins[i];
for(int j = start; j < amount + 1; j++)
{
if(j >= coin)
{
combinations[j] += combinations[j - coin];
}
}
for(int j = 0; j < amount + 1; j++)
cout << combinations[j] << " ";
cout << endl;
}
Now uncomment the line and run. The basic problem is when you create the combinations array, you have to initialize the elements to 0. If you don't, they may be all 0 by a lucky coincidence, but you can't guarantee that.
EDIT : Using empty initializer list to initilize array with zeros as max66 suggested.

Array that fills using a loop *AND swaps

I did this program in class and I'm trying to recreate it for an exam coming up. The program is supposed to be an array[2][10] and is supposed to output numbers in this order:
1 3 5 7 9 11 13 15 17 19
0 2 4 6 8 10 12 14 16 18
I'm really lost on this and I could really use any help.
#include<iostream>
#include <string>
#include <cstring>
using namespace std;
void out(int n[2][10]);
void fillit(int n[2][10]);
int main(){
int nums[2][10];
fillit(nums);
out(nums);
}
void fillit(int n[2][10]){
n[0][0] = 1;
n[1][0] = 0;
for (int i = 1; i < 10; i++){
n[0][i] = n[0][i] + 2;
n[1][i] = n[0][i] + 2;
}
}
void out(int n[2][10]){
for (int r = 0; r <= 1; r++){
for (int c = 0; c <= 9; c++){
cout << n[r][c];
}
cout << endl;
}
}
Update I have the program successfully filling the array but now I need the program to swap row 1 with row 2 and then output the new array. I.e.
0 2 4 6 8 10 12 14 16 18
1 3 5 7 9 11 13 15 17 19
void fillit(int n[2][10]){
for (int i = 0; i < 10; i++){
n[0][i] = (i * 2 ) + 1;
n[1][i] = i * 2;
}
}
#include<iostream>
#include <string>
#include <cstring>
using namespace std;
void out(int n[2][10]);
void fillit(int n[2][10]);
int main(){
int nums[2][10];
fillit(nums);
out(nums);
}
void fillit(int n[2][10]){
n[0][0] = 1;
n[1][0] = 0;
for (int i = 1; i < 10; i++){
n[0][i] = n[0][i-1] + 2;
n[1][i] = n[0][i-1] + 2;
}
}
void out(int n[2][10]){
for (int r = 0; r <= 1; r++){
for (int c = 0; c <= 9; c++){
cout << n[r][c];
}
cout << endl;
}
}
How about this, its shorter?
int nums[2][10];
for (int i = 0; i < 20; i++)
{
nums[(i%2)^1][i/2] = i;
}
I noticed that second array elements are even numbers, and the first array corresponding elements are one bigger (and thus odd) ... so this answer accomplishes using ONLY addition. Might be easier to understand.
void fillit(int n[2][10])
{
int even = 0; // start value
for (size_t i = 0; i < 10; i++)
{
int odd = even + 1;
n[0][i] = odd;
n[1][i] = even;
even += 2;
}
}
I noticed that you tagged this problem with C++. Perhaps you should try vectors.
For small vectors, you simply declare with initial values in curly-braces, making it easy to define the matrix limits [2] and [10]. In this example, I initialized using easy to recognize values, so you can tell the 2x10 matrix has not yet been filled. Without this init, the values contained will be random noise.
std::vector<std::vector<int>> n {
{ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }, // row 1
{ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 } // row 2
// 1 2 3 4 5 6 7 8 9 10 <-- col
};
Yes, you could write in the target value of your output, but then fillit() would not be needed.
For passing the 2x10 vector to fillIt(), remember that you need to mark the matrix as a reference. The following emphasizes that n[0] contains odd numbers, and n[1] contains 10 even numbers.
// vector x vector v--reference
void fillIt(std::vector<std::vector<int>>& n)
{
int even = 0;
for (size_t c = 0; c < 10; ++c) // row major {
int odd = even + 1;
n[0][c] = odd;
n[1][c] = even;
even += 2;
}
}
For test, I recommend just passing the 2x10 vector to a new function "showIt()".
// do not modify--vvvvv do not copy--v
void vec2x10Show (const std::vector<std::vector<int>>& n)
{
// header
std::cout << "\nCOL->";
for (int i=0; i<10; ++i) std::cout << std::setw(3) << i+1 << " ";
std::cout << "\nr1: "; // r c
for (int c=0; c<10; ++c) std::cout << std::setw(3) << n[0][c] << " ";
std::cout << "\nr2: "; // r c
for (int c=0; c<10; ++c) std::cout << std::setw(3) << n[1][c] << " ";
std::cout << std::endl;
}
Note the hard coded '10' in each loop (a magic number). Using vectors, this magic number is not necessary because the vector includes this information (your next assignment).