DSA Array
Array Full Concept in Data Structures
An array is a basic and most-used way to organize data in memory. Think of it as a container that holds items of the same type, arranged in a line, and each item has a position (called index) starting from 0.
Definition
An array is a fixed-size group of values, where each value can be reached by its index number.
Why Arrays are Useful:
- You can keep a lot of similar items in one place (like marks of students).
- You can find any item instantly if you know its index.
- Simple to understand and use.
- Efficient in both reading and updating data.
How Memory Works in Arrays:
All items in an array sit next to each other in memory. If the first item is at location X, then the next will be at X + size_of_type, and so on. This structure allows instant access to any element by calculating its exact memory location using its index.
Key Characteristics
- An array stores only one kind of data, like all numbers or all characters.
- Once an array is made, its total number of slots can’t be changed.
- You can access elements using an index.
- Indexing starts from 0.
Real-Life Analogy:
Imagine a row of mailboxes. Each mailbox has a number (index), and you can go directly to mailbox 5 to check what’s inside. You don’t need to open all mailboxes — just go to the correct one by number.
Syntax
int numbers[5]; // creates an array with 5 integer spots
You can also initialize it like this:
int numbers[5] = {10, 20, 30, 40, 50};Now:
- numbers[0] is 10
- numbers[1] is 20
- numbers[2] is 30
Example
#include <stdio.h>
int main() {
int marks[4] = {85, 90, 78, 92};
// Print each mark using a loop
for(int i = 0; i < 4; i++) {
printf("Student %d got %d marks\n", i+1, marks[i]);
}
return 0;
} OutPut:
Student 1 got 85 marks Student 2 got 90 marks Student 3 got 78 marks Student 4 got 92 marks
Common Operations on Arrays
| Operation | Meaning |
|---|---|
| Access | Get the value using index like a[2] |
| Update | Change a value like a[1] = 50 |
| Traverse | Visit each element using a loop |
| Insert (Manual) | Place a new item by shifting others |
| Delete (Manual) | Remove an item by shifting elements |
Pros
- Easy to implement and use.
- Fast access to any element.
- Good for storing fixed-size data.
Cons
- Arrays have a constant length—you can’t add or remove spaces after they’re created.
- Inserting/deleting in middle is slow (needs shifting).
- Memory can be wasted if not fully used.
When to Use
- You know how many items you need.
- You need fast access to data using index.
- The items are of the same type.
Types of Arrays
- 1D Array: A simple list (like marks, prices).
- 2D Array: Like a table or matrix (rows and columns).
- Multidimensional Array: More than 2 levels, rarely used.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand DATA STRUCTURES ALGORITHMS Tutorial visually:
What You'll Learn:
- 📌 WHAT IS ARRAY? | Array Data Structures | DSA Course | GeeksforGeeks
- 📌 1.1 Arrays in Data Structure | Declaration, Initialization, Memory representation