Single Dimensional Arrays

Consider the case where we want to record the test marks for 30 students in a class.
Without arrays, we would have to allocate 30 almost-identical variables.

int marks1;
int marks2;
int marks3;
int marks30;

Arrays give us a much easier way to do this.
They are a way to group number of items into a larger unit.
Arrays can have data items of simple types like int or float, or even of user-defined types like structures and objects.

The simplest form of an array is a 1D array (Single dimensional).
Array declaration uses the following syntax base-type array-name [size];

Here base-type declares the type of each element in the array.
The array-name specifies the name with which the array will be referenced and the size defines how many elements the array will hold.
The size must be an integer constant without any signs.

Example: int marks[20]; The above statement declares an array marks which has 20 elements.

We can directly access individual elements of an array using the indices.
A point to note that arrays are 0-indexed, means indexing of an array starts from 0.
So above declared array has 20 elements from marks[0] to marks[19].

Example: marks[7] = 20;

We accessed the 8th element of marks array (remember arrays are 0-indexed) and assigned it the value 20.
Accessing the elements using the indices is called indexing the array.

Note: For an array of size N, the array elements are numbered from 0 to N-1.
This is called the array’s range.

Initialization of 1D Arrays

Arrays can also be initialized at the time of declaration like variables.
Example: int arr[5] = {1, 2, 3, 4, 5};
We initialized an array of 5 elements at declaration time.

How are arrays stored in memory?

In memory, one-dimensional arrays are implemented by allocating a sequence of addressed locations so as to accommodate all its elements.
The starting address of the very first element of the array is called base address of the array.
The elements of array are given contiguous memory locations.
That’s why it is possible to randomly access any element of the array.
Let's take an example:


The above image can be looked as a top-level view of an array.
Each element can be uniquely identified by their index in the array.
When we declare an array, the base address of the array is stored in the variable name.
An when we say access 5th element (arr[4]), it just moves 4 steps forward and accesses that element (pointer arithmetic). If an array would not store its elements in contiguous memory locations, we would not be able to randomly access any element (like in linked lists).

Let's look at a complete program.

#include <iostream>
using namespace std;
int main()
    //array declared for storing marks of students
    float marks[100];

    //loop to read 100 marks
    for(int i = 0; i < 100; i++)
        cout<<"Enter marks for student "<<i+1<<": ";


    //loop to print all marks
    for(int i = 0; i < 100; i++)
        cout<<"Marks of student "<<i+1<<" : "<<marks[i]<<"\n";
    return 0;

I hope this article helped you better understand how single dimensional arrays work.
These concepts can be applied to any programming language.



Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?