1.1 Arrays in Data Structure | Declaration, Initialization, Memory representation

An array is a collection of elements, each identified by at least one array index or key.


  • Declaration: In most programming languages, arrays can be declared by specifying the type of elements they will hold followed by the array name and size. For example, in C, an integer array can be declared as int numbers[10]; which creates an array that can hold ten integers.

  • Initialization: Arrays can be initialized at the time of declaration or later. For instance, int numbers[] = {1, 2, 3, 4, 5}; initializes the array with five integer values.

  • Memory Representation: Arrays are stored in contiguous memory locations, which allows for efficient access to elements using their indices. The memory allocation for an array is determined at compile time, making it a static data structure.


Here are some problems related to arrays that you can practice solving:

  1. Declaration and Initialization: Declare an array of type float that can hold 5 elements. Then, initialize it with the values 1.5, 2.5, 3.5, 4.5, and 5.5.

  2. Access Elements: Given the array int numbers[] = {10, 20, 30, 40, 50};, write a statement to access the third element of the array.

  3. Finding the Length: Write a function that takes an integer array and returns the number of elements in the array. For example, if given the array: {1, 2, 3, 4}, your function should return 4.

  4. Sum of Elements: Create a program that calculates the sum of all elements in an integer array. For instance, for the array int arr[] = {1, 2, 3, 4, 5};, the program should output 15.

  5. Reversing an Array: Write a function that takes an array of integers and reverses it in place. For example, if the input is arr[] = {1, 2, 3}, the output should be arr[] = {3, 2, 1}.