1 / 6
May 2006

Does anybody know how to change the default value when initializing an array?

If I want to initialize a multidimensional array of ints (default value for int is 0) and I want it to initialize with -1, is there any way to get around it without having to go like:

int myArray [HEIGHT][WIDTH];
int n, m;

for (n = 0; n < HEIGHT; n++)
for (m = 0;m < WIDTH; m++)
myArray[n][m] = -1;

I don't like this approach since it's a nested loop and the idea is to speed up, plus I think it's very cumbersome.

Thanks

  • created

    May '06
  • last reply

    Mar '11
  • 5

    replies

  • 203

    views

  • 4

    users

Hi,

I can imagine two possibilities to achieve that:

int x[10] = { [0 ... 9] = 2 };
int a[10][10] = { [0 ... 9][0 ... 9] = 1 };

Note the spaces around the "..."!
The first line is an example for a one-dimensional array, the second for two dimensions.
This is a GNU extension for the C Compiler, so it won't work in most places, especially not within the C++ Compiler. But it should work on SPOJ. I wouldn't use such constructs myself, though.

A more common way to do it is:

int x[10];
void init()
{
  memset(x, 0xFF, 10 * sizeof(int));
}

There are some variations, so you can specift sizeof(x) instead of 10 * sizeof(int) if you want to initialize the whole array and the size is known at compile time.
But the important thing to remember is: That value is assigned bytewise, not elementwise. Therefore memsetting the value 0x01 will result in integers with a value of 0x01010101 = 16843009, not 1 as one might expect.

But in essence: The initialization shouldn't be the bottleneck...

Greetings

Well thanks a lot overwise. I'll consider the 2nd option.
I was thinking that I could also use the Vector class like:

vector<int> v1( 10, -1 );

but then I'd have to work my way around to make it multidimentional.

Well, that's easy:

vector< vector<int> > v(100, vector<int>(50, -1));

is 'equivalent' to

int v[100][50];
for (int i = 0; i < 100; i++)
  for(int j = 0; j < 50; j++)
    v[i][j] = -1;
6 months later

If you really want to use arrays, this would be cumbersome but you could do it for certain cases:

int a[] = {-1,-1,-1};
int b[] = {-1,-1,-1};
int c[] = {-1,-1,-1};
int d[] = {-1,-1,-1};

int** doublearray[4];
doublearray[0] = a;
doublearray[1] = b;
doublearray[2] = c;
doublearray[3] = d;

This should give you a 4x3 array initialized to -1, but of course this method is only possible if you know the dimensions of the array before compiling.

4 years later

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:

type name [elements];

where type is a valid type (like int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies how many of these elements the array has to contain.

Therefore, in order to declare an array called billy as the one shown in the above diagram it is as simple as:

int billy [5];