Broad Network


Arrays in C++

C++ Tutorials from Roots – Part 11

Forward: In C++ an array is a set of consecutive objects of the same type, in memory. We see how to create and use arrays in this part of the tutorials.

By: Chrysanthus Date Published: 22 Aug 2012

Introduction

This is part 11 of my series, C++ Taking the Bull by the Horns. In C++ an array is a set of consecutive objects of the same type, in memory. We see how to create and use arrays in this part of the tutorials. You can have a set of consecutive int objects; you can have a set of consecutive float objects; you can have a set of consecutive _Bool objects; you can have a set of consecutive Char objects. You cannot have a set of strings (see later).

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Array of Integers
The following is an array of 5 integers:

        int arrInt[] = {25, 20, 256, 5, 7};

The syntax to create an array is:

        Type arrayName[] = {value1, value2, value3, . . .};

You begin with the type for the objects that will be in the array. Then you have a space. Next you have the name (identifier) of the array. This is followed by the open square and close square brackets. Then you have the assignment operator. Next you have a block delimited by curly brackets. Inside the block, you have the data for the array. All the data must be of the same type. The data are separated by commas. After the last datum, there is no need for a comma. Each datum is kept in an object (region) in memory. The objects lie one next to another in memory, forming a consecutive set of objects.

The data in an array has meaning. For example an array of ints might be an array of students marks in a test. The name of the array will have to be related to the meaning of the data. The following is the creation of an array of marks for 10 students.

        int marks[] = {43, 29, 35, 50, 60, 65, 78, 56, 67, 90};

Arrays of floats, bools and chars
The following is the creation of an array of floats:

        float arrFlt[] = {12.56, 0.258, 5.4, 456.01};

The number of elements in the float array is 4.

The following is the creation of an array of _Bools:

        _Bool arrB[] = {1, 0, 0, 1, 0, 1};

The following is the creation of an array of chars:

        char arrChar[] = {'A', 'a', 'C', 'k', 'F', 'Y'};

Note that each char value in the block (array) is in single quotes.

Defining an Array
All the above arrays have been created by initialization. You can define an array and then assign the elements later. The syntax to define an array is:

        Type arrayName[size];

You begin with the type; a space; then array name; the square brackets. Inside the square brackets you have an integer, which is the size of the array. To define an array of int that will have a size (maximum number of elements) of 15, you would type something like:

    int myArr[15];

Index
Elements in an array have positions. Consider the following array:

        int marks[] = {43, 29, 35, 50, 60, 65, 78, 56, 67, 90};

The first element in the array is 43; the second is 29; the third is 35, and so on. The values in an array have positions. These positions are called indices. Index (position) counting in computing and arrays begin from zero, not one. So the index of 43 above is zero; that of 29 is 1; that of 35 is 2; and so on.

Accessing an Array Element
To access a value in an array, you need to know the index of the value. The syntax to access an array element (value) after the array has been defined or initialized is:

    arrayName[index]

If you want to access the first element of the above array, you would type:

    marks[0]

To access the second element you would type:

    marks[1]

To access the third element, you would type

    marks[2]

and so on. Always subtract 1 from the English position to have the index.

When accessing an array value, the index should not be more than the array size minus 1.

Assigning and Changing Array Value
After defining an array, the size of the array is known. Also, after initializing an array, the size of the array is known. After defining an array, it is empty. However, after initialization of an array, the array is not empty. Whatever is the case, you can assign a value or change the value of an element of an array as follows:

    arrayName = value;

Assume that you want a value of 47 for an int array at index, 5. To assign or change the value at index, 5, you would type:

    marks[5] = 47;

Do not forget the semicolon at the end of the statement (above). Remember, index 5 means English position 6.

Example
In the following example, an int array is defined. Five integers are assigned to this array and then displayed. The display is done using a for-loop.

#include <iostream>
using namespace std;

int main()
    {
        int myInt[5];

        myInt[0] = 8;
        myInt[1] = 63;
        myInt[2] = 55;
        myInt[3] = 78;
        myInt[4] = 2;

        for (int i=0; i<5; ++i)
            {
                cout << myInt[i]; cout << "\n";
            }

        return 0;
    }

We have come to the end of this part of the series. Before we leave this part, know that while initializing an array, you can still have the size of the array in the square brackets as in the following example:

        int marks[10] = {43, 29, 35, 50, 60, 65, 78, 56, 67, 90};

There are ten elements in the array, so you have 10 in the square brackets.

You now know the basics of an array in C++. Let us stop here and continue in the next part of the series where we shall see how the pointer is related to the array.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message