EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Array Functions in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Array Functions in C

array function in C

Introduction to Array Functions in C

Array Functions in C is a type of data structure that holds multiple elements of the same data type. The size of an array is fixed and the elements are collected in a sequential manner. There can be different dimensions of arrays and C programming does not limit the number of dimensions in an Array.

Different Functions of Array in C

There are different functions that can be performed on arrays.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1) Traversing

Traversing an Array means going through each element of an Array exactly once. We start from the first element and go to the last element. An example of such a program that performs traversing operation on a linear Array is given below in C language.

Code:

#include <stdio.h>
void main()
{
int array[] = {1,2,3,4,5};
int i, n = 5;
printf(" The array elements are: \n " );
for( i=0;i < n; i++)
{
printf(" array[%d] = %d \n " , i, array[i] );
}
}

Output:

traversing Array in C

2) Searching

The search operation is used to find a particular data item or element in an Array. We can perform searching in an unsorted array with the help of traversal of the Array. The linear traversal from the first element to the last element can be used to search if a given number is present in an Array and can also be used to find its position if present.

This is done by comparing each element with the given element (which is to be searched). Once the element is found, the search operation is stopped. Here is an example to show search operation performed on an Array in C

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Code:

#include<stdio.h>
int findElement(int arr[], int n,  int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key
return i;
return -1;
}
int main()
{
int arr[] = {1, 4, 0, 6, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 4;
int position = findElement(arr, n, key);
if (position == - 1)
printf("Element not found");
else
printf("Element Found at Position: %d", position + 1 );
return 0;
}

Output:

searching in array function in C

3) Insertion

Insertion operation is used to add a new element in the Array. When we specify the particular element and position where it is to be added in the Array, we perform insertion operation. However, the size of the Array is not disturbed while performing this operation. An element will be inserted in an array only if it has sufficient space to add it. If the size of an array is full already, a new element cannot be added. An example to show insert operation in an unsorted Array in C.

Code:

#include<stdio.h>
int insertSorted(int arr[], int n,  int key, int capacity)
{
if (n >= capacity)
return n;
arr[n] = key;
return (n + 1);
}
int main()
{
int arr[20] = {8, 5, 6, 9, 0, 7} ;
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 2;
printf("\n Before Insertion: ");
for (i = 0; i < n; i++)
printf("%d  ", arr[i]);
n = insertSorted(arr, n, key, capacity);
printf("\n After Insertion: ");
for (i = 0; i < n; i++)
printf("%d  ",arr[i]);
return 0;
}

Output: 

intersection in array function in c

4) Deletion

In delete operation, the element which already exists in the Array is searched (using linear search) and deleted, followed by the shifting of elements. The user enters the position of the element which is to be deleted from the array. Deletion operation, just like the insertion operation, does not affect the size of array. Also, the position of the element to be deleted should be within the size of array, since the deletion of an element beyond the size of Array is not possible. C program to show delete operation in an unsorted array.

Code:

#include<stdio.h>
int findElement(int arr[], int n, int key);
int deleteElement(int arr[], int n, int key)
{
int pos = findElement(arr, n, key);
if (pos == - 1)
{
printf("Element not found");
return n;}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;return - 1;
}
int main()
{
int i;
int arr[] = {1, 5, 3, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 3;
printf("Array before deletion\n");
for (i = 0; i < n; i++)
printf("%d  ", arr[i]);
n = deleteElement(arr, n, key);
printf("\nArray after deletion\n");
for (i = 0; i < n; i++)
printf("%d  ", arr[i]);
return 0;
}

Output:

deleting

5) Sorting

This operation is performed to sort an Array into a fixed order, i.e., either ascending or descending. Here is an example of sort operation on an Array in C

Code:

#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a =  number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
}

Output: 

sorting

Different ways of Sorting an Array

Below are the different sorting methods for Array:

1) Bubble Sort

Bubble sort compares all the elements one by one and sorts them based on their values. It starts by comparing the first element with the second, if the first element is greater than the second element, it will swap both the elements, and carry on comparing the second and the third element, and so on.

2) Selection Sort

The basic idea behind selection sort is finding the least element in the unsorted array, replacing it with the first element. Then continue the same process with the rest of the unsorted array, i.e., from the second position, then from the third and so on.

3) Merge Sort

This method of sorting is based on the divide and conquer technique. It splits the array into two equal subarrays and continues until each subarray contains a single element, and then merges them in a sorted manner resulting in a sorted array.

4) Insertion Sort

In insertion sort, we start with the second element. The array elements are compared with each other in a sequential manner. The current element(the value to be sorted)  is compared with all the elements in the sorted subarray. All the elements in the sorted subarray which are greater than the current element are shifted, and the current value is inserted. This process is repeated until the whole array is sorted.

5) Quick Sort

Quicksort, just like the merge sort, is also based on the divide and conquer algorithm. In this method, an element is picked as the pivot (generally the first element). Then, partitions of an array are made around the picked pivot i.e., all the elements less than the pivot will form one sub-array and all the elements greater than the pivot will form another. The procedure is repeated with the sub-arrays too until the whole array is sorted.

6) Heap Sort

The algorithm of heap sort is based on the comparison. The maximum element is selected and placed in the end position. Then the second largest element is found and placed in the second last position. This process is repeated for all the elements.

Recommended Articles

This is a guide to Array Functions in C. Here we discuss the basic concept and different functions and ways of sorting an Array respectively. You can also go through our other related articles to learn more –

  1. Arrays in C++
  2. Arrays in R
  3. Functions in R
  4. Advantages Of Array

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More