Data Structure: Array

  
Embedded Systems
Embedded Systems: Fundamentals
Embedded Systems: Features
Embedded Systems: Design Metrics
Embedded Systems: History
Embedded Systems: Classification
Embedded Systems: Application Areas
 
 
Basic Electronics: Tutorial
Voltage, Current, and Resistance – Part 1
Voltage, Current, and Resistance – Part 2
Voltage, Current, and Resistance – Part 3
Voltage, Current, and Resistance – Part 4
Inductors And Its Applications
  
VLSI Design: Tutorial
Introduction to VLSI Design
Timeline of VLSI Evolution: Major Milestones
Design Methodology of VLSI Design
VLSI Design Flow
 
Microprocessors & Microcontrollers: Tutorial
Microprocessors: Fundamentals
Microprocessors Vs Microcontrollers
Origin & Timeline Of Microprocessor Evolution
Memory Unit of Microprocessor
Features of 8085 Microprocessor
8085 Microprocessor Pin Diagram
8085 Microprocessor Architecture
8086 Microprocessor Features
 
Data Structures: Tutorial
Data Structures: Introduction
Data Structure: Characteristics & Classifications
Data Structure: Array

What is an Array?

  • Definition: An array is a data structure that can hold multiple values, usually of the same data type, in a single variable.
  • Purpose: Arrays allow you to organize and manage data efficiently. Instead of creating a separate variable for each value, you can store all the values in a single array.

Key Features of Arrays

1. Fixed Size:
  • When we create an array, we need to define its size, meaning how many elements it can hold. This size is usually fixed and cannot be changed later.
2. Indexed Elements:
  • Each item in an array has a specific position, called an index. Indexing usually starts at 0, meaning the first element is at index 0, the second at index 1, and so on.
3. Homogeneous Data:
  • Arrays store elements of the same type. For example, an array of integers can only hold integers.
4. Contiguous Memory Allocation:
  • Elements in an array are stored next to each other in memory (contiguous memory locations). This makes accessing elements by their index very efficient.

1-Dimensional (1-D) Array

Imagine we have a row of lockers, and each locker can hold a number:

ArrayFig. 1: Representation of 1-Dimensional (1-D) Array

  • Locker Number: Represents the index of the array.
  • Number Inside: Represents the value stored at that index.

Real-life Example of 1-D Array

Example:

  • A piano keyboard is essentially a one-dimensional array where each key represents an element in the array. The keys are arranged in a linear sequence, and each key corresponds to a specific musical note (e.g., A, B, C, D, etc.).
  • The index of the array can be thought of as the position of the key on the keyboard, and the value stored at each index is the pitch or frequency of the note produced when the key is pressed.
  • For example:

Index 0: A0 (lowest note)

Index 1: B0

Index 2: C1

Index 87: C8 (highest note)

When you play a melody, you’re essentially traversing this 1-D array, accessing specific indices (keys) in a sequence to create music. The simplicity of the 1-D array structure beautifully mirrors the linear arrangement of the piano keys, yet it gives rise to the complexity and richness of musical expression.

Array

  • A 1-D array is a linear data structure that stores elements of the same type in a contiguous block of memory. Each element in the array can be accessed using an index, which represents its position in the sequence.
  • Let’s say we have an array of integers representing the number of apples sold each day of the week:

Array

Index: The position of each element in the array (starting from 0).

          Value: The data stored at each index.

  • Memory Representation: In memory, a 1-D array is stored as a contiguous block. For example, if the array is [10, 15, 12, 20, 18, 14, 16], it might look like this in memory:

Array

Each integer takes 4 bytes of memory (for example).

 The memory address increases by 4 for each subsequent element.

  • Mathematical Representation: A 1-D array can be represented mathematically as:

A = [a0, a1, a2, …, an−1]

Where,

A is the array.

a0, a1, …, an−1 are the elements of the array.

n is the size of the array.

C program that demonstrates the concept of a 1-D array

Following program will:

  1. Declare and initialize a 1-D array.
  2. Access and print elements of the array.
  3. Traverse the array using a loop.
  4. Modify an element in the array.
  5. Print the updated array.

C Program: 1-D Array Explanation

Array

 
Explanation of the Code

1) Array Declaration and Initialization:

int numbers[5] = {10, 20, 30, 40, 50};
  • numbersis an array of integers with a size of 5.
  • It is initialized with the values {10, 20, 30, 40, 50}.

2) Accessing and Printing Array Elements:

for (int i = 0; i < 5; i++) {
   printf("numbers[%d] = %d\n", i, numbers[i]);
}
  • forloop is used to traverse the array.
  • numbers[i]accesses the element at index i.

3) Modifying an Element:

numbers[2] = 100;
  • The value at index 2(third element) is changed from 30 to 100.

4) Printing the Updated Array:

  • The same loop is used to print the array again, showing the updated value

Output of the Program:

Elements of the array:
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
Updated elements of the array:
numbers[0] = 10
numbers[1] = 20
numbers[2] = 100
numbers[3] = 40
numbers[4] = 50

Notes:

  1. Arrays are zero-indexed: The first element is at index 0.
  2. Elements can be accessed and modified using their index.
  3. Loops are commonly used to traverse arrays.
  4. Arrays are stored in contiguous memory locations, making them efficient for random access.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top