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.
9 2026
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 - x is relevant data
-
_ is 0 or unititialized - not accessed, irrelevant
-
-
x _ _ _
-
x x _ _
-
x x x _
-
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?
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?
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.
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?
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
thanx paul.
ill be working on the solution.
BTW, how did this thing strike you????
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
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.
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. -
#include <iostream>
-
using namespace std;
-
-
void printseries(int start, int end);
-
void branchingseries(int start, int end);
-
-
int main(int argc, char **argv)
-
{
-
printseries(2,8);
-
branchingseries(2,8);
-
}
-
-
void printseries(int start, int end) {
-
// don't do anything if this isn't an ascending series
-
if (start > end)
-
return;
-
if (start == end) {
-
// terminate recursion if we're at the end
-
cout << end << endl;
-
return;
-
}
-
// if in the middle of the series, print the
-
// current item in the series and keep counting
-
cout << start << ",";
-
printseries(start+1, end);
-
}
-
-
void branchworker(int count, int limit, int start, int total)
-
{
-
cout << start;
-
if (count == limit)
-
{
-
// if we've reached the end, print
-
// the end of the line and terminate recursion
-
cout << ": " << (start+total) << endl;
-
return;
-
}
-
cout << ','; // we're going on, so print our comma now
-
branchworker(count+1, limit, start, total+start);
-
cout << ',';
-
branchworker(count+1, limit, start+1, total+start);
-
}
-
-
void branchingseries(int start, int end)
-
{
-
// don't do anything if this isn't an ascending series
-
if (start > end)
-
return;
-
branchworker(0, end-start, start, 0);
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
by: Richard Hayden |
last post by:
Hi,
I have the following code:
/******************************** file1.c
#include <iostream>
extern void dummy();
inline int testfunc() {
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
| |