473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help with recursion please

if i write a function to find the max value of an array and want to
write it recursively, how do i decide on the base case? i want to say:
if (index 0 is the only index then it is max)
else...
but i think that may be screwy logic.

please advise

Apr 3 '06 #1
2 1792

Well for me I d like to write sth like

foo(...)
{
if (it s the only one)
return this one;
else
return this one > foo (the other guys) ? this one : foo (the other
guys);
}

"andrew browning" <ah**@mac.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
if i write a function to find the max value of an array and want to
write it recursively, how do i decide on the base case? i want to say:
if (index 0 is the only index then it is max)
else...
but i think that may be screwy logic.

please advise

Apr 3 '06 #2
In article <11**********************@i40g2000cwc.googlegroups .com>,
"andrew browning" <ah**@mac.com> wrote:
if i write a function to find the max value of an array and want to
write it recursively, how do i decide on the base case? i want to say:
if (index 0 is the only index then it is max)
else...
but i think that may be screwy logic.

please advise


The general rule when making a loop recursive is don't. Loops are
usually faster, usually take up less space in RAM, and are usually easer
to understand and modify.

To create a recursive function, first solve for the case where no
recursion is necessary:

int max( int* arr, unsigned sz ) {
int result = arr[0];
return result;
}

Then test to see if recursion is necessary. If it is, change the
variable that you tested against to be one step closer to the proper
value and recall the function:

int max( int* arr, unsigned sz ) {
int result = arr[0];
if ( sz != 1 ) {
--sz;
result = arr[sz] > max( arr, sz ) ? arr[sz] : max( arr, sz );
}
return result;
}

Now compare the above to a loop that does the same thing:

int max( int* arr, unsigned sz ) {
int result = arr[0];
while ( sz != 1 ) {
--sz;
result = arr[sz] > result ? arr[sz] : result;
}
return result;
}

When changing from loop to recursion, the 'while' becomes an 'if' and
the references to the partial result inside the 'while' become recursive
calls inside the 'if'.

Of course, I recommend you use std::max_element instead (which is
implemented as a loop on my system. :-)
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: csx | last post by:
Hi all, I'm trying to count the number of leafnodes for a particular node. What im trying to do is make a function, that taking the tree structure: key row desc parent 1 1 A ...
5
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really...
7
by: grawsha2000 | last post by:
Hi, I'm designing a simple database for filing system: There are two levels of files (both look_up tables): tlkpFile1, tlkpSubFile1 and a transaction table, tblFilings, for filings (when...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
6
by: fool | last post by:
Dear group, Given a string I have to print the permutation, using some looping tricks. This is not a Home work problem. My best try as beginner is: #include<stdio.h> #include<stdlib.h> ...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
3
by: JWest46088 | last post by:
Hello everybody. I'm having a little trouble using recursion. I need to accept input from the user and display the input in a square. It has to be done recursively. I would be able to do it...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.