C++ Passing pointer to multi-dimensional arrays - c++

So I have a pointer to a 2D array like so:
int board[3][5] = { 3, 5, 2, 2, 1, 3, 4, 34, 2, 2, 3, 4, 3, 223, 923 };
int* ptr[sizeof(board[0]) / sizeof(board[0][0])] = board;
I'm trying to follow this example. But for some reason I'm getting the error:
IntelliSense: initialization with '{...}' expected for aggregate
object
Any idea what the problem is?

Assign pointer to the first element of the array like below
int (*ptr)[5] = board;
Note: Column size [5] in the pointer declaration should be equal to the original 2 dimension array column size [5].
Declaring row size [3] is optional.
int main() {
int board[3][5] = { 3, 5, 2, 2, 1, 3, 4, 34, 2, 2, 3, 4, 3, 223, 923 };
/*
// 3 Rows 5 Columns Matrix
int board[3][5] = { {3, 5, 2, 2, 1 },
{3, 4, 34, 2, 2 },
{3, 4, 3, 223, 923}
};
*/
// Assign pointer to the first element of the array
int (*ptr)[5] = board;
for(int i=0; i< (3*5); i++) {
std::cout<<(*ptr)[i]<<std::endl;
}
return 0;
}

A 2D array is not the same as an array of pointers. You cannot directly convert one to the other.

I just needed to put () around the *ptr. I have no idea how this fixes it but now I can do ptr[1][2].

Related

C++ How to add two arrays of unequal sizes using the for loop?

So the aim is to take two arrays as shown below
int x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k[4] = {1, 2, 3, 4};
and add each element of k to each element of x in a loop as shown
1 2 3 4 5 6 7 8 9 10
+1 +2 +3 +4 +1 +2 +3 +4 +1 +2
This should give us a final array [2, 4, 6, 8, 6, 8, 10, 12, 10, 12].
Any suggestions as to how I could achieve this in C++
Loop through the indexes of the larger array, using the modulus (%) operator to wrap-around the indexes when accessing the smaller array.
int x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k[4] = {1, 2, 3, 4};
int res[10];
for (int i = 0; i < 10; ++i) {
res[i] = x[i] + k[i % 4];
}
Online Demo
With % you can have the wrap-around behavior and with std::size(from C++17 onwards) the size of the array.
#include <algorithm>
#include <iostream>
int main()
{
int x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k[4] = {1, 2, 3, 4};
for(int i = 0; i < std::size(x); ++i)
{
x[i] = x[i] + k[i%std::size(k)];
}
//lets confirm if x has the right elmennts
for(const int& element: x)
{
std::cout<< element<<std::endl;
}
}
Note that here i have not used a separate array to store the resulting array. Instead the elements are added into the original array x. Storing the result in a new array is trivial.

Issue with passing an argument to function

I'm working with C++ and found a problem. I want to pass an argument to a function. The argument must be a 2d array. When I try to do it, I get 2 errors:
Too many initializer values
and
initializing cannnot convert from initializer list to size_t**
How do I fix this? I've tried with changing it as 5x5 matrix, but it doesn't make it good.
size_t** matrix =
{
{1, 16, 20, 23, 25},
{6, 2, 17, 21, 24},
{10, 7, 3, 18, 22},
{13, 11, 8, 4, 19},
{15, 14, 12, 9, 5},
};
set<bool> set1 = iterateover(matrix);
The function:
std::set<bool> iterateover(size_t **arrayy)
size_t** matrix defines a pointer to a pointer to a size_t. An array is not a pointer. It can decay to a pointer, but in the case of a 2D array, it decays to a pointer to a 1D array, not to a pointer to a pointer.
The closest thing I can think of to what you seem to be after is
// here be the data
size_t actual_matrix[][5] = // note: We can omit the first dimension but we cannot
// omit the inner dimensions
{
{1, 16, 20, 23, 25},
{6, 2, 17, 21, 24},
{10, 7, 3, 18, 22},
{13, 11, 8, 4, 19},
{15, 14, 12, 9, 5},
};
// an array of pointers to the rows of actual data. This 1D array of pointers will
// decay to a size_t **
size_t * matrix[] =
{
actual_matrix[0],
actual_matrix[1],
actual_matrix[2],
actual_matrix[3],
actual_matrix[4],
};
// now we have the correct type to use with iterateover
std::set<bool> set1 = iterateover(matrix);
I want to pass an argument to a function. The argument must be a 2d array.
You can make iteratreOver a function template which can take a 2D array by reference, as shown below. You can make additional changes to the function according to your needs since it is not clear from the question what your iterateover function does. I have just printed all the elements inside the 2D array.
#include <iostream>
template<typename T,std::size_t N, std::size_t M>
void iterateOver(T (&arr)[N][M])
{
for(std::size_t i= 0; i < N; ++i)
{
for(std::size_t j = 0; j < M; ++j)
{
std::cout<<arr[i][j] <<" ";
}
std::cout<<std::endl;
}
}
int main()
{
size_t matrix[5][5] =
{
{1, 16, 20, 23, 25},
{6, 2, 17, 21, 24},
{10, 7, 3, 18, 22},
{13, 11, 8, 4, 19},
{15, 14, 12, 9, 5},
};
//call iterateOver by passing the matrix by reference
iterateOver(matrix);
}
The output of the above program can be seen here:
1 16 20 23 25
6 2 17 21 24
10 7 3 18 22
13 11 8 4 19
15 14 12 9 5

In C and C++, how to move some elements in an array into a new array?

For example, how can I move the 1st, 5th and 10th elements in array A to a new three-elements array B without assigning separately for three times?
In C, just declare and initialize a new array with the selected elements of your array. No assignment needed.
int main(void)
{
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[3] = {a[0], a[4], a[9]};
return 0;
}
Remember that initializers for arrays with automatic storage duration does not have to be constants.
Just do the three assignments! Why do you avoid it?
int ar1[10], ar2[10];
ar2[0] = ar1[0];
ar2[4] = ar1[4];
ar2[9] = ar1[9];
However, if you have lots of indices to move, perhaps you need another way.
I suggest this:
int ar1[1000], ar2[1000];
int indices[] = { 1, 3, 54, 6, 23, 35, 9, 42, 44, 995, 722, .... };
for (int i = 0; i < sizeof(indices) / sizeof(indices[0]); i++)
{
ar2[i] = ar1[i];
}

Initialize a 2 Dimensional vector<int> in CPP

How to initialize a 2 dimensional vector<int> in C++?
For instance I have 4 arrays each of length 8 ints, like the below
int a1[] = {1,2,3,4,5,6,7,8};
int a2[] = {1,2,3,4,9,10,11,12};
int a3[] = {1,2,5,6,9,10,13,14};
int a4[] = {1,3,5,7,9,11,13,15};
and I have this
vector< vector <int> > aa (4);
aa[i] (a1,a1+8);
But this gives error. I even tried supplying the array a1 to v1 and passed v1 to aa[i] , still it fails.
So what would be the proper way of initializing the elements of a 2 dimensional vector<int>
aa[i].assign(a1,a1+8);
int arr[4][8] =
{
{1, 2, 3, 4, 5, 6, 7, 8},
{1, 2, 3, 4, 9, 10, 11, 12},
{1, 2, 5, 6, 9, 10, 13, 14},
{1, 3, 5, 7, 9, 11, 13, 15},
};
std::vector<std::vector<int> > vec(4, std::vector<int>(8));
for (int i = 0; i < 4; ++i)
{
vec[i].assign(arr[i], arr[i] + 8);
}
The initialization of aa also initialized all four of the contained vector<int> objects, using the default constructor for vector<int>. So you'll need to add data to those empty vectors, not initialize them.
Try for example:
std::copy(a1, a1+8, std::back_inserter(aa[i]));

swapping 2 numbers in 2 dimensional array

My question is as follows:
Refer to the following array declaration in the main():
const int size = 4;
int x[size][size] = {{1, 2, 3, 4}, {5, 6, 7, 8},
{9, 8, 7, 3}, {2, 1, 7, 1}};
Write a function SwapRows() to swap two rows of the above 2D array. For
example, if the function has been called to swap the first and the second rows of the
above 2D array then the result would be that the first row now becomes {5, 6, 7, 8}
and the second row now becomes {1, 2, 3, 4}. The function receives as parameter the
2D array, the size of the array, and two integers to indicate the rows to swap.
Help,,how can i go about this?????
Note: Using C++ Language
Pseudo code:
SwapRows(x[size][size], row0, row1, size)
for col = 0 to size - 1 do
temp = x[row0][col]
x[row0][col] = x[row1][col]
x[row1][col] = temp
Now all you need to do is convert the pseudo code into C++, then test, debug and document it.
#include <algorithm>
void SwapRows(int arr[][4], int r1, int r2)
{
std::swap(arr[r1],arr[r2]);
}