Hi guys,

Do anyone knows how we can implement this code:
Write an efficient recursive function called IndexEqual(A,i,j) that returns true if there
exists an index x (i < x < j) such that A[x] = x, otherwise returns false. You may assume A is a
sorted integer array in which every element is unique.


I need a help quickly.

Recommended Answers

All 12 Replies

>Do anyone knows how we can implement this code
Yes.

>I need a help quickly.
We're not a charity for lazy students. If you want help, you need to do some work yourself first.

>Do anyone knows how we can implement this code
Yes.

>I need a help quickly.
We're not a charity for lazy students. If you want help, you need to do some work yourself first.

ok I will show u my work

That's what I have done so far

int IndexEqual (int A, int i, int j)
{
int index[20];
	
	int k;

	for ( k = i; k =< j; k++)
	{
		if index[20] == A;
		{
			return 1;
		}

		else return 0;
	}
	

	IndexEqual( A, i, j);
}

Well, your first problem is not reading the requirements carefully. Let's start with the basic premise of the function:

returns true if there exists an index x (i < x < j) such that A[x] = x, otherwise returns false.

I interpret this to mean the value of any array element matches the index. Iteratively you'd do it like so:

for ( int x = i; i < j; i++ ) {
  if ( A[x] == x )
    return true;
}

return false;

You may assume A is a sorted integer array in which every element is unique.

In your attempt, A is not an array. I would expect a function definition more like this:

bool IndexEqual ( int A[], int i, int j )
{
  //...
}

Give it another shot.

I modified my code to

int IndexEqual (int A, int i, int j)
{
int index[20];
	
	int k;

	for ( k = i; k =< j; k++)
	{
		if index[k] == A;
		{
			return 1;
		}

		else return 0;
	}
	

	IndexEqual( A, i, j);
}

what does iterative version mean?

>what does iterative version mean?
Clicky.

what does iterative version mean?

It means that it's a demo, but not the answer you need. "This is what you need to accomplish, but a different way."

You need a recursive version.

>what does iterative version mean?
Clicky.

come on man ! I am just a beginner seeking help

>I am just a beginner seeking help
I am helping you. Do you want to beg for "help" the rest of your life? I'm not going to just give you answers that a sack of gravel could find in a few seconds, but I'll gladly teach you how to help yourself and answer your more complicated questions in the process.

If you don't want that, just say the word and I'll not waste my time with you.

I think of an iteration as being a single time through something or a version of something that changes over time. So if I use a loop to increase the value of x by 1 ten times there are 10 iterations to the process. There are 20 some versions/iterations to the shape of the moon. And so on. Iterative means using iteration.

Recursion means calling the same function from within the function (passing the new call with different values of the parameters).

In general, a recursive process can be done iteratively and visa versa. However, the distinction between an iterative and a recursive approach to the problem isn't the real hang up at this point.

That explanation aside, the problem here and now appears to be how to declare and use arrays and elements of an array. Once that problem is answered then converting the iterative solution to a recursive one can be pursued.

>what does iterative version mean?
Clicky.

>I am just a beginner seeking help
I am helping you. Do you want to beg for "help" the rest of your life? I'm not going to just give you answers that a sack of gravel could find in a few seconds, but I'll gladly teach you how to help yourself and answer your more complicated questions in the process.

If you don't want that, just say the word and I'll not waste my time with you.

I got you now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.