is there any way for an _array_ to have all the VARIABLES have the same initial value?

2

There is some way for an array to have all VARIABLES have the same initial value without using a FOR or WHILE loop.

here is an example of how I managed it with a FOR loop.

#include<iostream>
using namespace std;

int main(){
    int a[10];
    for(int i = 0;i < 10;i++){
        a[i] = 0;
    }
}

The idea is that all the VARIABLES that are inside the array to have the same value.

NOTE: it was an array , not a table (rookie error)

    
asked by bassily 15.06.2016 в 00:21
source

5 answers

2

It will depend on what initial value you want to assign to the values of that table and the type of them.

For the case that you expose in your example:

int a[10];

If you want to initialize to values 0 you have the following options:

Initialization by empty list

int a[10] {};

The array a receives an empty list as the initial value, this empty list starts all its elements at the default value of the type of the array ( int ), in this case the value is 0 ... so that all the elements of the array a will get the value 0.

Initialization by empty list copy

int a[10] = {};

The array a copy the elements of the list provided as the initial value, since this list is empty all its elements contain the default value of the type of the array ( int ), in this case the value 0 ... so all the elements of the array a will get the value 0.

The current compilers usually optimize this initialization so that the copy is omitted in most cases (performs an initialization of the value in situ being then equivalent to the initialization by empty list); however, the omission of copying in other more complex types is not guaranteed; whenever you have doubts about whether a list will be copied and you want to avoid such a copy: use Initialization by empty list.

Initialization in static space

static int a[10];

Static elements are initialized to the default value even when they are not assigned an initial value, so in the case of the static array a all their elements contain the default value of the array type ( int ) , in this case the value 0 .

In the case of wanting to initialize values other than 0, it is more complicated:

Initialization by list

int a[10] { 1 };

The array a receives a list as the initial value, this list contains a single element of the 10 that the array accepts ... what will happen is that the first element of the array will get the first value from the list and the rest of elements will be initialized to the default value of the type of the fix ( int ), which in this case will be 0 , this happens until filling all the values of the array:

int b[10] { 1, 1 };       // los 8 ultimos elementos contienen 0
int c[10] { 1, 1, 1, 1 }; // los 6 ultimos elementos contienen 0
int d[10] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; // El ultimo elemento contiene 0

Initialization by list copy

int a[10] = { 1 };

Follow the same rules as the previous initialization but copying the list provided (copy that is usually omitted by the compiler). Therefore all the elements to which value has not been specified, will obtain the default value of the type of the fix ( int ), in this case the value 0 .

If you want to initialize a value different from the default value, you must specify the value for each of the elements to be initialized; if that is your choice, you can omit the size of the array and let the compiler deduce it for you:

int a[] {}; // Tamanyo 0!
int b[] { 1, 1 };       // Tamanyo 2
int c[] { 1, 1, 1, 1 }; // Tamanyo 4
int d[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; // Tamanyo 9
    
answered by 15.06.2016 / 09:20
source
1

From what I understand, what you call "table" (why capital letters?), in C is called an "array". And the elements of an array are not "variables", the variable is (in your example) a , the elements of an array are called ... elements of an array.

In general, there is no simpler way to write all the elements of an array with the same value. The for loop is the appropriate way.

I say "in general" because in some particular cases it can be done with memset : if it is an array of bytes (or chars), or if they are int that you initialize to zero (as in your case) . That is, in your case you could replace the loop with

memset(&a[0], 0 , sizeof(int)*10);

but this is rarely preferable to for - it is darker and harder to maintain, and the gain is minimal.

If it is not an assignment, but the initialization itself (where the variable is declared), there is a simple way (always if you try to initialize to zero):

int a[10] = { 0 };

More info in English .

    
answered by 15.06.2016 в 04:09
1

You should take a look at the STL containers:

// Tabla de tamaño fijo
std::array<int,10> a; // Todos los valores inicializados a 0

// Tabla de tamaño variable
std::vector<int> b(10,2); // Todos los valores inicializados a 2

And if you can not always make use of the initialization by default (only works for fixes of fixed size):

int a[10] = { 0 };

And even if the above options do not work for you, you can also use the functions of the STL to initialize the array:

#include <algorithm>

int a[10];

// opción 1
std::fill(std::begin(a),std::end(a),0);

// opción 2
std::fill_n(a,10,0);

Greetings

    
answered by 15.06.2016 в 09:24
0

If I understand the question correctly, you can use the initialization notation online specifying the values, which can be the same or different, it does not matter.

int datos[3][3] = { 
    1,2,3, 
    4,5,6, 
    7,8,9 
};

As you can see, the values are loaded consecutively, the indentation is to facilitate reading.

Keep in mind that you must include the exact number of elements declared in the matrix (that is, 3 * 3 = 9).

    
answered by 15.06.2016 в 00:31
0

If it is a vector with size N, by default it is already initialized with zeros.

vector <int> A(N);
    
answered by 28.06.2018 в 02:26