What are Arrays and Vectors?
In this sub-module, we will introduce you to two fundamental data structures: arrays and vectors. These data structures allow us to store and manipulate collections of data in a efficient manner.
Arrays
An array is a collection of elements of the same type stored in contiguous memory locations. Each element in an array is identified by its index or subscript. In other words, an array is a fixed-size, homogeneous dataset that can be accessed using an index.
Example: Student Grades
Suppose you are a teacher and want to store the grades of 5 students in your class. You can use an array to store this data:
| Index | Grade |
| --- | --- |
| 0 | 90 |
| 1 | 85 |
| 2 | 95 |
| 3 | 80 |
| 4 | 92 |
In this example, the array has a fixed size of 5 elements, and each element is a grade stored as an integer value. The index or subscript identifies each element in the array.
Vectors
A vector is a one-dimensional array that represents a mathematical vector. In programming, vectors are used to store collections of numerical values that can be manipulated using various operations such as addition, subtraction, multiplication, and division.
Example: 3D Coordinate System
Suppose you want to represent the coordinates of a point in a 3D space. You can use a vector to store this data:
| Index | Value |
| --- | --- |
| 0 | x |
| 1 | y |
| 2 | z |
In this example, the vector has a size of 3 elements, and each element represents a coordinate in the 3D space.
Key Characteristics of Arrays and Vectors
Here are some key characteristics that distinguish arrays and vectors from other data structures:
- Fixed Size: Both arrays and vectors have a fixed size, which means that once they are created, their size cannot be changed.
- Homogeneous Elements: Both arrays and vectors store elements of the same type. For example, an array of integers or a vector of floating-point numbers.
- Indexed Access: Both arrays and vectors allow for indexed access to their elements using subscripts or indices.
Applications of Arrays and Vectors
Arrays and vectors have numerous applications in various fields:
- Scientific Computing: Vectors are used to represent mathematical vectors, which are essential in scientific computing applications such as physics, engineering, and computer graphics.
- Data Analysis: Arrays are used to store and manipulate large datasets in data analysis applications such as statistics, machine learning, and data visualization.
- Game Development: Arrays and vectors are used to create game logic, handle user input, and manage game states.
Theoretical Concepts
Here are some theoretical concepts that are important for understanding arrays and vectors:
- Memory Allocation: Both arrays and vectors require memory allocation, which is the process of reserving a block of memory for storing data.
- Memory Management: Proper memory management is crucial when working with arrays and vectors to avoid memory leaks and other issues.
- Cache Efficiency: Arrays and vectors can be designed to take advantage of cache hierarchies in computer systems, which improves performance.
In this sub-module, we have introduced you to the basics of arrays and vectors. We hope that this content has provided a solid foundation for understanding these fundamental data structures. In the next sub-module, we will explore other important data structures such as linked lists and stacks.