473,385 Members | 1,893 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,385 software developers and data experts.

recursions in functions

3
hi guys,
i am a newbie and learning c++ currently.
i know functions and all the basic stuff.

I was recently given this problem and need a solution.

a triangle is inputted in the form of lower triangular matrix in a 2d array.
the matrix may contain negative numbers.
u have to find the maximum sum from all possible routes going top to bottom.
each time u can either move down or move diagonally bottom right.

eg.

2
4 5
100 -80 12
20 1400 99

here the maximum sum is 1327.

my teacher has recommended me to use recursions in functions.(where u call a function inside itself).

if u have any other logic that would do.

thanx in advance.


ishan.
Jan 18 '07 #1
9 2026
Ganon11
3,652 Expert 2GB
I think you need to explain your problem more fully. Let me try to sort out the information you have given us:

You will have a 2D int matrix filled with values in a triangular pattern, like

Expand|Select|Wrap|Line Numbers
  1. x is relevant data
  2. _ is 0 or unititialized - not accessed, irrelevant
  3.  
  4. x _ _ _
  5. x x _ _
  6. x x x _
  7. x x x x
You want to find the path from top to bottom (moving only down a.k.a. array[0][0] to array[1][0] or moving down-and-right a.k.a. array[0][0] to array[1][1]) that results in the largest sum, and report that sum.

You want to use a recursive function to determine this.

Is all this correct? Is there any other information that might help us help you?
Jan 18 '07 #2
Also, complementing what Ganon11 said, if his interpretation is correct (which was my interpretation too), your example seems incorrect.

eg.

2
4 5
100 -80 12
20 1400 99

here the maximum sum is 1327.
Why can't you get:
2->4->100->1400, getting a sum of 1506, thus greater than 1327.

Why isn't this sequence valid?
Jan 18 '07 #3
ishan
3
sorry guys.
your interpretetion is correct.
the maximum sum is not what i mentioned.
i was just trying to show that sometimes having a negative value in your 'path' can also lead to maximum sum and thus u have to check all possible ways.

now can u guys help me with program.
Jan 21 '07 #4
Ganon11
3,652 Expert 2GB
I'm afraid that I don't understand the problem at all, then. Could you explain it in a little more detail so we could have an idea as to where to start?
Jan 21 '07 #5
macklin01
145 100+
I think I would represent each path as a seqence of 0s and 1s. 0 represents straight down, and 1 represents down and to the right.

Think about your 4 x 4 example: the possible sequences are:

{0,0,0}
{0,0,1}
{0,1,0}
{0,1,1}
{1,0,0}
{1,0,1}
{1,1,1}

Do you see the natural ordering here? It's like going from 2^0 to 2^3-1 in binary. You simply iterate through all these paths to find the maximum number. No recursion is necessary. First, generate a sequence. Suppose we write it as

int Path[3];
Path[0] = 0;
Path[1] = 1;
Path[2] = 1;

In our example we go straight down, then diagonal twice. This path gives us the following matrix entries:

Matrix(0,0)
Matrix(1,0)
Matrix(2,1)

Notice that the notation even makes traversing the path easy to do in a very natural way. (Think of tracking a current row and current column, and each path step either increments or holds the column.) -- Paul
Jan 21 '07 #6
ishan
3
thanx paul.
ill be working on the solution.
BTW, how did this thing strike you????
Jan 22 '07 #7
macklin01
145 100+
thanx paul.
ill be working on the solution.
BTW, how did this thing strike you????
I'm not sure what you meant by this.

At any rate, how did it turn out? -- Paul
Jan 26 '07 #8
I'm afraid that I don't understand the problem at all, then. Could you explain it in a little more detail so we could have an idea as to where to start?
Hi I am new to recursions, my teacher wants me to use recursions to produce an out put 2,3,4,5,...N
this is the function prototype
int addint(int i, int N);
// hint: can use auxillary recursive function.
my thought process was:
assert (i<N);
if N = 0;
return 0;
i =2;
addone(i);
cout << i;
......
addone(int x){
return x+=1;
....
am i on the right track?my doubt is, recursion is to break the problem into smaller problems, i feel my code just calls another function repetitively to do the job.
what is your feedback?
thnx in advance.
Feb 2 '07 #9
nmadct
83 Expert
You're right that recursion helps by breaking up a problem into smaller steps. I've written a program that shows how to break up the counting problem into a recursive function.

Following the function that solves that problem, I've included a very similar function that counts through the series, but at each step it has the option of making the next number in the sequence the same value instead of increasing it by one.

I think you'll see that this ends up being very similar to the "triangle" problem. It ends up branching out into more paths of execution as it proceeds. The output isn't completely intuitive at first but I think if you follow the code it will become clear what it's showing.

At the end of each line, it prints the total that the series adds up to.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void printseries(int start, int end);
  5. void branchingseries(int start, int end);
  6.  
  7. int main(int argc, char **argv)
  8. {
  9.         printseries(2,8);
  10.         branchingseries(2,8);
  11. }
  12.  
  13. void printseries(int start, int end) {
  14.         // don't do anything if this isn't an ascending series
  15.         if (start > end)
  16.                 return;
  17.         if (start == end) {
  18.                 // terminate recursion if we're at the end
  19.                 cout << end << endl;
  20.                 return;
  21.         }
  22.         // if in the middle of the series, print the
  23.         // current item in the series and keep counting
  24.         cout << start << ",";
  25.         printseries(start+1, end);
  26. }
  27.  
  28. void branchworker(int count, int limit, int start, int total)
  29. {
  30.         cout << start;
  31.         if (count == limit)
  32.         {
  33.                 // if we've reached the end, print
  34.                 // the end of the line and terminate recursion
  35.                 cout << ": " << (start+total) << endl;
  36.                 return;
  37.         }
  38.         cout << ','; // we're going on, so print our comma now
  39.         branchworker(count+1, limit, start, total+start);
  40.         cout << ',';
  41.         branchworker(count+1, limit, start+1, total+start);
  42. }
  43.  
  44. void branchingseries(int start, int end)
  45. {
  46.         // don't do anything if this isn't an ascending series
  47.         if (start > end)
  48.                 return;
  49.         branchworker(0, end-start, start, 0);
  50. }
  51.  
Feb 2 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.