CodersEditor Tutorials

CodersEditor Tutorials

  • Online IDE
  • Q&A
  • C Programming
  • Java
  • GraphQL.NET
  • System Software

›Arrays

C Programming

    Introduction

    • Overview
    • Installation & Setup
    • C Program Structure
    • Keywords & Identifiers
    • Data Types
    • Variables & Constants
    • Scope Rules
    • Storage Class
    • Standard Input and Output
    • Operators

    Control Flow/Decision Making

    • Overview
    • if Statement
    • if else Statement
    • Nested If statement
    • Switch Statement
    • Nested Switch Statement
    • Conditional Operators

    Loop

    • Loops Overview
    • While loop
    • For Loop
    • Do while
    • Nested Loops
    • Break
    • Continue Statement
    • Goto statement

    Arrays

    • Arrays
    • Multi Dimensional Arrays
    • Arrays & Functions

    Pointers

    • Pointers Basics
    • Pointers and Arrays
    • Pointers and Functions
    • Memory allocation

    Strings

    • String Basics
    • String Functions

    C Programming Examples

    • Basic Example 1

Arrays & Functions

In this article you will learn about the different functions of an array. There are five different functions that can be performed on arrays.

1) Traversing

Traversing an Array is a function in which each element of an Array is passed only once, starting from first element to the last in a linear pattern.

Sample program for Traversing in C

#include <stdio.h> 
  
void printTraversingArray(int* arr, int n) 
{ 
    int i; 
  
    printf("Traversing Array: "); 
    for (i = 0; i < n; i++) { 
        printf("%d ", arr[i]); 
    } 
    printf("\n"); 
} 
  
int main() 
{ 
    int arr[] = { 5,-1,3,-2,55}; 
     int n = sizeof(arr) / sizeof(arr[0]);
  
    printTraversingArray(arr, n); 
  
    return 0; 
} 

Output

Traversing Array: 5,-1,3,-2,55

2) Searching

The search operation is used to find a particular data item or element in an Array. It can be performed 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.

Sample program for Searching in C

#include<stdio.h>
int findElement(int array[], int size, int elementToBeSearched) 
{
    int i;
    for (i = 0; i < size; i++)
    if (array[i] == elementToBeSearched)
    return i;
    return - 1;
}
int main()
{
    int array[] = { 25, 57, 23, 74, 37, 22 };
    int size = sizeof(array) / sizeof(array[0]);
    int elementToBeSearched = 57;
    int pos = findElement(array, size, elementToBeSearched);
    if(pos==-1){
    printf(" Element %d not found", elementToBeSearched);
}
else{
    printf(" Position of %d is: %d", elementToBeSearched ,pos+1);
}
return 0;
}

Output

Position of 57 is: 2

3) Insertion

Insertion operation is used to add a new element in the Array. We need to specify the particular element and the position of the element, i.e. where the new element should be added in the Array. But, the size of the Array is not overridden 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.

Sample program for Insertion in C

#include<stdio.h>
int insertElement(int arr[], int elements, int elementToBeInserted, int size)
{
    if (elements >= size)
    return elements;
    arr[elements] = elementToBeInserted;
    return (elements + 1);
}
int main()
{
    int array[20] = { 23, 57, 73, 15, 35, 46 };
    int size = sizeof(array) / sizeof(array[0]);
    int elements = 6;
    int i, elementToBeInserted =55 ;
    printf("Before Insertion: ");
    for (i = 0; i < elements; i++)
    printf("%d  ", array[i]);
    elements = insertElement(array, elements, elementToBeInserted, size); 
    printf("\n After Insertion: ");
    for (i = 0; i < elements; i++)
    printf("%d  ",array[i]);
    return 0;
}

Output

Before Insertion: 23  57  73  15  35  46
After Insertion: 23  57  73  15  35  46  55

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.

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

Different ways of Sorting an Array

There are different methods of sorting in an array. They are Bubble Sort. Selection Sort, Merge Sort, Insertion Sort, Quick Sort, Heap Sort.

← Multi Dimensional ArraysPointers Basics →
  • 1) Traversing
    • Sample program for Traversing in C
    • Output
  • 2) Searching
    • Sample program for Searching in C
    • Output
  • 3) Insertion
    • Sample program for Insertion in C
    • Output
  • 4) Deletion
  • 5) Sorting
    • Different ways of Sorting an Array
CodersEditor Tutorials
CoderdEditor Developer Tools
CodersEditor Online EditorGit Flavoured Markup EditorFree Online JSON FormatterFind My IP Address Details
Tutorials
C ProgrammingJava
Copyright © 2021 CodersEditor.com