473,327 Members | 2,069 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,327 software developers and data experts.

need help to build a recursive function

5
hello.

I have a problem to build a recursive function
The function gets an array of prices, an array of weight and and spesific Price.
the output's function is the minimum weighet of the subset and the summry of the subset values is equall or bigger then the specific Price.
help me to build the function in a recursive way. (C)

thank you
Mar 21 '08 #1
4 1492
weaknessforcats
9,208 Expert Mod 8TB
Any recursion can be replaced by a loop.

Therefore, get your code working using a loop.

Then write a function that returns when the loop conditon is met. Otherwise, perform the loop instructions, adjuct the cycle counter and call the function again.
Mar 21 '08 #2
adato
5
I have already done that

I cant change the loop
I dont know what is my stopping terms
Mar 21 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
If you have this loop:

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i <10; ++1)
  2. {
  3.     printf("Hello\n");   
  4. }
  5.  
Then you can write this recursive function:
Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int arg)
  2. {
  3.     if (arg <10)
  4.    {
  5.         printf("Hello\n"); 
  6.         MyFunction(++arg);  
  7.    }
  8.    return;
  9. }
  10. main()
  11. {
  12.    MyFunction(0);
  13. }
  14.  
Do you see how the loop appears inthe recursive function?
Mar 21 '08 #4
JosAH
11,448 Expert 8TB
If you have this loop:

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i <10; ++1)
  2. {
  3.     printf("Hello\n");   
  4. }
  5.  
Then you can write this recursive function:
Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int arg)
  2. {
  3.     if (arg <10)
  4.    {
  5.         printf("Hello\n"); 
  6.         MyFunction(++arg);  
  7.    }
  8.    return;
  9. }
  10. main()
  11. {
  12.    MyFunction(0);
  13. }
  14.  
Do you see how the loop appears inthe recursive function?
Yep, but you are showing a 'primitive recursive' function here; even a simpler
'tail recursive' function. If you want to try it with a 'total recursive' function you
at least need an explicit stack so a recursive solution is as good and even more
efficient as an iterative solution. Try this one (the Ackermann function):

Expand|Select|Wrap|Line Numbers
  1. int A(int m, int n) {
  2.    if (!m) return n+1;
  3.    if (!n) return A(m-1, n);
  4.    return A(m-1, A(m, n-1));
  5. }
  6.  
kind regards,

Jos
Mar 21 '08 #5

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

Similar topics

2
by: actuary77 | last post by:
I am trying to write simple recursive function to build a list: def rec(n,alist=): _nl=alist print n,_nl if n == 0: print n,_nl return _nl else:
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
9
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
0
by: Michael L | last post by:
Hi Guys(I apologize for the lengty post - Im trying to explain it as best i can) I've been cracking my head on this one for the past 24+ hours and i have tried creating the function in ten...
4
by: so.intech | last post by:
for example, ret = 0; for(i=0; i<3; i ++;) { for(j=0; j<4; j++;) { for(k=0; k<3; k++;) { for(m=0; m<4; m++;) {
2
by: Anders B | last post by:
I want to make a program that reads the content of a LUA array save file.. More precicely a save file from a World of Warcraft plugin called CharacterProfiler, which dumps alot of information about...
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.