I have a 2d and i want to set the elements to zero without looping all the elements
int a[100][200];
I can't initialize them at point of declaration.
I have a 2d and i want to set the elements to zero without looping all the elements
int a[100][200];
I can't initialize them at point of declaration.
Try memset(a,0,sizeof(a));
This simply overwrites the memory used by the array with 0 bytes. Don't do this for user-defined data types unless you really know what you do. There's also std::fill and std::fill_n, which is more C++ish (but not the easiest way here).
I'm not sure if you can fill all the elements of 2D array in one go, but you can do this row by row, for example:
for (int i = 0; i < 100; i++)
{
std::fill(&a[i][0], &a[i][200], 0);
}
If this array is declared at file scope, or at function scope but with 'static', then it is automatically initialized to zero for you, you don't have to do anything. This is only good for one initialization, at program startup; if you need to reset it you have to code that yourself. I would use memset for that.
If it's declared at function scope without static, you need to use memset, or an explicit initializer - a single = { 0 } is enough, you don't need to write out all 2002 zeroes like someone else suggested.
Try
int a[100][200] = {{0}};
If you initialize some (but not all) of the elements, the rest will be initialized to zero. Here, you are only explicitly initializing the first element of the first sub-array and the compiler is taking care of the rest.
What exactly does "I can't initialize them at point of declaration" mean? "Don't know how to"? Or "can't modify the code there"?
The C++ language has a feature that initializes the entire array to 0 at the point of declaration
int a[100][100] = {};
(note, no explicit 0 is really necessary between the {}). Can you use it or not?
If you can't, then your options are: use the memset-hack, 1D-reinterpretation-hack or set each element explicitly by iterating through the array(s) (with or without the help fo std::fill).
The memset approach mentioned (memset(a,0,sizeof(a));) will work, but what about doing it the C++ way (per your tag), and using vector?
std::vector<std::vector<int> > a;
a.assign(100, std::vector<int>(200));
fill_n( a, 100*200, 0 );
or
fill_n( a, sizeof a / sizeof **a, 0 );
It's not much harder than memset, and much cleaner.
C++ allows multidimensional arrays to be iterated entirely through by a base-element pointer. So you can use std::fill and pass it the very first nonarray element
std::fill(a[0] + 0, a[99] + 100, 0);
In C this is formally not allowed, and you would need to iterate through each sub-array separately, because iterating beyond the first subarray's past-the-end pointer causes undefined behavior in C. Nontheless, in practice this still works.
I tested this solution and it worked.
int arr[100][200];
for (int i=0; i<100; i++)
for (int j=0; j<200; j++) (arr[i][j] = 0);
for (int i=0; i<100; i++)
for (int j=0; j<200; j++) cout << (arr[i][j]);