C Programming - Chapter 3 - Array
C Programming - Chapter 3 - Array
PROGRAMMING 2
C/C++ LANGUAGE
int main()
{
return 0;
}
3 . 2 a r r a y I N I T I A L I Z AT I O N
• Array Initialization:
• Initialization in C is the process to assign some initial value to the variable.
• To assign value for array, by enclosing those initial values in braces {}.
• For example:
int A[9] = { 40,55,63,17,22,68,89,97,89};
• The number of values between braces {} shall not be greater than the number of elements in the
array
3 . 2 a r r a y i n I T I A L I Z AT I O N
Array Initialization with Declaration:
• In this method, we initialize the array along with its declaration. We use an initializer list to initialize
multiple elements of the array. An initializer list is the list of values enclosed within braces { } separated b
a comma.
■ data_type array_name [size] = {value1, value2, ... valueN};
• This initializes an array of size 5, with the elements {2, 4, 8, 12, 16} in order:
■ int A[5] = {2,4,8,12,16};
• We don’t need to initialize all the elements.The following code is also valid:
■ int A[5] = {1, 2, 3};
3 . 2 A R R AY I N I T I A L I Z AT I O N
Array Initialization with Declaration without Size
• If we initialize an array using an initializer list, we can skip declaring the size of the array as the compiler
can automatically deduce the size of the array in these cases. The size of the array in these cases is equal to
the number of elements present in the initializer list as the compiler can automatically deduce the size of
the array:
■ data_type array_name [] = {value1, value2, ... valueN};
• This initializes an array of size 5, with the elements {2, 4, 8, 12, 16} in order:
■ int A[] = {2,4,8,12,16};
• The size of the above arrays is 5 which is automatically deduced by the compiler
3 . 2 A R R AY I N I T I A L I Z AT I O N
• Multidimensional Array Initialization:
• You can initialize a multidimensional array using any of the following techniques:
• Listing the values of all elements you want to initialize, in the order that the compiler assigns the
values. This form of a multidimensional array initialization looks like a one-dimensional array
initialization:
• int arr[3][2] = {1, 2, 3, 4, 5, 6};
• Using braces to group the values of the elements you want initialized. You can put braces around each
element, or around any nesting level of elements:
• int arr[3][2] = {{1, 2}, {3, 4}, {5,6}};
3 . 3 A C C E S S A R R AY E L E M E N T S
Array Elements:
• In C, an array is a collection of elements of the same data type, stored in contiguous memory locations.
Each element in an array is accessed by its index. This indexing represents the position in the array, which
starts from zero for the first element.
• To access elements of the array, you use the array name followed by the index in square brackets:
■ array_name [index];
• For example:
■ int A[5] = {1,2,3,4,5};
■ int x = A[0]; // A[0] is the first elements of array A
3 . 3 A C C E S S A R R AY E L E M E N T S
Array Elements:
• We can update the value of an element at the given index i in a similar way to accessing an element by
using the array subscript operator [ ] and assignment operator = :
■ int A[5];
■ A[0] = 5;
• Notice: that the first one is A[0], the second one is A[1], and therefore. By this same reason, its last
element is A[4]. Therefore, with an array has 5 elements, if we write A[5], we would be accessing the
sixth element of A, and therefore actually exceeding the size of the array. The element is not
available.
3 . 3 A C C E S S A R R AY E L E M E N T S
Multidimensional Array Elements:
• To access the element of multi-dimensional array, we need multi indices. For example, to access the element
of bi-dimensional array:
■ int A[3][2];
■ A[0][1] = 5;
• Notice : [0] means the element located in first row, [1] means the element located in second column.
3 . 3 A C C E S S A R R AY E L E M E N T S
C Array Traversal:
• Traversal is the process in which we visit every element of the data structure. For C array traversal, we use
loops to iterate through each element of the array.
• Array Traversal using for Loop:
for (int i = 0; i < N; i++)
{
array_name[i];
}
• Similarly, array elements can also be displayed in the output using the printf() method. The index is
specified indicating the position of the element to be printed.
■ printf("%d", my_array[0]);
• This statement prints the element stored at 1st position or 0th index
3 . 3 A C C E S S A R R AY E L E M E N T S
// C Program to perform input and output on array
#include <stdio.h>
int main()
{
// declaring an integer array
int arr[5];
// taking input to array elements one by one
for (int i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
// printing array elements
printf("Array Elements: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
3 . 3 A C C E S S A R R AY E L E M E N T S
// C Program to perform input and output on 2D array
#include <stdio.h>
int main()
{
int arr[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("Array Elements: ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
return 0;
}
3 . 4 A R R AY E L E M E N T S S E A R C H I N G
• To search array elements:
• To search for an element in an array in C, you can use a loop (such as a for or while loop) to iterate
through the array elements and compare each element with the target value you want to find:
o We use a for loop to iterate through each element of the array.
o Inside the loop, we compare each element with the target value.
o If a match is found, we print the index of the element and set the found flag to 1. We then break out
of the loop to stop searching.
o After the loop, we check the found flag. If it's still 0, indicating that the element was not found in the
array.
3 . 4 A R R AY E L E M E N T S S E A R C H I N G
• Array search algorithms:
• There are several algorithms for searching elements in an array in computer science, each with its own
characteristics and use cases. Here are some commonly used searching algorithms:
• Linear Search.
• Binary Search.
• Jump Search.
• Exponential Search.
• Interpolation Search
3 . 4 A R R AY E L E M E N T S S E A R C H I N G
• Linear Search:
• Also known as sequential search.
• It iterates through each element of the array one by one until the target element is found.
• It is suitable for unordered arrays and small datasets.
• Time complexity: O(n), where n is the number of elements in the array.
3 . 4 A R R AY E L E M E N T S S E A R C H I N G
• Binary Search:
• Requires the array to be sorted in ascending or descending order.
• It repeatedly divides the search interval in half until the target element is found.
• It is very efficient for large sorted datasets.
• Time complexity: O(log n), where n is the number of elements in the array.
3 . 4 A R R AY E L E M E N T S S E A R C H I N G
• Jump Search:
• Requires the array to be sorted.
• It jumps ahead by fixed steps to reduce the number of comparisons.
• Combines elements of linear and binary search.
• Time complexity: O(√n), where n is the number of elements in the array.
3 . 4 A R R AY E L E M E N T S S E A R C H I N G
• Exponential Search:
• Requires the array to be sorted.
• It first finds a range where the target element may exist using exponential increments and then performs
a binary search within that range. It is useful for unbounded arrays.
• Time complexity: O(log n), but with a larger constant factor compared to binary search
3 . 5 A R R AY S O RT I N G
• Array sorting in C refers to the process of arranging the elements of an array in a particular order, such as
ascending or descending. Sorting is a fundamental operation in computer science and is used to organize
data for efficient retrieval and manipulation.
• There are various sorting algorithms in C that can be used to sort arrays, including:
• Quick Sort: A fast and widely used sorting algorithm, time complexity of O(n log n).
• Merge Sort: Another divide-and-conquer algorithm, time complexity of O(n log n).
• Bubble Sort: A simple sorting algorithm, time complexity of O(n^2) in the worst case.
• Insertion Sort: has a time complexity of O(n^2) in the worst case.
• Selection Sort: has a time complexity of O(n^2) in the worst case.
• Heap Sort: A comparison-based sorting algorithm, time complexity of O(n log n).
3 . 5 A R R AY S O RT I N G
• Bubble sort:
• Bubble sort is a simple comparison-based sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong order.
• The pass through the list is repeated until no swaps are needed, indicating that the list is sorted
3 . 5 A R R AY S O RT I N G
• Selection sort:
• Selection sort is a simple comparison-based sorting algorithm that divides the array into two parts: the
sorted part and the unsorted part.
• The algorithm repeatedly selects the smallest (or largest, depending on the sorting order) element from
the unsorted part and moves it to the end of the sorted part.
3 . 5 A R R AY S O RT I N G
3 . 5 A R R AY S O RT I N G
• Insertion sort:
• Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time.
• It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge
sort.
• However, it has some advantages in simplicity and is often used in practice for small lists or as a building
block in more complex sorting algorithms.
3 . 5 A R R AY S O RT I N G
3 . 5 A R R AY S O RT I N G
• Quick sort:
• Quick sort is a popular and efficient sorting algorithm that uses a divide-and-conquer strategy to sort
an array.
• It works by selecting a "pivot" element from the array and partitioning the other elements into two sub-
arrays according to whether they are less than or greater than the pivot.
• The sub-arrays are then sorted recursively.
3 . 5 A R R AY S O RT I N G
3 . 5 A R R AY S O RT I N G