-8

For example I have a one dimensional array integers as its elements and I want to make sure that at least one of the element in the array is 5. How can I achieve this in C language ?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
underdog_eagle
  • 25
  • 1
  • 1
  • 5

4 Answers4

2

You can write such a function yourself.

Here is a demonstrative program. The function is called any_of like the corresponding algorithm in C++.

#include <stdio.h>

int /* _Bool */ any_of( const int a[], size_t n, int value )
{
    size_t i = 0;

    while ( i < n && a[i] != value ) i++;

    return i != n;
}

int main( void ) 
{
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    const size_t N = sizeof( a ) / sizeof( *a );

    int value = 5;

    printf( "The array contains %d is %s\n", 
            value, 
            any_of( a, N, value ) != 0 ? "true" : "false"  );

    return 0;
}

Its output is

The array contains 5 is true
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

This is a very basic question, however, here's an example, I hope it'll help you in your programming efforts

int main()
{
    int a[5] = { 1,2,3,5,3 };
    bool fiveFound = false;
    for (int i = 0; i < 5; ++i) {
        if (5 == a[i]) {
            printf("At least one element of the array is 5");
            fiveFound = true;
            break;
        }
    }
    if (!fiveFound) {
        printf("Array doesn't contain 5");
    }
}

I'd also suggest you to read The C Programming Language (Second edition) by Brian W. Kernighan and Dennis M. Ritchie, and do some online tutorials

buld0zzr
  • 962
  • 5
  • 10
  • Thanks for the help, actually I'm beginner in the programming world. – underdog_eagle Jul 01 '16 at 15:50
  • @underdog_eagle here's a link to good book list on C http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list, also, a stackoverflow way to say thanks is accepting the answer too :) – buld0zzr Jul 01 '16 at 15:51
0

No there is no such shortcut for that in C [And I think not even in any language]. But if you really want this fuctinality you may find it easier to write a function to check whether the element exists in the array or not.

Madhusoodan P
  • 681
  • 9
  • 20
-2

You need to iterate through the elements of the array:

#define SIZE 5

int main() {
    int array[SIZE] = {2, 4, 6, 8, 10};
    for (int i = 0; i<SIZE; i++){
        if (array[i] == 5)
            printf("Found it!\n");
            return 0;
    }
    return -1;
}
Aleins
  • 43
  • 8
  • 1
    check the `printf()` statement – Cherubim Jul 01 '16 at 15:45
  • 1
    @Aleins be careful of undefined behaviour, and you're doing just that when not initializing the array before using it – buld0zzr Jul 01 '16 at 15:53
  • @cherubim anand what do you mean? – Aleins Jul 01 '16 at 15:59
  • @buld0zzr fixed now – Aleins Jul 01 '16 at 16:00
  • 1
    BTW please try to get into the habit of putting the `newline` at the other end of your `printf` statements, such as `printf("Found it!\n");` That way, you can be sure they reach the terminal. And if you ever insert debugging cues, without that you can't be certain to get the right messages before a crash. (This fixes @CherubimAnand comment too). – Weather Vane Jul 01 '16 at 16:02
  • The answer from @VladfromMoscow is the equivalent of the [MCVE](http://stackoverflow.com/help/mcve) which is requested of those asking a question. It is complete (has the header), is compilable, and is verifiable by changing the `5` in the array. – Weather Vane Jul 01 '16 at 16:08
  • @Weather Vane I like having the '/n' at the beginning of printfs. In my experience, this is the most purposeful way to do it. It's basically a question of preference – Aleins Jul 01 '16 at 16:13
  • No it's a question of functionality. – Weather Vane Jul 01 '16 at 16:14