Loading content...
Loading content...
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.
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.
#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;
}2 4 8 12 16 18Arrays 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 involves two steps:
Array Declaration
Array Initialization
Array declaration specifies:
Data type
Array name
Number of elements
data_type array_name[size];int marks[5];This creates an array named marks that can store 5 integer values.
Initialization means assigning values to array elements.
int marks[5] = {10, 20, 30, 40, 50};int marks[] = {10, 20, 30, 40, 50};In this case, the compiler automatically determines the size of the array.
We can initialize only some elements.
int marks[5] = {10, 20};Remaining elements automatically become 0.
The array will contain:
10 20 0 0 0Array elements are accessed using their index number.
array_name[index]#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;
}8
16
2Array 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
Array elements can be modified using the assignment operator.
#include <stdio.h>
int main()
{
int arr[5] = {2, 4, 8, 12, 16};
arr[0] = 1;
printf("%d", arr[0]);
return 0;
}1Array traversal means visiting every element of an array one by one.
Loops are commonly used for traversal.
#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;
}2 4 8 12 16#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;
}16 12 8 4 2The 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.
sizeof(array) / sizeof(array[0])#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;
}5Suppose:
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.
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.
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 can be passed to functions as arguments.
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.
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
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
#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;
}Maximum Element = 45#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;
}Sum = 150#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;
}Reversed Array: 50 40 30 20 10#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;
}10 20 25 30 40 50#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;
}10 20 40 50#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;
}20 30 40 50 10#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;
}10 20 30 40 50#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;
}Sorted Array: 10 20 30 40 50#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;
}Pair Found: 2 + 7 = 9