Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Recursive pseudocode to search an array for positive numbers

Posted on 2006-05-07
Medium Priority
386 Views
Hi,

I've been trying to write a recursive function to count the number of positive integers in an array.  The thing is, it's easy if I just use a for loop like this:

for(int i =0; i<n; i++)
{
if(A[i] > 0)
count ++;
}

0
Question by:warriorfan808
• 2

LVL 45

Expert Comment

ID: 16624110
Hi warriorfan808,

This problem lends itself better to iteration as compared to recursion

Recursive function requires ...
1. A terminating condition
2. Logic to maintain your results

Here, terminating condiftion will be when i becomes equal to n and count will be mantained for each level of recursion .. something like

int count_positive (int index, int max_index, int count )
{
int ret;

if (index>= max_index)
return 0;

ret = count_positive (index+1, max_index, count);

if (A[index] >0)
return ret+1;
else
return ret;

Cheers!
Sunnycoder
0

LVL 1

Author Comment

ID: 16624280
Thanks for the reply sunnycoder.  Whoa, you're on another level.  Do you mind if I sort through this so I can understand it?

int count_positive (int index, int max_index, int count )
{
int ret;             //what does this do?  Is it the count of positive numbers?

if (index>= max_index)    // is max index (n-1)
return 0;

ret = count_positive (index+1, max_index, count);   //this part is where recursion occurs and the index increments by one through each recursive call?

if (A[index] >0)
return ret+1;
else
return ret;

sorry man, I'm a horrible programmer.
0

LVL 45

Accepted Solution

sunnycoder earned 2000 total points
ID: 16624345
int count_positive (int index, int max_index, int count )
{
int ret;             //what does this do?  Is it the count of positive numbers? No it is a simple variable to store the return value from recursive call ... we do return the count so far, so in that sense yes it does have a role in maintaining count

if (index>= max_index)    // is max index (n-1) .. thats right
return 0;

ret = count_positive (index+1, max_index, count);   //this part is where recursion occurs and the index increments by one through each recursive call? .. right again ...

if (A[index] >0)
return ret+1;
else
return ret;
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Progress
###### Suggested Courses
Course of the Month21 days, 7 hours left to enroll