Arrays
Types of Arrays:
One-dimensional Array: A one-dimensional array, also known as a linear array, is a collection of elements stored in a single row or column. Each element is accessed using a single index value.
Multi-dimensional Array: A multi-dimensional array is an array of arrays. It can be visualized as a table with rows and columns. The elements are accessed using multiple indices corresponding to each dimension.
Two-dimensional Array: A two-dimensional array has two indices to access its elements, typically representing rows and columns.
Multi-dimensional Arrays: Arrays with more than two dimensions are also possible. For example, a three-dimensional array can be thought of as a cube, with elements accessed using three indices.
Code in C:
1. One-dimensional Array:
#include <stdio.h>
int main()
{
// Declaring an array of integers
int numbers[5] = {10, 20, 30, 40, 50};
// Accessing elements of the array
printf("Elements of the array:\n");
for (int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
2. Two-dimensional Array:
c#include <stdio.h>
int main()
{
// Declaring a 2D array of integers
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing elements of the 2D array
printf("Elements of the 2D array:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Notes:
Arrays are fundamental data structures in programming languages like C. They provide a convenient way to store and manipulate collections of data elements. In C, arrays are of fixed size, meaning once declared, their size cannot be changed during runtime. However, they offer efficient memory access and are widely used in various applications ranging from simple data storage to complex algorithms like sorting and searching. Understanding arrays is essential for mastering data structures and algorithms in C programming.
Comments
Post a Comment