Arrays-Linear Search

Can you please tell me what i am doing wrong here

any TA available please help me here

Hi @akshat42bajpai
First of all don’t return - 1, cout statement should be there
Secondly print - 1 only if you have not found the key after traversing whole array, ie it should be printed after for loop not inside it. You can keep a variable to check if desired element was found or not and print - 1 accordingly

i am not able to understand the desired variable concept like what are you tryin to say

Desired element is the key you’re searching.

  1. Initialize isFound variable = false
  2. Traverse the array and try to find the key. If key is found, update the isFound variable = true and print i
  3. After array is traversed completely, check isFound variable. If isFound == false then print -1.

If this is not clear i can send code

this is clear thank you

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.