Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8339263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:50:17+00:00 2026-06-09T04:50:17+00:00

I want to find an element of array by using recursion.The function takes an

  • 0

I want to find an element of array by using recursion.The function takes an array and the target value. The recursion function checks whether it is an element of the given array. But unfortunately I cant set this code on it.The function always retuns ‘1’ . where do I make mistake?

#include <stdio.h>

int isElement(int *a, int target) ;

int main()
{

  int a[] = {3,5,6,4,2,32,15} ;
  int target = 12 ,res  ;


  res = isElement( a, target) ;

  if(res==1)
    printf("its an element of the array\n");

  if(res==0) 
    printf("its not an element of the array\n");

  return 0 ;
}


int isElement(int *a, int target)
{
  int son=0  ;


  printf("array = %d\n",a[0] );

  if(a[1] == '\0')
      son = 0 ;

  else if(target == a[0])
    son = 1 ; 

  else
    son = isElement(&a[1] ,target);

  return son ;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:50:19+00:00Added an answer on June 9, 2026 at 4:50 am

    Your termination condition is for when a[1] == '\0'. However, you array doesn’t terminate with a 0. Therefore you are searching for target out of the boundaries of the array, over the rest of the stack. This is undefined behavior, so anything could happen (and you can’t complain). However, in your specific case, it so happens that target is placed after a on the stack, so once you go out of a, you see the same value you were looking for, hence always returning 1.

    What you should do is change the definition of a to:

    int a[] = {3,5,6,4,2,32,15, 0} ;
                               ^^^
    

    Also, the condition:

    if(a[1] == '\0')
          son = 0 ;
    

    will give you wrong results in such an example:

    int a[] = {12, 0};
    int target = 12;
    

    Because before checking for a[0] == target, you mark this case as fail. Therefore you should change the condition to:

         vvv
    if (a[0] == 0)
        son = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find an html element by its value. I tried with id,
I want to write a program to find the n-th smallest element without using
I need to check whether document element value is changed after particular operation using
I want to find the first element that has appeared in previous positions in
I know that in Jsoup when you want to find a certain Element with
I want to find out the position of an element which is in iframe.
I want to add style in ASP.NET textbox control. I couldn't find textbox element
I want find all Saturdays and Sundays in A given month. How can I
I want to find a struct in a vector, but I'm having some troubles.
I am trying to find the location of an element in the array. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.