What is an Array?
What is an Array?
Learn how arrays help programmers store multiple values under one name and access each value using its position or index.
What is an Array?
An array is a data structure used to store multiple values under a single variable name.
In simple words, an array is like a container that can hold many related values together. Instead of creating separate variables for each value, we can store all values in one array.
For example, if we want to store marks of five students, we do not need five separate variables. We can store all marks inside one array.
marks = [85, 90, 78, 88, 92]
Here, marks is an array that stores five values.
Easy Real-Life Example
Array as an Egg Tray
Imagine an egg tray. It has multiple slots, and each slot can hold one egg. Each slot has a position, so you can easily identify where an egg is placed.
An array works in a similar way. It has multiple positions, and each position stores one value.
Index: 0 1 2 3 4
Value: 85 90 78 88 92
In most programming languages, array indexing starts from 0. That means the first element is at index 0, not index 1.
Why are Arrays Needed?
Arrays are needed because storing many related values using separate variables becomes difficult and messy.
Without Array
studentMark1 = 85
studentMark2 = 90
studentMark3 = 78
studentMark4 = 88
studentMark5 = 92
This approach may work for five values, but it becomes difficult when there are hundreds or thousands of values.
With Array
studentMarks = [85, 90, 78, 88, 92]
This is cleaner, easier to manage, and easier to process using loops.
Importance of Arrays
- Arrays store multiple values under one name.
- They reduce the need for many separate variables.
- They make data easier to organize.
- They allow values to be accessed using indexes.
- They work well with loops.
- They help store lists of marks, names, prices, products, scores, and records.
- They are used as a foundation for many advanced data structures.
- They improve code readability and maintainability.
Main Parts of an Array
| Part | Meaning | Example |
|---|---|---|
| Array Name | The name used to identify the array. | marks |
| Element | Each value stored inside the array. | 85, 90, 78 |
| Index | The position number of an element. | 0, 1, 2 |
| Length / Size | The total number of elements in the array. | 5 |
| Data Type | The type of values stored in the array. | Integer, Text, Decimal |
General Syntax of an Array
The exact syntax of arrays differs from language to language, but the general idea is:
arrayName = [value1, value2, value3, value4]
Example:
studentNames = ["Aman", "Riya", "Sohan", "Meera"]
Here, studentNames is an array that stores four text values.
What is an Array Index?
An index is the position number of an element inside an array.
Most programming languages start array indexing from 0. So the first element is stored at index 0, the second element at index 1, and so on.
fruits = ["Apple", "Banana", "Mango", "Orange"]
Index 0 → Apple
Index 1 → Banana
Index 2 → Mango
Index 3 → Orange
length - 1.
If an array has 4 elements, the indexes are 0, 1, 2, and 3.
Accessing Array Elements
To access an element, we use the array name and the index of the element.
fruits = ["Apple", "Banana", "Mango", "Orange"]
DISPLAY fruits[0]
DISPLAY fruits[2]
Expected Output
Apple
Mango
fruits[0] gives the first element, and fruits[2] gives the third element.
Updating Array Elements
We can change an existing value in an array by using its index.
marks = [85, 90, 78, 88, 92]
SET marks[2] = 80
DISPLAY marks
Updated Array
[85, 90, 80, 88, 92]
The value at index 2 changed from 78 to 80.
Traversing an Array
Traversing means visiting each element of an array one by one.
Loops are commonly used to traverse arrays.
marks = [85, 90, 78, 88, 92]
FOR index FROM 0 TO length(marks) - 1
DISPLAY marks[index]
END FOR
Expected Output
85
90
78
88
92
This loop starts from index 0 and visits every value in the array.
Example 1: Store Student Marks
/*
This program stores and displays student marks using an array.
*/
ENTRY POINT
DECLARE marks AS ARRAY = [85, 90, 78, 88, 92]
FOR index FROM 0 TO length(marks) - 1
DISPLAY "Mark: " + marks[index]
END FOR
END ENTRY POINT
Example 2: Find Total Marks
/*
This program calculates total marks from an array.
*/
ENTRY POINT
DECLARE marks AS ARRAY = [85, 90, 78, 88, 92]
DECLARE total AS INTEGER = 0
FOR index FROM 0 TO length(marks) - 1
SET total = total + marks[index]
END FOR
DISPLAY "Total Marks: " + total
END ENTRY POINT
Expected Output
Total Marks: 433
Example 3: Find Highest Marks
/*
This program finds the highest mark in an array.
*/
ENTRY POINT
DECLARE marks AS ARRAY = [85, 90, 78, 88, 92]
DECLARE highest AS INTEGER = marks[0]
FOR index FROM 1 TO length(marks) - 1
IF marks[index] > highest THEN
SET highest = marks[index]
END IF
END FOR
DISPLAY "Highest Mark: " + highest
END ENTRY POINT
Expected Output
Highest Mark: 92
Example 4: Search an Element in an Array
/*
This program searches for a name inside an array.
*/
ENTRY POINT
DECLARE names AS ARRAY = ["Aman", "Riya", "Sohan", "Meera"]
DECLARE targetName AS TEXT = "Sohan"
DECLARE isFound AS BOOLEAN = false
FOR index FROM 0 TO length(names) - 1
IF names[index] == targetName THEN
SET isFound = true
BREAK
END IF
END FOR
IF isFound == true THEN
DISPLAY targetName + " found in the array"
ELSE
DISPLAY targetName + " not found in the array"
END IF
END ENTRY POINT
Common Array Operations
| Operation | Meaning | Example Idea |
|---|---|---|
| Access | Read an element using its index. | marks[0] |
| Update | Change an existing element. | marks[2] = 80 |
| Traverse | Visit every element one by one. | Use a loop. |
| Search | Find whether a value exists. | Check each element. |
| Insert | Add a new element. | Add a new mark. |
| Delete | Remove an element. | Remove a value by index. |
| Sort | Arrange elements in order. | Ascending or descending order. |
Types of Arrays
Arrays can be classified in different ways.
1. One-Dimensional Array
A one-dimensional array stores values in a single line or list.
marks = [85, 90, 78, 88, 92]
2. Two-Dimensional Array
A two-dimensional array stores data in rows and columns, like a table.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
A two-dimensional array is useful for tables, grids, matrices, game boards, and seat arrangements.
3. Multi-Dimensional Array
A multi-dimensional array has more than two dimensions. It is used when data has multiple levels.
Array vs Normal Variables
| Feature | Normal Variable | Array |
|---|---|---|
| Storage | Stores one value. | Stores multiple values. |
| Access | Accessed by variable name. | Accessed by array name and index. |
| Best For | Single value. | List of related values. |
| Example | mark = 85 |
marks = [85, 90, 78] |
Array vs List
In some programming languages, arrays and lists are different. In some languages, list-like structures are commonly used instead of traditional arrays.
| Feature | Array | List |
|---|---|---|
| General Meaning | A structured collection of elements. | A flexible collection of elements. |
| Size | Often fixed in many languages. | Often dynamic and resizable. |
| Access | Usually fast by index. | May support more flexible operations. |
| Use Case | When storing indexed values. | When frequent adding/removing is needed. |
Advantages of Arrays
Benefits
- Arrays store multiple values using one name.
- They make data easier to manage.
- They allow direct access using indexes.
- They work well with loops.
- They are useful for lists, tables, marks, names, and records.
- They help reduce repeated variables.
- They are a foundation for many data structures.
Limitations of Arrays
Limitations
- In many languages, traditional arrays have a fixed size.
- Insertion and deletion may require shifting elements.
- Accessing an invalid index can cause errors.
- In many languages, arrays usually store values of the same type.
- Managing large arrays requires careful logic.
Common Beginner Mistakes
Mistakes
- Thinking the first index is
1instead of0. - Trying to access an index that does not exist.
- Forgetting that the last index is usually
length - 1. - Using many variables instead of an array.
- Not using loops to process array elements.
- Changing the wrong index value.
- Confusing array length with last index.
- Not checking whether an array is empty before accessing values.
Better Habits
- Remember that indexing usually starts from
0. - Use
length - 1for the last index. - Use meaningful array names.
- Use loops for traversal.
- Validate indexes before accessing elements.
- Use arrays for related values.
- Test with empty, one-element, and multiple-element arrays.
- Trace array values using tables when debugging.
Best Practices for Arrays
Recommended Practices
- Use arrays when you need to store multiple related values.
- Give arrays meaningful plural names, such as
marks,names, orprices. - Use loops to process arrays.
- Use indexes carefully.
- Do not access indexes outside the valid range.
- Keep array data organized.
- Use comments only when the array purpose is not obvious.
- Choose the right structure if frequent insertion and deletion are needed.
Prerequisites Before Learning Arrays
To understand arrays properly, students should already know:
Basic Prerequisites
- Variables and constants.
- Data types.
- Input and output.
- Operators.
- Control flow.
- Loops and iteration.
- Functions and methods basics.
- Basic problem-solving logic.
Practice Activity: Identify Array Elements
Study the array below and answer the questions.
numbers = [10, 20, 30, 40, 50]
Questions
1. What is the value at index 0?
2. What is the value at index 3?
3. What is the length of the array?
4. What is the last index?
5. What is the value at the last index?
Sample Answers
1. Value at index 0 = 10
2. Value at index 3 = 40
3. Length = 5
4. Last index = 4
5. Value at last index = 50
Mini Quiz
What is an array?
An array is a data structure that stores multiple values under a single name.
What is an array element?
An array element is a value stored inside an array.
What is an array index?
An index is the position number used to access an element in an array.
What is usually the first index of an array?
In most programming languages, the first index is 0.
Why are arrays useful?
Arrays are useful because they allow multiple related values to be stored, accessed, and processed easily.
Interview Questions on Arrays
Define array in programming.
An array is a collection of elements stored under one name, where each element can be accessed using an index.
What is the difference between an element and an index?
An element is the value stored in an array, while an index is the position number used to access that value.
Why do arrays usually work well with loops?
Arrays work well with loops because loops can visit each index one by one and process every element.
What is a one-dimensional array?
A one-dimensional array stores values in a single line or list.
What is a two-dimensional array?
A two-dimensional array stores values in rows and columns, like a table.
Quick Summary
| Concept | Meaning |
|---|---|
| Array | A structure used to store multiple values under one name. |
| Element | A value stored inside an array. |
| Index | The position number of an element. |
| Length | The total number of elements in an array. |
| First Index | Usually 0 in most programming languages. |
| Last Index | Usually length - 1. |
| Traversal | Visiting each element one by one. |
| Best Use | Storing lists of related values such as marks, names, prices, and scores. |
Final Takeaway
An array is one of the most important data structures in programming. It allows us to store multiple related values under one name and access each value using an index. In the Programming Mastery Course, students should understand arrays as organized containers that make it easier to store, access, update, search, and process multiple values using loops and indexes.