Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,943 questions

55,787 answers

573 users

How to implement the none_of_equal algorithm to check if no element equal to a value in C

1 Answer

0 votes
#include <stdio.h>
#include <stdbool.h>

/*
    none_of_equal: Check whether NO element in an array
                   is equal to a specific value.

    Parameters:
        arr     - pointer to the first element of the array
        n       - number of elements
        value   - the value to compare against

    Returns:
        true  - if no element equals 'value'
        false - if at least one element equals 'value'

    Idiomatic C approach:
        - Use a simple loop (fast and predictable).
        - Stop immediately when the first match is found (efficient).
*/
bool none_of_equal(const int *arr, size_t n, int value) {
    for (size_t i = 0; i < n; ++i) {
        if (arr[i] == value) {
            return false;   // Early exit: found a matching element
        }
    }
    return true;            // No element matched
}

int main(void) {
    int data1[] = { 5, 3, 8, 1 };
    int data2[] = { 7, 7, 7, 7 };
    int data3[] = { 2, 4, 6, 8 };

    size_t size1 = sizeof(data1) / sizeof(data1[0]);
    size_t size2 = sizeof(data2) / sizeof(data2[0]);
    size_t size3 = sizeof(data3) / sizeof(data3[0]);

    printf("None equal to 3 in data1? %s\n",
           none_of_equal(data1, size1, 3) ? "true" : "false");

    printf("None equal to 7 in data2? %s\n",
           none_of_equal(data2, size2, 7) ? "true" : "false");

    printf("None equal to 5 in data3? %s\n",
           none_of_equal(data3, size3, 5) ? "true" : "false");

    return 0;
}


/*
run:

None equal to 3 in data1? false
None equal to 7 in data2? false
None equal to 5 in data3? true

*/

 



answered Jul 24 by avibootz

Related questions

...