C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Determine Whether An Array Contains An Element in C Programming Language

In this tutorial, we will learn how to determine if an array contains a specific element in the C programming language. This operation is a common task when working with arrays, as you may need to check if an item exists within the array before performing further operations.

  • Linear search:

To determine whether an array contains an element, you can use a linear search algorithm. This involves iterating through the array and comparing each element with the target value. If a match is found, return a 1 (true) and exit the loop. If the loop completes without finding a match, return a 0 (false).

Here's a function that implements a linear search to check if an element is in an array:

#include <stdio.h>

int containsElement(int arr[], int size, int target) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == target) {
            return 1; // Element found
        }
    }
    return 0; // Element not found
}

int main() {
    int myArray[] = {1, 2, 3, 4, 5};
    int size = sizeof(myArray) / sizeof(myArray[0]);
    int target = 3;

    if (containsElement(myArray, size, target)) {
        printf("The array contains %d.\n", target);
    } else {
        printf("The array does not contain %d.\n", target);
    }

    return 0;
}

The output will be:

The array contains 3.

This function can be applied to any integer array and can be modified to work with other data types (e.g., float, char, or even custom structures) by changing the function signature, data type of the target, and comparison operation.

Note that the linear search algorithm has a time complexity of O(n), where n is the size of the array. In the worst case, the target element might be at the end of the array or not in the array at all, resulting in the loop iterating through all elements. For large data sets, this can be time-consuming. If the array is sorted, more efficient search algorithms, such as binary search, can be used.

That's it for our tutorial on determining whether an array contains an element in the C programming language. Understanding how to perform linear search in an array is a fundamental skill for working with data sets and performing lookups or validations.

  1. C program to check if an array contains a specific element:

    #include <stdio.h>
    
    int main() {
        int numbers[] = {1, 2, 3, 4, 5};
        int target = 3;
        int found = 0;
    
        // Linear search
        for (int i = 0; i < sizeof(numbers) / sizeof(numbers[0]); ++i) {
            if (numbers[i] == target) {
                found = 1;
                break;
            }
        }
    
        if (found) {
            printf("Element %d found in the array.\n", target);
        } else {
            printf("Element %d not found in the array.\n", target);
        }
    
        return 0;
    }
    
    • Checks if the array contains a specific element using a linear search.