Cpp - Array Array Creation

What Is an Array?

An array is a collection of data in the same type.

Each data in the array is called an element of the array.

An array is declared by writing the data type and array name followed by the number of elements the array holds inside square brackets.

Here's an example:

long my_array[25]; 

The my_array array holds 25 long integers.

Array elements are numbered from 0, so the my_array array holds elements from 0 through 24.

Each element can be accessed by using its number in square braces.

The following code assigns a value to the first my_array element:

my_array[0] = 100; 

This statement assigns a value to the last:

my_array[24] = 7804; 

The number of an array element also is called its subscript.

Please note that array index is zero-based.

The following code shows how to use array to store float value.

Demo

#include <iostream> 
 
int main() //from   w ww  .  ja  va2  s  .  c om
{ 
    float discount[4]; 
    discount[0] = 0.9; 
    discount[1] = 0.75; 
    discount[2] = 0.5; 
    discount[3] = 0.25; 
    float current = 100.0, target= 90.0; 
  
    for (int i = 0; i < 4; i++) 
    { 
          float loss = (current - target) * discount[i]; 
          std::cout << "Goal " << i << ": "; 
          std::cout << target + loss << std::endl; 
    } 
 
    return 0; 
}

Result