Table of Contents
There are two types of data in C++: Fundamental Data Types, and Derived Data Types. The fundamental data type such as integer, character, boolean, are in their simplest form and cannot be divided or broken into simpler types. The derived data type is made up of one or more fundamental data types. An array is a derived data type that we will discuss in this lesson.
Arrays in C++
An array stores a list of values in a linear order. The purpose of an array is to group up similar data and store them inside one single variable.
To store 5 different numbers, we will need 5 variables like this:
int num1, num2, num3, num4, num5;
But with arrays, we can simply declare an int array that can hold 5 integers:
int num[4];
Array Terminology
Element
An array stores a list of values. Each value or data of an array is called an array element.
Size or Length
The total number of elements of an array is called its size or length. An array with a size/length of 4 can store four elements at max.
Index
An array stores the elements in a linear order. Every element of an array has a position called an index. You must know the index to retrieve or change that particular array element.
The index of an array in C++ always starts from 0 to length – 1. If we have an array of 3 elements then the first, second, and third elements will have index 0, 1, and 2, respectively.
Traversing An Array
To visit each element of an array is called traversing an array. It is necessary to traverse an array to display its values or to perform some form of processing.
Array Traversal can be performed using a loop.
C++ Array Syntax
Array Declaration
We can declare an array using the square bracket notation. And inside the square bracket, the array size must be specified.
The general syntax is:
datatype arrayname[size];
Example
/* declare an integer array of size 20 */ int num[20]; /* declare a character array of size 50 */ char name[50];
C++ Arrays can store only elements with the same data type. So, an integer array can only store a list of integers and not a mixture of characters and numbers.
Initialization of an Array in C++
We can optionally initialize the declared array with default values inside braces.
The general syntax is:
datatype arrayname[] = { element1, element2, element3, … };
Example
/* An integer array of length three with the values 50, 100 and 200 as default */ int array[3] = { 50, 100, 200 }; The array size is optional if we initialize it, /* A character array initialized with 4 different characters */ char array[] = {'a', 'b', 'c', 'd' };
Accessing array elements
We can access the array element by using an index. Remember that the index starts at 0 for the first element and so on.
The general syntax for accessing an array element:
arrayName[index]
Example:
int array[5] = { 8, 3, 1, 3, 2 }; /* access & print the third element of the array */ cout<<array[2]; /* Always use the index one less than the position of the element you want to access */
Assigning values to array elements
Similar to reading an array value, we can simply assign the value we want to store at that array index:
arrayName[index] = myValue;
Example:
int array[5]; /* store array values */ array[0] = 3; array[3] = 5; /* print the stored value */ cout<<”first element: "<<array[0]<<", fourth element: "<<array[3];
Memory representation of an array
Array elements are stored in contiguous memory locations. So, if the first element of an integer array is stored in a memory location of 4000, then the next will be stored at 4002, then 4004, etc. Each integer element occupies 2 bytes of data.
This is the reason why the position(index) of the array elements is important to access the array values.
Working with arrays
Arrays are required most of the time to store similar data. In the practical world, arrays are used to store a list of users, a list of posts, ratings of a product, etc.
Reading inputs and processing an array
Most of the array values are inputs received from the user.
An array is a list of multiple values. So to process each element one by one we need to use a loop construct (for, while, do-while).
Inside the loop, we can read inputs, perform calculations or display the array contents. Here is an example that performs the sum of five numbers:
/* declare the array */ int size = 5, sum = 0, num; int numbers[size]; /* find the sum of any five numbers */ /* loop will run five times for each element of the array */ for ( int i = 0; i < size; i++ ) { cout<<"Enter num "<<i+1<<": "; cin>>num; /* assign the value of the input to the array element at index i */ numbers[i] = num; } /* now array contains all the inputs. */ /* PROCESSING AN ARRAY */ for( int i = 0; i < size; i++) { /* numbers[i] will return the value of the array */ sum += numbers[i]; } /* print the sum */ cout<<" Sum is "<<sum;
Character Arrays & Strings
C++ has a character data type but not any string type. A character variable cannot store any word or text. It can store only a single value. Eg: ‘a’, ‘b’, ‘*’, ‘[‘, ‘1’, etc.
Strings are a sequence of characters. It is used widely in many cases. Eg: “hello world”, “welcome”, “good morning”, etc. An array of characters are used to represent strings in C++.
/* define a character array */ char name[] = { 'J', 'o', 'h', 'n' }; /* we can also initialize strings inside double quotes */ char name2[] = "David"; char name3[5] = "ball"; cout<<name<<endl<<name2<<endl<<name3<<endl; /* Displays John David ball */
Points To Remember
- An array is a derived data type.
- An array is an ordered collection of elements. It is used to store similar types of values.
- Array element uses the square bracket notation to distinguish from other variables.
- The array size must be specified when declaring an array. It is optional if array values are initialized.
- Index of an array represents the position of an array element. It always starts from 0.
- Array elements can be traversed inside a loop, for reading, displaying, or processing its values.