need C++ help with recursion

Started by
10 comments, last by Omicron 21 years, 5 months ago
well i finally got it working. i first tried using the example that codejunkie posted, although i used an array instead of pointers, cause im not too good with pointers either. but i was getting crazy answers for the max, like 69392734. even though my numbers were like 5,7,9,3,4. but i also posted at another programming forum and someone was able to help me out. i posted both codes below. also thanx for all the help, it was very usefull.

heres the code that wasn't working


int get_max(int bigv, int tot, LIST_I num)
{
if (tot <0 )
return bigv;
if (num[tot]> bigv)
bigv = num[tot];
get_max(bigv, tot-1, num);
return bigv;

}


and heres the code that i ended up using


int get_min(int tot, LIST_I num)
{
if (tot==1)
return num[0];
else
{
int left = get_min(tot/2, num);
int right = get_min(tot-(tot/2), num+(tot/2) );
if(left < right)
return left;
else
return right;
}
}



[edited by - Omicron on December 4, 2002 1:18:56 AM]
Advertisement
Yeah, i figured it out what was wrong before you posted.

int get_max(int bigv, int tot, LIST_I num)
{
//if (tot <0 ) return bigv; *forget the deincrement*
if (--tot <0 ) return bigv;
if (num[tot]> bigv) bigv = num[tot];
return(get_max(bigv, tot, num)); // this what i didn't do right in the first place
}

Thanks!

[edit]
Should have read the code all the way after pasting it. Forgot to deincremet tot. Man!!!

[edited by - CodeJunkie on December 4, 2002 3:08:56 AM]

This topic is closed to new replies.

Advertisement