C++ Array and other languages are a collection of elements of the same variable type. They are placed in contiguous memory locations that are referenced individually. The way they are individually referenced is by adding an index to the identifier.

Normally dimensional arrays are more common because they are useful for holding characters and string variables.

The general form of declaring a one-dimensional array is:

variable_type variable_name[size];

For example, a c++ array containing five integer values of the variable type int is represented visually as:

array diagram

The code below outputs the elements in the c++ array

one dimesional array code 1

Output from the example code above. As you can see, it just outputs the array index numbers.

one dimensional array code 1 output

Setting and displaying c++ array values 

By default, regular arrays are left with no set values. The elements in an array are set to specific values when they are declared with braces like the example below:

int sample[5] = {10,20,50,100,200};

The elements are outputted on the screen with the help of the for loop code. Firstly you must output the array variable name which we named “sample”. And then you must type in “[i]” after it so the for loop elements are displayed.

one dimensional array code 2

The output of the set array values

one dimensional array code 2 output

Share this post