Programmers learn & share
0 votes
9 views

Problem:

Given the array a, write an expression that refers to the first element of the array?

by (5k points)   | 9 views

2 Answers

0 votes

Solution:

a [0]

by (9.1k points)  
0 votes

The expression that refers to the first element of an array can be represented as;

a[0];

in this expression a is the variable of which is declared as an array type and 0 represents the first index element in of an array.

Example:

For Example, an array is;

Array[4, 9, 25, 49];

The first element is 4 that is at position 0. If you want to get element 25, then it can be accessed by using the index 2.

Reason to start array from 0:

The convenient reason to start an index from 0 in computers is the concept of offset. An offset is a number representing the distance from a location in memory and hard disk or any other addressable medium. In computers all the data is stored linearly meaning that the data is stored in order. Counting the arrays from 0 simplifies the computation of the memory address of each element.

The usage of an array index as an offset is not just the requirement by just a habit. The offset of the first element could be hidden by the system and taken into consideration when allocating and referencing elements.

by (2.5k points)  
2,220 questions
2,690 answers
59 comments
241 users