I was just trying to write a linear search program in C. But I'm not getting expected output for some reason that I don't understand. Please, someone, explain what I've done wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main(){
int arr[5];
int query;
bool found = false;
int index;
printf("Insert five elements of your choice: \n");
/*
Getting 5 input from user.
*/
for(int i=0; i<5; i++){
printf("Element %d: ", i+1);
scanf("%d\n", &arr[i]);
}
printf("What number you are looking for: ");
scanf("%d\n", &query);
/*
Checking if the the number is in the array or not!
*/
for(int check=0; check<5; check++){
if(arr[check] == query){
found = true;
index = check;
}
}
if(found){
printf("Result: %d was found in index %d.\n", query, index);
}else
{
printf("Result: Item was not found!");
}
}