Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Searching in array in C

#include <stdio.h>

int main()
{

    int arr[] = {10, 20, 30, 8, 2};
    int index = -1, ele;

    printf("Enter the elment you want to search :");
    scanf("%d", ele);
    for (int i = 0; i < 5; i++)
    {
        if (arr[i] == ele)
            ;
        index = i;
        break;
    }

    if (index == -1)
    {
        printf("Not found\n");
    }
    else
    {
        printf("Found at %d", index);
    }

    return 0;
}

the above code isn’t printing the result. I am learning C programing from past few days. Can You please help me out with this. I am not getting any type of error.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your if statement doesn’t have any code within it (just a semi colon). Prefer using braces


        if (arr[i] == ele) {
          index = i;
          break;
        }

Also, you’ll need to specify the address of the variable you are reading into when using scanf, e.g.

    scanf("%d", &ele); //< take the address of 'ele' using &
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading