473,396 Members | 1,982 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.

code error with a recursivefunction

Thing I want is find the largest sum down a triangle,(moving to adjacent numbers on the row below )there are many methods to go down.
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
I wrote a programme with a recursive() called finder.but it dose not work properly,at run time it becomes to a infinite status.please help me to detect the error at runtime.here is the code.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void finder(int x,int y);
  3. int tot;
  4.     int a[15][15]={{75},{95, 64},{17, 47 ,82},{18 ,35 ,87 ,10},{20, 04, 82 ,47 \
  5.     ,65},{19 ,01, 23 ,75, 03, 34},{88 ,2, 77 ,73, 7, 63, 67},{99 ,65, 4 ,28, 6,\
  6.  16, 70, 92},{41 ,41 ,26, 56 ,83 ,40, 80 ,70, 33},{41, 48, 72 ,33 ,47 ,32, 37\
  7.   ,16, 94 ,29},{53 ,71, 44, 65, 25 ,43 ,91, 52, 97 ,51 ,14},{70 ,11 ,33 ,28 ,77\
  8.  ,73 ,17 ,78 ,39 ,68 ,17 ,57},{91 ,71 ,52 ,38 ,17 ,14 ,91 ,43 ,58, 50, 27 ,29,48},{63 ,66\
  9.   ,4 ,68 ,89 ,53, 67, 30, 73 ,16 ,69 ,87 ,40 ,31},{4 ,62 ,98 ,27 ,23, 9 ,70 ,98, 73, 93 ,38, 53, 60, 4, 23}};
  10. int main(){
  11.     tot=a[0][0];
  12. finder(0,1);
  13. }
  14. void finder(int x,int y){
  15.     int i;static int max=0;
  16. if(y==14){
  17.     for(i=x;i<=x+1;i++){
  18.         tot+=a[y][i];printf("%i\n",tot);
  19.         if(max<tot){max=tot;}
  20.         tot-=a[y][i];y--;
  21.     }
  22. }
  23.     for(i=x;i<=x+1;i++){
  24.         tot+=a[y][i];
  25.         finder(i,++y);
  26.         tot-=a[y][i];y--;
  27.         }    
  28. }
i think the error is the changing value of x after a round of for loop.how can i fix it?
Sep 29 '13 #1

✓ answered by nilushika

I was able to find the errors,there must come a "else" after
"if",and also don't need to y--;now it works:)
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void finder(int x,int y);
  3. int max=0;
  4. int tot;
  5.    int a[15][15]={{75},{95, 64},{17, 47 ,82},{18 ,35 ,87 ,10},{20, 04, 82 ,47 \
  6.    ,65},{19 ,01, 23 ,75, 03, 34},{88 ,2, 77 ,73, 7, 63, 67},{99 ,65, 4 ,28, 6,\
  7.  16, 70, 92},{41 ,41 ,26, 56 ,83 ,40, 80 ,70, 33},{41, 48, 72 ,33 ,47 ,32, 37\
  8.   ,16, 94 ,29},{53 ,71, 44, 65, 25 ,43 ,91, 52, 97 ,51 ,14},{70 ,11 ,33 ,28 ,77\
  9.  ,73 ,17 ,78 ,39 ,68 ,17 ,57},{91 ,71 ,52 ,38 ,17 ,14 ,91 ,43 ,58, 50, 27 ,29,48},{63 ,66\
  10.   ,4 ,68 ,89 ,53, 67, 30, 73 ,16 ,69 ,87 ,40 ,31},{4 ,62 ,98 ,27 ,23, 9 ,70 ,98, 73, 93 ,38, 53, 60, 4, 23}};
  11. int main(){
  12.    tot=a[0][0];
  13.     finder(0,1);
  14.     printf("%i",max);
  15. }
  16. void finder(int x,int y){
  17.    int i,temp=x;
  18.  if(y==14){
  19.    for(i=x;i<=x+1;i++){
  20.       tot=tot+a[y][i];
  21.       if(max<tot){max=tot;}
  22.       tot=tot-a[y][i];
  23.    }
  24.   }
  25. else{
  26.       for(i=x;i<=x+1;i++){
  27.       tot=tot+a[y][i];
  28.       finder(i,y+1);
  29.       tot=tot-a[y][i];
  30.       }    
  31.   }
  32. }

2 1306
weaknessforcats
9,208 Expert Mod 8TB
When does finder return?

I would expect to see code at the beginning of finder which causes finder to return.

All recursion is able to be coded as a loop. All loops need to know when to stop by testing a condition. So if a loop stops when a variable is zero, then written as a recursion, the function should test the condition at the beginning and return immediately if the variable is zero.
Sep 29 '13 #2
I was able to find the errors,there must come a "else" after
"if",and also don't need to y--;now it works:)
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void finder(int x,int y);
  3. int max=0;
  4. int tot;
  5.    int a[15][15]={{75},{95, 64},{17, 47 ,82},{18 ,35 ,87 ,10},{20, 04, 82 ,47 \
  6.    ,65},{19 ,01, 23 ,75, 03, 34},{88 ,2, 77 ,73, 7, 63, 67},{99 ,65, 4 ,28, 6,\
  7.  16, 70, 92},{41 ,41 ,26, 56 ,83 ,40, 80 ,70, 33},{41, 48, 72 ,33 ,47 ,32, 37\
  8.   ,16, 94 ,29},{53 ,71, 44, 65, 25 ,43 ,91, 52, 97 ,51 ,14},{70 ,11 ,33 ,28 ,77\
  9.  ,73 ,17 ,78 ,39 ,68 ,17 ,57},{91 ,71 ,52 ,38 ,17 ,14 ,91 ,43 ,58, 50, 27 ,29,48},{63 ,66\
  10.   ,4 ,68 ,89 ,53, 67, 30, 73 ,16 ,69 ,87 ,40 ,31},{4 ,62 ,98 ,27 ,23, 9 ,70 ,98, 73, 93 ,38, 53, 60, 4, 23}};
  11. int main(){
  12.    tot=a[0][0];
  13.     finder(0,1);
  14.     printf("%i",max);
  15. }
  16. void finder(int x,int y){
  17.    int i,temp=x;
  18.  if(y==14){
  19.    for(i=x;i<=x+1;i++){
  20.       tot=tot+a[y][i];
  21.       if(max<tot){max=tot;}
  22.       tot=tot-a[y][i];
  23.    }
  24.   }
  25. else{
  26.       for(i=x;i<=x+1;i++){
  27.       tot=tot+a[y][i];
  28.       finder(i,y+1);
  29.       tot=tot-a[y][i];
  30.       }    
  31.   }
  32. }
Oct 8 '13 #3

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

Similar topics

1
by: Amit Kela | last post by:
Hey, I am getting the following error when I try to delete the contents of a recordset under a condition statement. Is there any way I can delete them without running into this error? I need to...
6
by: geronimo_me | last post by:
Hi, I am trying to run an Excel macro from an Access module, however when I run the code the macro runs but then I get an error in Access. The error is: Run-time error "440", Automation error. ...
4
by: yanyo | last post by:
hi, im trying to figure out whats the problem with this program i get a runtime error but i dont see where the problem is i tried changing declaration but nothing if somrbody can try this on their...
4
by: SteadySteps | last post by:
Hi I migrated a project which compiles correctly on VC 6.0 to VS 2002. However now all I get several warning that all the statements within catch blocks are "unreachable code". How can I correct...
8
by: Doug Bell | last post by:
Hi, I have been debugging a new VB.Net Application and today, I have been getting an error that I have not seen before This error is now appearing on a line with the following code: lnGUID =...
1
by: xian2 | last post by:
Hi All, I have been using this guide http://www.compasscomputing.co.uk/code/AcceesReportsToPDFandEmail.htm to create a command button that automatically saves and e-mails your report and have...
7
by: JewelsT | last post by:
Hello, I'm new to both RSS & XML and I am trying to create a ColdFusion RSS feed. I have my cfm file with all my code and at the end of the file I am writing that output to an XML file. At the top...
1
by: TSalm | last post by:
Hi, After sending a request, I would get the possible error message. Not the code @@error, nor the exact content of sysmessages, but the message like it could be in the log file. TIA, TSalm
13
by: problem. | last post by:
#include <stdio.h> #include <stdlib.h> int main(void) { int a, b; float result = 0.0f; char symbol = '\0'; int loop = 0;
0
by: vagueante | last post by:
Hi, I have a windows service that i connecto to a iSeries Dataqueue, i can write and read from it. The purpose of this service it's to keep reading the dataqueue and when it has some data i run...
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: 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
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.