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
i==10.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".