Populating 2d array zig zag with consecutive numbers C++ - c++

I'm stuck on this. Could you please help me?
Write a function which initialize a two-dimensional array. The array is a square
matrix (i.e., its width and height are identical.) The array should be initialized in the
zig-zag style. Specifically, we start at the top-left corner and go downward, and put a
number starting from 1. Once we hit the bottom, we go to the next column and fill in
the numbers from bottom to top. We fill the numbers downward in the third
column, upward in the fourth column, and so on. The process ends when all the
elements in the array are filled.
However, How do I get my output like this?
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
#include <iomanip>
#include <iostream>
using namespace std;
const int SIZE = 5; // Note SIZE can be anything between 1 to 9
void initGrid(int grid[SIZE][SIZE]);
void printGrid(int grid[SIZE][SIZE]);
int main() {
int grid[SIZE][SIZE];
initGrid(grid);
printGrid(grid);
}
void initGrid(int grid[SIZE][SIZE]) {
int inc = 1;
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE; i++) {
grid[i][j] = inc;
inc++;
}
}
}
void printGrid(int grid[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
// setw() function handles the printing format.
cout << setw(2) << grid[i][j] << " ";
}
cout << endl;
}
}

Every two columns of the grid share the same filling pattern, the odd ones are filled in ascending order, the even ones in descending order. All you have to do is translate that into code:
template<size_t Rows, size_t Cols>
void initGrid(int (&grid)[Rows][Cols]) {
int value = 1;
for (size_t j = 0; j < Cols; ++j) {
// first fill the odd column in descending order
for (size_t i = 0; i < Rows; ++i, ++value) {
grid[i][j] = value;
}
// then, if there is one, fill the even column
++j;
if (j == Cols )
break;
for (size_t i = Rows; i > 0; ++value) {
--i; // size_t is unsigned, so I have to check i before decrementing
grid[i][j] = value;
}
}
}
I used the same data structure you used (but a different function signature) only to focus on the algorithm, but I'd use a class instead.
If you don't want to traverse the array column-wise (which, for big arrays, could result in performance drop due to cache misses) you can calculate the differences between values in every row:
template<size_t Rows, size_t Cols>
void zig_zag_fill(int (&grid)[Rows][Cols])
{
int diff_up = 1;
int diff_down = Rows * 2 - 1;
for (size_t i = 0; i < Rows; ++i, diff_down -= 2, diff_up += 2)
{
int value = i + 1;
size_t j = 0;
while ( j < Cols )
{
grid[i][j] = value;
value += diff_down;
++j;
if ( j == Cols )
break;
grid[i][j] = value;
value += diff_up;
++j;
}
}
}
A complete test program like this:
#include <iostream>
#include <iomanip>
template<size_t Rows, size_t Cols>
void zig_zag_fill(int (&grid)[Rows][Cols]);
template<size_t Rows, size_t Cols>
void printGrid(int (&grid)[Rows][Cols]);
int main() {
int grid[5][6];
zig_zag_fill(grid);
printGrid(grid);
std::cout << '\n';
int grid2[6][5];
zig_zag_fill(grid2);
printGrid(grid2);
std::cout << '\n';
int grid3[5][5];
zig_zag_fill(grid3);
printGrid(grid3);
std::cout << '\n';
int grid4[6][6];
zig_zag_fill(grid4);
printGrid(grid4);
std::cout << '\n';
}
template<size_t Rows, size_t Cols>
void initGrid(int (&grid)[Rows][Cols]) {
int value = 1;
for (size_t j = 0; j < Cols; ++j) {
for (size_t i = 0; i < Rows; ++i, ++value) {
grid[i][j] = value;
}
++j;
if (j == Cols )
break;
for (size_t i = Rows; i > 0; ++value) {
--i;
grid[i][j] = value;
}
}
}
template<size_t Rows, size_t Cols>
void zig_zag_fill(int (&grid)[Rows][Cols])
{
int diff_up = 1;
int diff_down = Rows * 2 - 1;
for (size_t i = 0; i < Rows; ++i, diff_down -= 2, diff_up += 2)
{
int value = i + 1;
size_t j = 0;
while ( j < Cols )
{
grid[i][j] = value;
value += diff_down;
++j;
if ( j == Cols )
break;
grid[i][j] = value;
value += diff_up;
++j;
}
}
}
template<size_t Rows, size_t Cols>
void printGrid(int (&grid)[Rows][Cols]) {
for (size_t i = 0; i < Rows; ++i) {
for (size_t j = 0; j < Cols; ++j) {
std::cout << std::setw(2) << grid[i][j] << " ";
}
std::cout << '\n';
}
}
Would output:
1 10 11 20 21 30
2 9 12 19 22 29
3 8 13 18 23 28
4 7 14 17 24 27
5 6 15 16 25 26
1 12 13 24 25
2 11 14 23 26
3 10 15 22 27
4 9 16 21 28
5 8 17 20 29
6 7 18 19 30
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
1 12 13 24 25 36
2 11 14 23 26 35
3 10 15 22 27 34
4 9 16 21 28 33
5 8 17 20 29 32
6 7 18 19 30 31

Firstly, if I was writing this program I would represent my data in a different manner. I.e. I would likely use a vector and avoid global size constants. The being said, here is how I would do this. Let's write out the sequence the indexes follow and try to see if we can create some kind of sequence.
1 4 7
2 5 8
3 6 9
Is what is in your array after creating it and we want
1 6 7
2 5 8
3 4 9
Our indexes to the original array looks like this
(0, 0) (2, 1) (0, 2)
(1, 0) (1, 1) (1, 2)
(2, 0) (0, 1) (2, 2)
Well our j component is easy, it's just a simple arithmetic sequence where you add 1, i.e. 1, 2, 3, 4, 5...
for (int j = 0; j < 3; j++){
...
}
Now we need a sequence that follows the patten 0, 1, 2, 2, 1, 0, 0, 1, 2, ... repeating. Since we have repeating digits, I am thinking of clock arithmetic or modular arithmetic. Let's start with the sequence 0, 1, 2, 0, 1, 2 ... repeating. I.e the integers mod 3 (Z mod 3). For the first column we want the indicies straight up as they are (Z mod 3). Then for the reversed columns let's say we are given this sequence and we are iterating through it from 0 to 2. We can cleverly use modular arithmetic to get to our reversed sequence. For example if we had (2 mod 3), (4 mod 3), (6 mod 3), we would have 2, 1, 0. How do we get 0, 1, 2 to 2, 4, 6? Like this f(x) : (x + 1) * 2.
bool reversed = false;
for (int i = 0; i < 3; i++){
int idx = i;
if(reversed){
int offset = (i + 1) * 2;
idx = (offset) % 3;
}
}
reversed = !reversed;
Now we just have to put it all together. This should be our new init grid as the print function is fine.
for (int j = 0; j < SIZE; j++){
bool reversed = false;
for (int i = 0; i < SIZE; i++){
int idx = i;
if(reversed){
int offset = (i + 1) * (SIZE - 1);
idx = (offset) % SIZE;
}
arr[idx][j] = inc;
inc++;
}
reversed = !reversed;
}
That should do it. I got it to work in repl.it, hopefully this helps.

Related

How can I rotate a matrix a clockwise?

Here's an example:
1 2 3
4 5 6
7 8 9
After rotating:
4 1 2
7 5 3
8 9 6
4x4 example:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12
5x5 is similar
I can rotate 90° but that's not true in this exercise
A element of the matrix just move once
I have tried for 2 hours to find the algorithm but my code doesn't work.
Please help me solve this problem
#include <iostream>
using namespace std;
void input(int **a,int row,int column)
{
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
cin>>a[i][j];
}
}
void output(int **a,int row,int column)
{
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
cout<<a[i][j]<< " ";
cout<<endl;
}
}
void rotate(int **a,int **b,int m,int n)
{
for(int i=0; i<n; i++)
{
int k=m-1;
for(int j=0; j<m; j++)
{
b[i][j]=a[k][i];
k--;
}
}
}
int main()
{
int m,n;
cin>>m>>n;
int **a=new int*[m];
for(int i=0; i<m; i++)
a[i]=new int[n];
int **b=new int*[n];
for(int i=0; i<n; i++)
b[i]=new int[m];
input(a,m,n);
rotate(a,b,m,n);
output(b,n,m);
return 0;
}
Although I didn't find some single mathematical formula for this rotation, I did it using four tiny loops.
Also my program supports any arbitrary size of matrix NxM, not only square, and not only odd sizes.
For simplicity of running following code snippet instead of reading matrix from std::cin, I inlined values of matrix elements as constant in code.
Also for simplicity I used std::vector not to do any new/delete operations of plain matrix. It is quite obvious how to adopt my solution to your case of plain array.
Try it online!
#include <vector>
#include <iomanip>
#include <iostream>
void output(auto const & a) {
for (size_t i = 0; i < a.size(); ++i) {
for (size_t j = 0; j < a[i].size(); ++j)
std::cout << std::setw(2) << a[i][j] << " ";
std::cout << std::endl;
}
}
void rotate(auto & a) {
int i_first = 0, i_last = a.size() - 1,
j_first = 0, j_last = a[0].size() - 1;
auto b = a;
while (i_first <= i_last && j_first <= j_last) {
for (int j = j_first + 1; j <= j_last; ++j)
b[i_first][j] = a[i_first][j - 1];
for (int i = i_first + 1; i <= i_last; ++i)
b[i][j_last] = a[i - 1][j_last];
for (int j = j_last - 1; j >= j_first; --j)
b[i_last][j] = a[i_last][j + 1];
for (int i = i_last - 1; i >= i_first; --i)
b[i][j_first] = a[i + 1][j_first];
++i_first; --i_last;
++j_first; --j_last;
}
a = b;
}
int main() {
std::vector<std::vector<int>> a = {
{ 0, 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},
};
std::cout << "Before:" << std::endl;
output(a);
rotate(a);
std::cout << "After:" << std::endl;
output(a);
}
Output:
Before:
0 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
After:
6 0 1 2 3 4
12 13 7 8 9 5
18 19 15 14 10 11
24 20 21 22 16 17
25 26 27 28 29 23

Smallest element in each row of matrix C++

This code can only return one smallest element in a matrix, but how if I want to return smallest element in each row? I need to use recursive function in C++. Thanks for your help
#include<iostream>
using namespace std;
int smallest(int** arr, int rows, int columns, int column_index = 0)
{
if (rows <= 0 || column_index >= columns)
return INT_MAX;
if (rows == 1)
return min(*(*arr + column_index),
smallest(arr, 1, columns - 1,
column_index + 1));
return min(smallest(arr, 1, columns),
smallest(arr + 1, rows - 1, columns));
}
int main()
{
int row, col, index=0;
cin >> row;
cin >> col;
int** arr;
arr = new int* [row];
for (int i = 0; i < row; i++) {
arr[i] = new int[col];
for (int j = 0; j < col; j++) {
cin >> arr[i][j];
}
}
cout<<smallest(arr, row, col, index);
return 0;
}
I think this much code will be sufficient if you use standard algorithm - std::min_element:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
int r, c;
std::cin >> r >> c;
std::vector<std::vector<int>> mat(r, std::vector<int>(c));
for (auto &&row : mat)
for (auto &&ele : row)
std::cin >> ele;
for (auto &&row : mat)
std::cout << *std::min_element(row.begin(), row.end()) << std::endl;
}
If you want to do it your way (old school style, using recursion) then do something like this. You just need to fix the index of row while calling smallest. Below is some self-explanatory code :
#include <algorithm>
#include <iostream>
// here row_index represents the index of row and col represents the number of
// elements in that row which are not yet traversed (counted from beginning)
int smallest(int **arr, int row_index, int col) {
// only first element is not traversed
if (col == 1)
return arr[row_index][0];
// return minimum of last element and value returned by recursive call for
// first col - 1 elements
return std::min(arr[row_index][col - 1], smallest(arr, row_index, col - 1));
}
int main() {
int row, col;
std::cin >> row;
std::cin >> col;
int **arr = new int *[row];
for (int i = 0; i < row; i++) {
arr[i] = new int[col];
for (int j = 0; j < col; j++)
std::cin >> arr[i][j];
}
// call the function for each row
for (int i = 0; i < row; i++)
std::cout << "Smallest element in row " << i + 1 << " : "
<< smallest(arr, i, col) << '\n';
}
You can write a recursive function that finds the smallest element in a one-dimensional array that will be called for each "row" of an array of arrays or of a two-dimensional array.
Here is a demonstrative program.
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <ctime>
const int * smallest( const int *a, size_t n )
{
return n < 2 ? a
: std::min( a, smallest( a + 1, n - 1 ),
[]( const int *p1, const int *p2 )
{
return not ( *p2 < *p1 );
} );
}
int main()
{
std::srand( ( unsigned int )std::time( nullptr ) );
size_t rows, cols;
std::cin >> rows >> cols;
int **a = new int *[rows];
for ( size_t i = 0; i < rows; i++ )
{
a[i] = new int[cols];
for ( size_t j = 0; j < cols; j++ )
{
a[i][j] = std::rand() % ( rows + cols );
}
}
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < cols; j++ )
{
std::cout << std::setw( 2 ) << a[i][j] << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
for ( size_t i = 0; i < rows; i++ )
{
std::cout << std::setw( 2 ) << *smallest( a[i], cols ) << ' ';
}
std::cout << '\n';
for ( size_t i = 0; i < rows; i++ )
{
delete [] a[i];
}
delete [] a;
}
If to enter the numbers of rows and columns equal to 10 then the program output might look like
1 16 7 6 2 7 1 14 3 8
0 14 9 0 6 18 18 7 7 19
12 17 9 12 14 10 7 9 15 3
14 8 19 13 14 1 12 15 7 15
16 7 1 17 19 8 15 18 7 15
9 19 12 10 3 18 0 10 7 8
6 13 16 17 7 3 19 19 18 6
7 14 6 8 3 17 8 19 7 16
6 16 7 10 19 11 1 19 13 8
19 19 14 8 17 1 11 8 12 1
1 0 3 1 1 0 3 3 1 1
Pay attention to that the function should return a pointer to a smallest element because in general the function can be called with the second argument equal to 0. In this case returning any integer value does not make a sense because it can coincide with an actual value.

C++ "Flipping" the rows in an array

I am attempting to print out an array in a specific order, where it is formatted with columns and rows in ascending order, but with the bottom row containing the lowest values. The array is created via a for loop and a pointer.
here is my code so far:
#include <iostream>
#include <iomanip>
int main()
{
// Creation of the array
int* array = new int[24];
for (int i = 0; i < 24; i++)
{
array[i] = i;
}
// Displaying in grid format with 3 rows and 8 columns
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 8; j++)
{
std::cout << std::setw(2) << *array << ' ';
array++;
}
std::cout << '\n';
}
}
The output of my code is:
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
The desired output is:
16 17 18 19 20 21 22 23
8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7
How would I go about "reversing" the rows to reach the desired output?
SOLVED WITH THE FOLLOWING
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int* table = new int[40];
for (int i = 0; i < 40; ++i)
{
table[i] = i;
}
for (int i = 4; i >= 0; --i)
{
for (int j = 0; j < 8; ++j)
{
cout << setw(2) << table[j + (8 * i)] << ' ';
}
cout << "\n";
}
}
Thanks for the help.

Two different sized hard-coded Arrays into One 2-Dimensional Vector

I am using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. The arrays are size 4 and size 13.
I want COLUMN 0 to have: 55, 66, 77, 88.
I want COLUMNs 1-12 to have 1,2,3,4,5,6,7,8,9,10,10,10,11 in EACH ROW.
It would seem that the 2nd loop for the size 13 array would need to loop 4 times in order to fill 4 rows, however, I'm not sure how to do that.
Here is what I have so far in code and output:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int typeArray[4] = {55,66,77,88};
int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS;
// 0 = VALUE all cells are initialized to
vector< vector <int> > myVector(4, vector<int> (14,0));
for (int i = 0; i < myVector.size(); i++)
{
myVector[i][0] = typeArray[i];
for (int j = 0; j < myVector[i].size(); j++)
{
myVector[1][j] = valArray[j];
}
}
// print vector to screen with 4 ROWS, 1 COLUMNS
for (int i = 0; i < myVector.size(); i++)
{
for (int j = 0; j < myVector[i].size(); j++)
{
cout << myVector[i][j] << ' ';
}
cout << '\n';
}
OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------
55 0 0 0 0 0 0 0 0 0 0 0 0----first row-----------------------------------------------------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 10 10 11----second row-----------------------------------------------------------------------------------------------------
77 0 0 0 0 0 0 0 0 0 0 0 0-------third row----------------------------------------------------------------------------------------------------------
88 0 0 0 0 0 0 0 0 0 0 0 0-------fourth row
int typeArray[4] = {55,66,77,88};
int valArray[14] = {0,1,2,3,4,5,6,7,8,9,10,10,10,11};
// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS;
// 0 = VALUE all cells are initialized to
vector< vector <int> > myVector(4, vector<int> (14,0));
for (int i = 0; i < myVector.size(); i++)
{
myVector[i][0] = typeArray[i];
for (int j = 0; j < myVector[i].size(); j++)
{
myVector[1][j + 1] = valArray[j];
}
}
// print vector to screen with 4 ROWS, 14 COLUMNS
for (int i = 0; i < myVector.size(); i++)
{
for (int j = 0; j <= myVector[i].size(); j++)
{
cout << myVector[i][j] << ' ';
}
cout << '\n';
}
OUTPUT----------------------------------------------------------------------------------------------------------------------------------------------------
55 1 2 3 4 5 6 7 8 9 10 10 10 11----first row-------------------------------------------------------------------------------------------------------
66 1 2 3 4 5 6 7 8 9 10 10 10 11----second row--------------------------------------------------------------------------------------------------
77 1 2 3 4 5 6 7 8 9 10 10 10 11----third row----------------------------------------------------------------------------------------------------
88 1 2 3 4 5 6 7 8 9 10 10 10 11----fourth row
This is the revised code per Kerrek's update, it works perfectly:
int typeArray[4] = {55,66,77,88};
int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS;
// 0 = VALUE all cells are initialized to
vector< vector <int> > myVector(4, vector<int> (14,0));
for (int i = 0; i < myVector.size(); i++)
{
myVector[i][0] = typeArray[i];
for (int j = 1; j < myVector[i].size(); j++)
{
myVector[i][j] = valArray[j - 1];
}
}
// print vector to screen with 4 ROWS, 14 COLUMNS
for (int i = 0; i < myVector.size(); i++)
{
for (int j = 0; j < myVector[i].size(); j++)
{
cout << myVector[i][j] << ' ';
}
cout << '\n';
}
OUTPUT IS THE SAME AS THE 2ND BLOCK OF CODE, YES!
Something like this perhaps:
vector< vector <int> > myVector(4, vector<int> (14, 0)); // one longer!
for (int i = 0; i < myVector.size(); i++)
{
myVector[i][0] = typeArray[i];
for (int j = 1; j < myVector[i].size(); j++)
{
myVector[i][j] = valArray[j - 1];
}
}

C++ Multidimensional Array

i'm trying to figure out how to fill a multidimentional array in this way:
Input: rows = 3 , cols = 3 :
1 4 7
2 5 8
3 6 9
can somebody give me an idea?
P.S My task is to find how many nubers stay in the same position in both arrangements. Ex:
1 4 7 1 2 3
2 5 8 4 5 6
3 6 9 7 8 9
so the numbers that are in the same position are : 1 5 9.
i've tryied :
//n = 3 , m = 3
for(int i = 0; i <n; i++) {
for(int j = 0; j <m; j++){
if(array[i][j] == array2[i][j]) {
lol++;
}
}
}
cout<<lol;
/*
1 2 3
4 5 6
7 8 9
1 4 7
2 5 8
3 8 9
*/
it must show me 3 , but it shows 0, where is the problem?
Populate at initialization:
int a[3][3] = { { 1, 4, 7},
{ 2, 5, 8},
{ 3, 6, 9}
};
EDIT (unsure if resolved):
After update to question here is an example application that (with modification to accept input from user) will diff two arrays and construct an array indicating the elements that were the same and a count of the number of identical elements:
#include <iostream>
int** make_array(const size_t a_rows, const size_t a_columns)
{
int** result = new int*[a_rows];
for (size_t i = 0; i < a_rows; i++)
{
*(result + i) = new int[a_columns];
}
return result;
}
void print_array(int** a_array, const size_t a_rows, const size_t a_columns)
{
for (size_t r = 0; r < a_rows; r++)
{
for (size_t c = 0; c < a_columns; c++)
{
std::cout << *(*(a_array + r) + c) << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
int main()
{
// Example data.
int a[3][3] = { { 1, 4, 7},
{ 2, 5, 8},
{ 3, 6, 9}
};
int b[3][3] = { { 1, 2, 3},
{ 4, 5, 6},
{ 7, 8, 9}
};
size_t rows = 3;
size_t columns = 3;
// Create three arrays:
// - two input arrays
// - array that represents which elements are the same
int** in_1 = make_array(rows, columns);
int** in_2 = make_array(rows, columns);
int** diff = make_array(rows, columns);
// Populate with example data.
for (size_t r = 0; r < rows; r++)
{
for (size_t c = 0; c < columns; c++)
{
*(*(in_1 + r) + c) = a[r][c];
*(*(in_2 + r) + c) = b[r][c];
}
}
// Diff.
// The 'diff' array will hold '1' for elements that
// were the same and '0' for elements that were not.
size_t same_count = 0;
for (size_t r = 0; r < rows; r++)
{
for (size_t c = 0; c < columns; c++)
{
*(*(diff + r) + c) = *(*(in_1 + r) + c) == *(*(in_2 + r) + c);
same_count += *(*(diff + r) + c);
}
}
std::cout << "\n";
// Results.
print_array(in_1, rows, columns);
print_array(in_2, rows, columns);
print_array(diff, rows, columns);
std::cout << "Same element count: " << same_count << "\n";
// Free...
return 0;
}
Output:
$ ./cpp/main.exe
1 4 7
2 5 8
3 6 9
1 2 3
4 5 6
7 8 9
1 0 0
0 1 0
0 0 1
Same element count: 3
Create a dynamically allocated array if you only know the dimensions at runtime:
int** x = new int*[rows];
for ( int i = 0 ; i < rows ; i++ )
x[i] = new int[cols];
And then populate it:
for ( int i = 0 ; i < rows ; i++ )
for ( int j = 0 ; i < cols ; j++ )
x[i][j] = y;
Or better yet, use a vector of vectors, this will give you more flexibility:
std::vector<std::vector<int> > x;
A simple solution would be this:-
int k = 1;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
a[j][i] = k; // filling it in the column first order
++k;
}
}