An array can be intialized in the definition in the following way: int grades[] = {10, 20, 30};
Note: In the above line, the size of the array is not specified. The
compiler will determine the size based on the number of elements being
used in
the initialization
Alternatively, you can define the array on a separate line, and
initailize
it later. e.g.,
int grades[3];
for (int i=0; i < 3; i++){
cin >> grades[i];
}
It is good programming practice to define the array size value as a
const int value. e.g.,
const int MAX_SIZE = 3;
int grades[MAX_SIZE];
for (int i=0; i < MAX_SIZE; i++){
cin >> grades[i];
}
If arrays are local to a function, define them as static
arrays
to prevent them from being reinitialized every time the function is
called
Arrays defined and initialized with the const specifier
cannot
have their elements modified