Array
1. Definition of Array
An array is a data structure that stores a collection of values of the same data type under one name. Each value in an array is accessed by an index. Index always starts from 0.
Declaration of Array
General Syntax:
data_type array_name[size];
Examples:
int marks[5]; // An array to store 5 integers
float avg[10]; // An array to store 10 floating-point numbers
char name[20]; // An array to store 20 characters
Initialization of Array
When values are given to an array at the time of declaration, it is called initialization.
int num[5] = {10, 20, 30, 40, 50};
// num[0]=10, num[1]=20, … num[4]=50
If size is not specified, the compiler automatically calculates it:
int num[] = {5, 10, 15};
// Size becomes 3 automatically
Types of Array
(A) One-Dimensional Array
A one-dimensional array is a linear collection of elements of the same data type stored in contiguous memory locations. It is also called a single-dimensional array or a linear array.
Declaration of One-Dimensional Array
data_type array_name[size];
Example:
int marks[5]; // Creates an integer array with 5 elements
Initialization of One-Dimensional Array
int marks[5] = {90, 85, 76, 88, 95};
int marks[5] = {90, 85}; // remaining elements become 0
int marks[] = {90, 85, 76, 88, 95}; // size auto becomes 5
Accessing Elements
marks[0] = 90; // first element
marks[1] = 85; // second element
Example (printing all elements):
for(int i = 0; i < 5; i++) {
printf("%d ", marks[i]);
}
Advantages of One-Dimensional Array
- Efficient Storage – Stores multiple values of the same type under one name.
- Easy Access – Any element can be accessed using its index.
- Code Simplification – Loops can process all elements easily.
- Memory Management – Stored in contiguous memory locations.
- Reusability – Arrays can be used with functions for modularity.
Uses of One-Dimensional Array
- Storing student marks or scores.
- Storing daily data like temperatures, monthly sales, etc.
- Data processing (searching, sorting, modifying).
- Mathematical operations (vectors, sequences).
- Real-life applications (shop prices, roll numbers, employee IDs).
(B) Two-Dimensional Array
A two-dimensional array is an array of arrays where elements are arranged in rows and columns. It is also called a matrix array or table.
Example:
int a[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// a[0][0] = 1
// a[0][1] = 2
// a[1][2] = 6
Declaration of Two-Dimensional Array
data_type array_name[rows][columns];
Example:
int marks[3][4]; // 3 rows and 4 columns (12 elements)
Initialization of Two-Dimensional Array
int marks[2][3] = {
{90, 85, 76},
{88, 95, 80}
};
int marks[2][3] = {90, 85, 76, 88, 95, 80};
Accessing Elements
marks[0][0] = 90; // First row, first column
marks[1][2] = 80; // Second row, third column
Example (printing all elements):
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", marks[i][j]);
}
printf("\n");
}
Advantages of Two-Dimensional Array
- Tabular Data Representation – Stores data in rows and columns.
- Easy Access – Elements can be accessed using row and column indices.
- Simplifies Code – One 2D array can replace multiple 1D arrays.
- Efficient Memory Usage – Stored in contiguous memory locations.
- Useful in Complex Applications – For matrices, graphs, tabular records.
Uses of Two-Dimensional Array
- Mathematics (Matrices) – Addition, subtraction, multiplication of matrices.
- Storing Tabular Data – Example: marks of students in different subjects.
- Timetables / Schedules – School and college timetables.
- Game Development – Chess, Sudoku, Tic-tac-toe boards.
- Image Processing – Images stored as 2D arrays of pixels.
- Maps and Grids – Used in navigation, pathfinding, and scientific data.
No comments:
Post a Comment