0% found this document useful (0 votes)
2 views40 pages

Array

Chapter 3 discusses arrays in C programming, covering their definition, creation, accessing elements, traversal, size determination, updating, insertion, deletion, and merging of both unsorted and sorted arrays. It provides code examples and explanations for each concept, demonstrating how to manipulate arrays effectively. The chapter concludes with algorithms for insertion, deletion, and merging operations.

Uploaded by

nikhil082116
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views40 pages

Array

Chapter 3 discusses arrays in C programming, covering their definition, creation, accessing elements, traversal, size determination, updating, insertion, deletion, and merging of both unsorted and sorted arrays. It provides code examples and explanations for each concept, demonstrating how to manipulate arrays effectively. The chapter concludes with algorithms for insertion, deletion, and merging operations.

Uploaded by

nikhil082116
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter – 3 (Array)

1. Array
An array is a collection of similar data types stored in
contiguous memory locations. It is used to store multiple
values under a single variable name.
Array elements are accessed using indexes, and indexing
starts from 0.
Example –
#include <stdio.h>
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
for(int i = 0; i < 5; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Output: 10
20
30
40
50
Explanation –
• #include <stdio.h> is used to include the standard
input/output library.
• int main() is the main function where program execution
starts.
• int arr[5] = {10, 20, 30, 40, 50}; is used to declare and
initialize an array with 5 elements.
• for loop is used to access and print all array elements one
by one.
• printf("%d\n", arr[i]); prints each element of the array
on the screen.
2. Creating an Array –
Creating an Array is divided into two parts –
i. Array Declaration - Array declaration is the process
of creating an array by specifying its data type, array
name, and size. It reserves memory for storing
multiple elements of the same data type.
Syntax –
data_type array_name[size];
Example –
int arr[5];
Explaination - Here:
• int → data type of array
• arr → array name
• 5 → size of array
This declaration creates an array that can store 5 integer
values.
ii. Array initialization = Array initialization is the
process of assigning values to the array elements at
the time of array declaration. It helps to store multiple
values in an array using a single statement.
Syntax –
data_type array_name[size] = {value1, value2, value3};
Example –
int arr[5] = {10, 20, 30, 40, 50};
3. Accessing Array Elements –
Accessing array elements means retrieving or using the values
stored in an array by using index numbers. Array indexing
starts from 0.
Syntax - array_name[index];
Example - arr[2] This accesses the third element of the array.
Program
#include <stdio.h>
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
printf("%d\n", arr[0]);
printf("%d\n", arr[1]);
printf("%d\n", arr[2]);
return 0;
}
Output –
10
20
30
Explanation
1. int arr[5] = {10, 20, 30, 40, 50}; creates and initializes an
array.
2. arr[0] accesses the first element of the array.
3. arr[1] accesses the second element of the array.
4. arr[2] accesses the third element of the array.
5. printf() is used to print the accessed array elements on
the screen.
Conclusion -
Accessing array elements helps in retrieving data from an
array using index numbers efficiently.
4. C Array Traversal –
Array traversal is the process of accessing and visiting each
element of an array one by one. It is usually done using loops
such as the for loop.
Program for Array Traversal –
#include <stdio.h>
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
printf("Array Elements are:\n");
for(int i = 0; i < 5; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Output – Array Elements are:
10
20
30
40
50
Explanation -
1. int arr[5] = {10, 20, 30, 40, 50}; creates and initializes an
array.
2. The for loop is used to traverse the array elements one
by one.
3. i = 0 starts traversal from the first element.
4. i < 5 makes the loop run from index 0 to 4.
5. printf("%d\n", arr[i]); prints each array element on the
screen.
Conclusion -
Array traversal is used to access and display all elements of an
array efficiently using loops.
5. Size of Array –
The size of an array means the total amount of memory
occupied by the array in bytes. In C language, the sizeof()
operator is used to find the size of an array.
The size depends on:
• number of elements
• data type of array
Syntax –
sizeof(array_name)
Program to Find Size of Array –
#include <stdio.h>
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int size = sizeof(arr);
printf("Size of array = %d bytes", size);
return 0;
}
Output –
Size of array = 20 bytes
Explanation
1. int arr[5] = {10, 20, 30, 40, 50}; creates an integer array
with 5 elements.
2. sizeof(arr) is used to calculate the total size of the array
in bytes.
3. Each integer occupies 4 bytes in memory.
4. Total size calculation:
5 × 4 = 20 bytes
5. printf() prints the size of the array on the screen.
Conclusion -
The sizeof() operator is used to determine the total memory
occupied by an array in bytes.
6. Update in an Array –
Updating an array means changing or modifying the value of
an existing array element using its index number.
In C language, array elements can be updated by assigning a
new value to a specific index.
Program for Updating an Array Element –
#include <stdio.h>
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
arr[2] = 100;
printf("Updated Array Elements are:\n");
for(int i = 0; i < 5; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Output - Updated Array Elements are:
10
20
100
40
50
Explanation
1. int arr[5] = {10, 20, 30, 40, 50}; creates and initializes an
array.
2. arr[2] = 100; updates the value at index 2.
3. The old value 30 is replaced with 100.
4. The for loop is used to traverse and print all array
elements.
5. printf("%d\n", arr[i]); prints the updated array elements
on the screen.
Conclusion -
Updating an array is the process of changing the value of an
existing array element using its index number.
7. Insertion in Array –
Insertion in an array means adding a new element at a specific
position in the array. To insert an element, existing elements
are shifted to the right side to create space for the new
element.
Program for Insertion in Array -
#include <stdio.h>
int main()
{
int arr[6] = {10, 20, 30, 40, 50};
int i, pos = 2, value = 25, size = 5;
for(i = size; i > pos; i--)
{
arr[i] = arr[i - 1];
}
arr[pos] = value;
printf("Array after insertion:\n");
for(i = 0; i < size + 1; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Output – Array after insertion:
10
20
25
30
40
50
Explanation
1. int arr[6] = {10, 20, 30, 40, 50}; creates an array with
extra space for insertion.
2. pos = 2 specifies the position where the new element will
be inserted.
3. value = 25 is the new element to insert.
4. The for loop shifts elements one position to the right
side.
5. arr[pos] = value; inserts the new value into the array.
6. Another for loop prints the updated array elements.
Algorithm for Insertion in an Array at a Specific
Position
1. Start
2. Initialize the array:
arr = {10, 20, 30, 40, 50}
3. Set:
position = 2
value = 25
size = 5
4. Repeat the loop from i = size to i > position
Shift elements to the right:
arr[i] = arr[i - 1]
5. Insert the new element at the given position:
arr[position] = value
6. Increase array size by 1.
7. Print the updated array.
8. Stop.
8. Deletion in Array -
Deletion in an array means removing an element from a
specific position of the array. After deletion, all remaining
elements are shifted one position to the left to fill the empty
space.
Program for Deletion in Array –
#include <stdio.h>
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int i, pos = 2, size = 5;
for(i = pos; i < size - 1; i++)
{
arr[i] = arr[i + 1];
}
size--;
printf("Array after deletion:\n");
for(i = 0; i < size; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Output – Array after deletion:
10
20
40
50
Explanation
1. int arr[5] = {10, 20, 30, 40, 50}; creates and initializes the
array.
2. pos = 2 specifies the position of the element to delete.
3. The for loop shifts elements one position to the left.
4. arr[i] = arr[i + 1]; replaces the deleted element with the
next element.
5. size-- decreases the array size after deletion.
6. Another for loop prints the updated array.
Algorithm for Deletion in Array –
1. Start
2. Initialize the array:
arr = {10, 20, 30, 40, 50}
3. Set:
position = 2
size = 5
4. Repeat the loop from i = position to size - 1
o Shift elements to the left:
arr[i] = arr[i + 1]
5. Decrease the size of the array by 1.
6. Print the updated array.
7. Stop.
Conclusion -
Deletion in an array removes an element from a specific
position and shifts the remaining elements to maintain the
sequence of the array.
9. Merging Two Unsorted Arrays –
Merging two unsorted arrays means combining the elements
of two arrays into a single array without arranging them in
any order.
The elements remain in the same order in which they were
originally stored.
Logic of Merging Two Unsorted Arrays
1. Create two arrays with elements.
2. Create a third array to store merged elements.
3. Copy all elements of the first array into the third array.
4. Copy all elements of the second array after the first array
elements.
5. Print the merged array.
Program for Merging Two Unsorted Arrays –
#include <stdio.h>
int main()
{
int arr1[5] = {10, 40, 20, 50, 30};
int arr2[5] = {60, 15, 70, 25, 80};
int merge[10];
int i;
for(i = 0; i < 5; i++)
{
merge[i] = arr1[i];
}
for(i = 0; i < 5; i++)
{
merge[i + 5] = arr2[i];
}
printf("Merged Array Elements are:\n");
for(i = 0; i < 10; i++)
{
printf("%d\n", merge[i]);
}
return 0;
}
Output - Merged Array Elements are:
10
40
20
50
30
60
15
70
25
80
Explanation
1. arr1 and arr2 are two unsorted arrays.
2. merge[10] creates a new array to store merged elements.
3. The first for loop copies elements of arr1 into merge.
4. The second for loop copies elements of arr2 after the
first array elements.
5. The final loop prints all merged array elements.
Algorithm for Merging Two Unsorted Arrays
1. Start
2. Initialize first array:
arr1 = {10, 40, 20, 50, 30}
3. Initialize second array:
arr2 = {60, 15, 70, 25, 80}
4. Create a third array merge[10].
5. Repeat the loop from i = 0 to i < 5
o Copy first array elements:
merge[i] = arr1[i]
6. Repeat the loop from i = 0 to i < 5
o Copy second array elements:
merge[i + 5] = arr2[i]
7. Repeat the loop from i = 0 to i < 10
o Print merged array elements.
8. Stop.
Conclusion -
Merging two unsorted arrays combines the elements of both
arrays into a single array without sorting them.
10. Merging Two Sorted Arrays

Merging two sorted arrays means combining two arrays that


are already arranged in ascending order into a single sorted
array.
The merged array also remains sorted.
Logic of Merging Two Sorted Arrays
1. Take two sorted arrays.
2. Compare elements of both arrays one by one.
3. Insert the smaller element into the new array.
4. Move to the next element of the array from which the
element was taken.
5. Repeat the process until all elements are copied.
6. Print the merged sorted array.
Program for Merging Two Sorted Arrays –
#include <stdio.h>
int main()
{
int arr1[5] = {10, 20, 30, 40, 50};
int arr2[5] = {15, 25, 35, 45, 55};
int merge[10];
int i = 0, j = 0, k = 0;
while(i < 5 && j < 5)
{
if(arr1[i] < arr2[j])
{
merge[k] = arr1[i];
i++;
}
else
{
merge[k] = arr2[j];
j++;
}
k++;
}
while(i < 5)
{
merge[k] = arr1[i];
i++;
k++;
}
while(j < 5)
{
merge[k] = arr2[j];
j++;
k++;
}
printf("Merged Sorted Array:\n");
for(i = 0; i < 10; i++)
{
printf("%d\n", merge[i]);
}
return 0;
}
Output - Merged Sorted Array:
10
15
20
25
30
35
40
45
50
55
Explanation
1. arr1 and arr2 are two sorted arrays.
2. merge[10] stores the merged sorted elements.
3. i, j, and k are index variables.
4. The first while loop compares elements of both arrays.
5. The smaller element is stored in the merged array.
6. Remaining elements are copied using the next two while
loops.
7. The final for loop prints the merged sorted array.
Algorithm for Merging Two Sorted Arrays
1. Start
2. Initialize two sorted arrays:
arr1 = {10, 20, 30, 40, 50}
arr2 = {15, 25, 35, 45, 55}
3. Create a third array merge[10].
4. Compare elements of both arrays.
5. Store the smaller element into the merged array.
6. Move to the next element of the corresponding array.
7. Repeat until all elements are merged.
8. Copy remaining elements if any.
9. Print the merged sorted array.
10. Stop.
Conclusion - Merging two sorted arrays combine two ordered
arrays into a single sorted array efficiently
11. Sorting Using memcpy()
memcpy() is a library function in C used to copy data from one
memory location to another memory location.
It is declared in:
#include <string.h>
In sorting, memcpy() can be used to copy one array into
another array before performing sorting operations.
Syntax of memcpy() –
memcpy(destination, source, size);
Where:
• destination → target array
• source → source array
• size → number of bytes to copy
Program for Sorting Using memcpy() -
#include <stdio.h>
#include <string.h>
int main()
{
int arr[5] = {40, 10, 50, 20, 30};
int copy[5];
int i, j, temp;
memcpy(copy, arr, sizeof(arr));
for(i = 0; i < 5; i++)
{
for(j = i + 1; j < 5; j++)
{
if(copy[i] > copy[j])
{
temp = copy[i];
copy[i] = copy[j];
copy[j] = temp;
}
}
}
printf("Sorted Array:\n");
for(i = 0; i < 5; i++)
{
printf("%d\n", copy[i]);
}
return 0;
}
Output - Sorted Array:
10
20
30
40
50
Explanation of the Program
1. Header Files
#include <stdio.h>
#include <string.h>
• stdio.h is used for input/output functions.
• string.h is used for memcpy() function.
2. Array Declaration
int arr[5] = {40, 10, 50, 20, 30};
This creates an unsorted array.
3. Copy Array
int copy[5];
This array stores copied elements.
4. Using memcpy()
memcpy(copy, arr, sizeof(arr));
This copies all elements of arr into copy.
5. Sorting Logic
Nested for loops compare elements.
if(copy[i] > copy[j])
If the first element is greater, swapping is performed.
6. Swapping Elements
temp = copy[i];
copy[i] = copy[j];
copy[j] = temp;
This swaps two elements.
7. Printing Sorted Array
printf("%d\n", copy[i]);
Prints sorted elements one by one.
Conclusion
memcpy() is used to copy array elements from one array to
another, and sorting is then performed on the copied array
using comparison and swapping techniques.
12. Sorting in Array -
Sorting in an array is the process of arranging array elements
in a specific order such as ascending order or descending
order.
Sorting makes searching and data processing easier and
faster.
Types of Sorting Techniques -
1. Bubble Sort
Bubble sort compares adjacent elements and swaps them if
they are in the wrong order.
Example
40 20 30 10
After sorting:
10 20 30 40
Features
• Simple and easy
• More time consuming for large data
2. Selection Sort
Selection sort finds the smallest element and places it at the
correct position.
Example
40 10 30 20
After sorting:
10 20 30 40
Features
• Less swapping
• Easy to understand
3. Insertion Sort
Insertion sort inserts elements into their correct position one
by one.
Example
30 10 20 40
After sorting:
10 20 30 40
Features
• Efficient for small arrays
• Simple logic
4. Merge Sort
Merge sort divides the array into smaller parts, sorts them,
and merges them.
Features
• Fast and efficient
• Uses divide and conquer technique
5. Quick Sort
Quick sort selects a pivot element and partitions the array
around it.
Features
• Very fast for large data
• Efficient sorting technique
Conclusion
Sorting techniques are important methods used to arrange
array elements in a proper order for easy processing and
searching.
13. Sorting using selection sort –
Selection Sort is a sorting technique in which the smallest
element is selected from the array and placed at the correct
position.
This process continues until the entire array becomes sorted.
Logic of Selection Sort
1. Find the smallest element in the array.
2. Swap it with the first element.
3. Find the next smallest element.
4. Swap it with the second position.
5. Repeat the process until the array is sorted.
Program for Selection Sort –
#include <stdio.h>
int main()
{
int arr[5] = {40, 10, 50, 20, 30};
int i, j, min, temp;
for(i = 0; i < 5; i++)
{
min = i;
for(j = i + 1; j < 5; j++)
{
if(arr[j] < arr[min])
{
min = j;
}
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
printf("Sorted Array using Selection Sort:\n");
for(i = 0; i < 5; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Output - Sorted Array using Selection Sort:
10
20
30
40
50
Explanation of the Program
1. Array Declaration
int arr[5] = {40, 10, 50, 20, 30};
This creates an unsorted array.
2. Outer Loop
for(i = 0; i < 5; i++)
The outer loop controls the number of passes.
3. Minimum Element
min = i;
Assume the current element is the minimum element.
4. Inner Loop
for(j = i + 1; j < 5; j++)
This loop checks remaining elements to find the smallest
value.
5. Comparison
if(arr[j] < arr[min])
If a smaller element is found, update min.
6. Swapping
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
This swaps the current element with the smallest element.
7. Printing Sorted Array
printf("%d\n", arr[i]);
Prints the sorted array elements.
Algorithm for Selection Sort
1. Start
2. Initialize the array:
40 10 50 20 30
3. Repeat from i = 0 to n - 1
4. Assume:
min = i
5. Repeat from j = i + 1 to n
6. Compare:
arr[j] < arr[min]
7. If true, update:
min = j
8. Swap:
arr[i] and arr[min]
9. Repeat until the array is sorted.
10. Print the sorted array.
11. Stop.
Conclusion
Selection Sort repeatedly selects the smallest element and
places it at the correct position to sort the array in ascending
order.
14. Types of Array –
Arrays are classified into different types based on their
dimensions. An array is used to store multiple values of the
same data type under a single variable name.
Arrays can be classified on two bases:
1. On the Basis of Size
2. On the Basis of Dimension
Types of Array on the Basis of Size -
(a) Fixed Size Array
A fixed size array is an array whose size is declared at the time
of array creation and cannot be changed later.
Example -
int arr[5];
This array can store only 5 elements.
Features
• Size remains constant
• Memory is allocated at compile time
• Easy to use
(b) Dynamic Size Array -
A dynamic size array is an array whose size can be changed
during program execution using dynamic memory allocation.
Functions used:
malloc()
calloc()
realloc()

Example
int *arr;
arr = (int*) malloc(5 * sizeof(int));
Features
• Size can change at runtime
• Efficient memory usage
2. Types of Array on the Basis of Dimension -
(a) One Dimensional Array (1D Array)
A one dimensional array stores elements in a single row or
linear form.
It uses only one index.
Syntax
data_type array_name[size];
Example
int arr[5] = {10, 20, 30, 40, 50};
Representation
10 20 30 40 50
Uses
• Marks storage
• Number lists
(b) Two-Dimensional Array (2D Array)
A two-dimensional array stores data in rows and columns like
a table or matrix.
It uses two indexes.
Syntax
data_type array_name[row][column];

Example
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Representation
123
456
Uses
• Matrix operations
• Tables
(c) Multidimensional Array
An array having more than two dimensions is called a
multidimensional array.
Example
int arr[2][2][2];
Uses
• 3D graphics
• Scientific calculations
Conclusion
Arrays are classified on the basis of size and dimensions. Fixed
and dynamic arrays depend on memory allocation, while one
dimensional, two dimensional, and multidimensional arrays
depend on data representation.
15. In 2-D Array (Addition of Two Matrics) -
Matrix addition is the process of adding corresponding
elements of two matrices and storing the result in a third
matrix.
Both matrices must have the same number of rows and
columns.
Formula
C[i][j] = A[i][j] + B[i][j]
Where:
• A = First Matrix
• B = Second Matrix
• C = Result Matrix
Program for Addition of Two Matrices –
#include <stdio.h>
int main()
{
int A[2][2] = {
{1, 2},
{3, 4}
};
int B[2][2] = {
{5, 6},
{7, 8}
};
int C[2][2];
int i, j;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
printf("Addition of Two Matrices:\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Output - Addition of Two Matrices:
68
10 12
Explanation of the Program –
1. Matrix Declaration
int A[2][2]
int B[2][2]
These are two 2-D arrays (matrices).
2. Result Matrix
int C[2][2];
This matrix stores the addition result.
3. Nested Loops
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
Nested loops access rows and columns of matrices.
4. Matrix Addition
C[i][j] = A[i][j] + B[i][j];
Corresponding elements are added and stored in matrix C.
Example:
1+5=6
2+6=8
5. Printing Result
printf("%d ", C[i][j]);
Prints the resultant matrix.
Conclusion
Addition of two matrices is performed by adding
corresponding elements of both matrices using nested loops
in a 2-D array.
16. In 2-D Array (Multiplication of Two Matrics) -
Matrix multiplication is the process of multiplying rows of the
first matrix with columns of the second matrix and storing the
result in a third matrix.
For matrix multiplication:
Number of columns of first matrix = Number of rows of
second matrix
Formula -
C[i][j] = A[i][k] * B[k][j]
Where:
• A = First Matrix
• B = Second Matrix
• C = Result Matrix
Program for Multiplication of Two Matrices –
#include <stdio.h>
int main()
{
int A[2][2] = {
{1, 2},
{3, 4}
};
int B[2][2] = {
{5, 6},
{7, 8}
};
int C[2][2];
int i, j, k;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
C[i][j] = 0;
for(k = 0; k < 2; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
printf("Multiplication of Two Matrices:\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
Output - Multiplication of Two Matrices:
19 22
43 50
Explanation of the Program
1. Matrix Declaration
int A[2][2]
int B[2][2]
These are two matrices stored using 2-D arrays.
2. Result Matrix
int C[2][2];
This matrix stores multiplication results.
3. Nested Loops
Three loops are used:
• i → rows
• j → columns
• k → multiplication process
4. Matrix Multiplication
C[i][j] = C[i][j] + A[i][k] * B[k][j];
This multiplies row elements of first matrix with column
elements of second matrix.
Example:
C[0][0] = (1×5) + (2×7)
= 5 + 14
= 19
5. Printing Result
printf("%d ", C[i][j]);
Prints the resultant matrix.
Conclusion
Matrix multiplication in a 2-D array is performed by
multiplying rows and columns of two matrices using nested
loops.

You might also like