Arrays Tutorials
Programming
Arrays in C
An array is a linear data structure used to store multiple values of the same data type in a single variable.
Instead of creating separate variables for similar data, we can store all the values inside an array.
Arrays store elements in contiguous memory locations, which allows fast and efficient access to elements using their index.
Why Do We Need Arrays?
Imagine you want to store marks of 100 students.
Without arrays:
int mark1;
int mark2;
int mark3;
...
int mark100;Managing so many variables becomes difficult.
Using an array:
int marks[100];All values can be stored in a single variable.
Example of Array
#include <stdio.h>
int main()
{
int arr[] = {2, 4, 8, 12, 16, 18};
int n = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}Output
2 4 8 12 16 18Characteristics of Arrays
Arrays store elements of the same data type.
Elements are stored in contiguous memory locations.
Array size is fixed after declaration.
Elements can be accessed directly using an index.
Indexing starts from 0.
Creating an Array
Creating an array involves two steps:
Array Declaration
Array Initialization
Array Declaration
Array declaration specifies:
Data type
Array name
Number of elements
Syntax
data_type array_name[size];Example
int marks[5];This creates an array named marks that can store 5 integer values.
Array Initialization
Initialization means assigning values to array elements.
Example
int marks[5] = {10, 20, 30, 40, 50};Declaration and Initialization Together
int marks[] = {10, 20, 30, 40, 50};In this case, the compiler automatically determines the size of the array.
Partial Initialization
We can initialize only some elements.
int marks[5] = {10, 20};Remaining elements automatically become 0.
The array will contain:
10 20 0 0 0Accessing Array Elements
Array elements are accessed using their index number.
Syntax
array_name[index]Example
#include <stdio.h>
int main()
{
int arr[5] = {2, 4, 8, 12, 16};
printf("%d\n", arr[2]);
printf("%d\n", arr[4]);
printf("%d\n", arr[0]);
return 0;
}Output
8
16
2Important Note
Array indexing starts from 0.
For an array of size 5:
Index : 0 1 2 3 4
Value : 2 4 8 12 16First element → index 0
Last element → index 4
Updating Array Elements
Array elements can be modified using the assignment operator.
Example
#include <stdio.h>
int main()
{
int arr[5] = {2, 4, 8, 12, 16};
arr[0] = 1;
printf("%d", arr[0]);
return 0;
}Output
1Array Traversal
Array traversal means visiting every element of an array one by one.
Loops are commonly used for traversal.
Traversing an Array
#include <stdio.h>
int main()
{
int arr[5] = {2, 4, 8, 12, 16};
for(int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}Output
2 4 8 12 16Traversing an Array in Reverse Order
#include <stdio.h>
int main()
{
int arr[5] = {2, 4, 8, 12, 16};
for(int i = 4; i >= 0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}Output
16 12 8 4 2Size of an Array
The size of an array represents the total number of elements it can store.
C does not automatically provide the length of an array, but we can calculate it using the sizeof() operator.
Formula
sizeof(array) / sizeof(array[0])Example
#include <stdio.h>
int main()
{
int arr[5] = {2, 4, 8, 12, 16};
int size = sizeof(arr) / sizeof(arr[0]);
printf("%d", size);
return 0;
}Output
5How It Works
Suppose:
int arr[5];Each integer occupies 4 bytes.
Therefore:
sizeof(arr) = 20 bytes
sizeof(arr[0]) = 4 bytesSo:
20 / 4 = 5Hence, the array contains 5 elements.
Advantages of Arrays
Store multiple values using a single variable.
Easy to access elements using index numbers.
Reduce the number of variables in a program.
Efficient memory usage.
Easy traversal using loops.
Limitations of Arrays
Array size is fixed after declaration.
Only elements of the same data type can be stored.
Inserting and deleting elements can be costly.
Memory may be wasted if the array size is larger than required.
Arrays and Functions
Arrays can be passed to functions as arguments.
Example
void display(int arr[], int size)
{
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
}This allows functions to process multiple values efficiently.
Multidimensional Arrays
C also supports arrays with more than one dimension.
Example:
int matrix[3][3];Multidimensional arrays are commonly used for:
Matrices
Tables
Grids
Game Boards
Summary
An array is a collection of elements of the same data type.
Arrays store data in contiguous memory locations.
Array indexing starts from 0.
Arrays support fast random access using indexes.
Elements can be accessed and updated using square brackets
[].Loops are commonly used for array traversal.
The size of an array can be calculated using the
sizeof()operator.Arrays help manage large amounts of similar data efficiently.
Practice Array Problems
1. Find Maximum Value in an Array
#include <stdio.h>
int main()
{
int arr[] = {10, 25, 8, 45, 30};
int max = arr[0];
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 1; i < size; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
}
printf("Maximum Element = %d", max);
return 0;
}Output
Maximum Element = 452. Calculate Sum of Array Elements
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int sum = 0;
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < size; i++)
{
sum += arr[i];
}
printf("Sum = %d", sum);
return 0;
}Output
Sum = 1503. Reverse Array in C
Program
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Reversed Array: ");
for(int i = size - 1; i >= 0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}Output
Reversed Array: 50 40 30 20 104. Insert an Element in an Array
Program
#include <stdio.h>
int main()
{
int arr[10] = {10, 20, 30, 40, 50};
int size = 5;
int pos = 2;
int value = 25;
for(int i = size; i > pos; i--)
{
arr[i] = arr[i - 1];
}
arr[pos] = value;
size++;
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return 0;
}Output
10 20 25 30 40 505. Delete an Element in an Array
Program
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int size = 5;
int pos = 2;
for(int i = pos; i < size - 1; i++)
{
arr[i] = arr[i + 1];
}
size--;
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return 0;
}Output
10 20 40 506. Array Rotation (Left Rotation by One Position)
Program
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int first = arr[0];
for(int i = 0; i < size - 1; i++)
{
arr[i] = arr[i + 1];
}
arr[size - 1] = first;
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return 0;
}Output
20 30 40 50 107. Return an Array from a Function
Program
#include <stdio.h>
int* getArray()
{
static int arr[] = {10, 20, 30, 40, 50};
return arr;
}
int main()
{
int *ptr = getArray();
for(int i = 0; i < 5; i++)
{
printf("%d ", ptr[i]);
}
return 0;
}Output
10 20 30 40 508. Sort an Array in Ascending Order
Program
#include <stdio.h>
int main()
{
int arr[] = {50, 20, 40, 10, 30};
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < size - 1; i++)
{
for(int j = i + 1; j < size; j++)
{
if(arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Sorted Array: ");
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return 0;
}Output
Sorted Array: 10 20 30 40 509. Find the Pair with a Given Sum in Array
Program
#include <stdio.h>
int main()
{
int arr[] = {2, 7, 11, 15, 5};
int target = 9;
int size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < size; i++)
{
for(int j = i + 1; j < size; j++)
{
if(arr[i] + arr[j] == target)
{
printf("Pair Found: %d + %d = %d",
arr[i], arr[j], target);
}
}
}
return 0;
}Output
Pair Found: 2 + 7 = 9