Array

Arrays are ways of having a block of memory allocated to a single variable type, this allows for holding the national lottery numbers within an array of 6 instead of actually having 6 different variables.

e.g.

int value1 = 1;
int value2 = 2;
int value3 = 3;
int value4 = 4;
int value5 = 5;
int value6 = 6;

and instead you could just have

int values[6] = {1,2,3,4,5,6};

this is a example of how to manipulate the array structure.

#include <iostream>
 
int main()
{
       // array structure
       int values[5];       // set asside 5 blocks of int(eger) size of continuous memory blocks
                     // the actual points into the array are 0-4 (e.g. 5 points but start at 0)
 
       int values2[6] = {0,1,2,3,4,5}; // exmaple of settings the values on define time.
 
       // direct assigning the values.
       values[0] = 5;
       values[1] = 4;
       values[2] = 3;
       values[3] = 2;
       values[4] = 1;
 
       //could use a position pointer
       for (int i = 0; i < 5; i++)
       {
              // display the values
              std::cout << "Value " << i << " : " << values[i] << "\n";
       }
 
       int *int1p = &values[0];
       std::cout << "Pointer value = " << *int1p << "\n";
       int1p++;              // increament the pointer to the next part of the array
       std::cout << "Pointer value = " << *int1p << "\n";
       return 0;
}

save as arrays.cpp and then compile the program, the result once executed will be

Value 0 : 5
Value 1 : 4
Value 2 : 3
Value 3 : 2
Value 4 : 1
Pointer value = 5
Pointer value = 4

and include three different ways to access the values within the array.

Leave a Reply

Your email address will not be published.