0

I created a program that search the specific number in array and give it position. here's the code.

#include<stdio.h>
#include<conio.h>
void main()
{
    int arr[10], srch, i;
    clrscr();

    printf("Enter elements of array.\n");
    for(i=0;i<10;i++){
        scanf("%d", &arr[i]);
    }

    printf("Enter element to search\n");
    scanf("%d", &srch);

    for(i=0;i<10;i++){
        if(arr[i]==srch){
            printf("%d is at position %d", srch, i+1);
            break;
        }
    }
    //Below statement run when no such number found in array.
    if(srch!=arr[i])
         printf("%d", arr[i]);
  
    getch();
}

Well, I can't understand why am i getting this output. whenever i run this program and entered a value that is in the current array it found its position and give correct answer.

But I have doubt that when i print this array with last value of index which was 10 it give the value of 'srch variable' i don't know why it is giving this as array 10 index.

For example when i enter 11 as value to search which will not be in array print("%d", arr[i]) // It print value of srch

4
  • 1
    After loop exit and no elements found, i==11 and access to arr[i] causes out of bounds access. It's undefined behaviour. Commented Jul 6, 2022 at 8:20
  • 1
    @dimich you probably mean i==10. Commented Jul 6, 2022 at 8:21
  • Yes, you right, i==10. Commented Jul 6, 2022 at 8:23
  • Besides the out-of-bounds issue, that condition does not make much sense if(srch!=arr[i]) Why print some "random" array element if the value you were looking for, wasn't found? It would make more sense to print something like "value not found". Commented Jul 6, 2022 at 9:48

4 Answers 4

0

After coming out the second loop in your code the value of i will be equal to 10. And when the if(srch!=arr[i]) gets read by the compiler, it searches for the arr[10] which has not been allocated by you. So theres a garbage value there at that position. That is being printed by printf("%d", arr[i]);

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

1 Comment

I would not call it "garbage". That is not uninitialized memory but it is memory not belonging to the array. Of course that is undefined behaviour in any case.
0

It's garbage value. You are reaching an index which is not created. Out of bonds. So you get a garbage value. It can be anything.

2 Comments

Yes, but garbage value could be anything right, but i tried this in many other devices and i am getting the value of srch in all.
is your srch in the last index of array in all times? I recommend you ignore that but if you insist, please give ALL inputs and more details please. @FgrReloaded
0

After the loop i is one past the last element, thus you can't access the array at this index.

You need to handle the fact that the value has been found or not, possibly this way:

int found = 0; // value not found
for (i=0; i<10; i++) {
    if (arr[i]==srch) {
        printf("%d is at position %d", srch, i+1);
        found = 1; // value found
        break;
    }
}
if (!found) {
    //Below statements run when no such number found in array.
}

2 Comments

if (i >= 10) ...
@dimich more error prone in case of refactoring the array length...
0

When No element found then from second for loop value of i will be 10. which is out of the bound of the array. since array will have 0,1,2.....9 index and you are trying to access 10th index, It will have garbage value.

to prevent this issue, you can replace i with i-1

 if(srch!=arr[i-1])
     printf("%d", arr[i-1]);

1 Comment

That only solves the out-of-bounds issue but will take wrong index in case the value was fond. Generally the condition is nonsense already in the question. It could better be done using if (i < 10)

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.