I'm working on a program using arrays and I'm trying to figure out
First, with the following array declaration, what is the value stored in the scores[2][2] element?
int scores[3][3] = { {1, 2, 3} };
And also with this array declaration, what is the value stored in the scores[2][3] element?
int scores[5][5] = {5};
Could someone please explain this for me.
int scores[3][3] = { {1, 2, 3} };
is equivalent to:
int scores[3][3] = { {1, 2, 3}, {0, 0, 0}, {0, 0, 0}};
The other one is similar. You know the answer.
Array indexing is zero-based.
Which means that for: int foo[3] = {10, 20, 30};
foo[0] is 10
foo[1] is 20
foo[2] is 30
For multidimensional arrays, you should think of them as arrays of arrays.
So this would create an array containing two int[3]s: int foo[2][3] = {{10, 20, 30}, {40, 50, 60}};
foo[0][0] is 10
foo[0][1] is 20
foo[0][2] is 30
foo[1][0] is 40
foo[1][1] is 50
foo[1][2] is 60
C supports partial initialization. In which it will default all non initialized values to 0.
So if you were to do this: int foo[3] = {5};
foo[0] is 5
foo[1] is 0
foo[2] is 0
Similarly for a multidimensional array: int foo[2][3] = {5};
foo[0][0] is 5
foo[0][1] is 0
foo[0][2] is 0
foo[1][0] is 0
foo[1][1] is 0
foo[1][2] is 0
Related
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];
}
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].
There is that way to set elements on array - int rgArrayNum [] = {16, 2, 77, 40, 12071};
How can I do same way on pointer with new ? I tried int *pArrayNum = new [] = {4 ,3 ,3} ; but it didn't worked .
In c++11, you can write:
int *pArrayNum = new int[3]{4, 3, 3};
However, in c++03 array new initialization is not allowed; you'd have to initialize the members individually or by copy from an array on the stack:
int rgArrayNum [] = {16, 2, 77, 40, 12071};
int *pArrayNum = new int[sizeof rgArrayNum / sizeof rgArrayNum[0]];
std::copy(&rgArrayNum[0], &rgArrayNum[sizeof rgArrayNum / sizeof rgArrayNum[0]],
pArrayNum);
In C++03 and earlier, you can't initialise the values of a dynamic array to anything except zero.
You can achieve something similar in C++11:
int *pArrayNum = new int [3] {4, 3, 3};
or if you don't mind using a container to manage the memory for you:
std::vector<int> array = {4, 3, 3};
You have to create the array not with integers but with integer pointers.
int* rgArrayNum2 [] = {new int(16), new int(16), new int(16), new int(16), new int(16)};
//test
int* test = rgArrayNum2[2];
*test = 15;
now rgArrayNum2[2] is 15.
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]));
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]);
}