0

I am trying to practice C programming after a long time and stuck in the very basic. Trying to search in an array that works fine using while loop but when tried to use For loop, I am not getting the actual output. Here is the code that I've tried and works almost fine:

#include<stdio.h>
int main()
{
    int arr[100], element, num, i;
    int cnt = 1;

    printf("Enter no of elements: ");
    scanf("%d", &num);

    printf("\nEnter the values: ");
    for(i = 0; i < num; i++)
    {
        scanf("%d", &arr[i]);
    }

    printf("\nEnter the element to be searched: ");
    scanf("%d", &element);

    for(i = 0; i < num; i++)
    {
        if(arr[i] == element)
        {
           printf("\nNumber found at the index = %d", i + 1);
        }
        else
        {
            /**To prevent repeated output but trying to remove this section - Starts**/
            if(cnt == 1)
            {
               printf("\nNumber not found");
            }
            cnt += 2;
            /**To prevent repeated output but trying to remove this section - Ends**/
        }
    }
    return 0;
}

The problem is when the searched value is matched, it gets the index of the matched value as well it prints the message 'Number not found'. But if no value matches, it only returns 'Number not found'. Here is the output I am getting for the searched values:

array_search

2
  • 1
    You need to reinitialize the value of cnt within the loop. Commented Dec 10, 2016 at 17:46
  • 1
    Well, that's obvious: The message that the number wasn't found appears always when there is a number in the array that isn't the one you look for, which will be the case with most arrays. You will only know whether the number is in the array or not after either finding the number or looking at all elements in the array. Commented Dec 10, 2016 at 17:46

2 Answers 2

1

fixed program is:

#include<stdio.h>
int main()
{
    int arr[100], element, num, i;
    int cnt = 1;

    printf("Enter no of elements: ");
    scanf("%d", &num);

    printf("\nEnter the values: ");
    for(i = 0; i < num; i++)
    {
        scanf("%d", &arr[i]);
    }

    printf("\nEnter the element to be searched: ");
    scanf("%d", &element);

    for(i = 0; i < num; i++)
    {
        if(arr[i] == element)
        {
           printf("\nNumber found at the index = %d", i + 1);
           cnt=0;
           break; //break the loop
        }
    }
    if(cnt==1){
        printf("\nNumber not found");   
    }
    return 0;
}

Output:

Enter no of elements: 4 

Enter the values: 20 10 40 30 

Enter the element to be searched: 10

Number found at the index = 2

As number found make cnt=0 and break the loop; after loop you can check if value of cnt is still 1 that means number not found.

Sign up to request clarification or add additional context in comments.

Comments

1

Or you could simply do

    if(arr[i] == element)
    {
       printf("\nNumber found at the index = %d", i + 1);
       cnt = 0;
       break;
    }

remove the else part and then after the for loop, check if cnt is still 1. print not found if it is still 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.