Recursion with Arrays

Hello again everyone!
So I am stuck. I want to write a recursion that pulls information from an Array. This array only has 10 elements to it. I want to print out how many times each number appears in the array using recursion.
This is what I have so far. I just need a nudge in the right direction really I feel kinda lost.

1
2
3
4
5
int howmany(int a[], int length, int target)
{
	if (target == 0)return 0;
	else ()
}


thanks!
Remember our last thread.
http://www.cplusplus.com/forum/general/171740/

The principles are the same.

Say I call it with the array int xs[] = { 1, 2, 3, 4 };

a1[], length1 = @xs[0], 4

Now treat your array as: first element, rest of the array. Here, that's 1, { 2, 3, 4}.

Think about how to get a2[], length2 from the previous ones when you recurse.

Good luck!
Last edited on
So instead of going n-1 would it be n+1?

I say this to show that I am moving forward through the Array.

Say like this?

1
2
if (target==0)return 0;
else return howmany(a,length,target+1);


On top of all of it I am having issues calling howmany in main.
Last edited on
You aren't paying attention to what things mean.

(Why would target change? That's the thing your looking for!)
.
Last edited on
So what you are saying is that I don't need to be moving target. What I need to be doing is moving length through the array and having target output what number or for that matter how many times that number appears in the array.

So instead of:
 
else return howmany (a,length,target+1);


I am looking to move length through so:
 
else return howmany(a,target,length+1) /* or minus one respectfully*/
Topic archived. No new replies allowed.