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. - #include<stdio.h>
-
void finder(int x,int y);
-
int tot;
-
int a[15][15]={{75},{95, 64},{17, 47 ,82},{18 ,35 ,87 ,10},{20, 04, 82 ,47 \
-
,65},{19 ,01, 23 ,75, 03, 34},{88 ,2, 77 ,73, 7, 63, 67},{99 ,65, 4 ,28, 6,\
-
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\
-
,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}};
-
int main(){
-
tot=a[0][0];
-
finder(0,1);
-
}
-
void finder(int x,int y){
-
int i;static int max=0;
-
if(y==14){
-
for(i=x;i<=x+1;i++){
-
tot+=a[y][i];printf("%i\n",tot);
-
if(max<tot){max=tot;}
-
tot-=a[y][i];y--;
-
}
-
}
-
for(i=x;i<=x+1;i++){
-
tot+=a[y][i];
-
finder(i,++y);
-
tot-=a[y][i];y--;
-
}
-
}
i think the error is the changing value of x after a round of for loop.how can i fix it?
I was able to find the errors,there must come a "else" after
"if",and also don't need to y--;now it works:) - #include<stdio.h>
-
void finder(int x,int y);
-
int max=0;
-
int tot;
-
int a[15][15]={{75},{95, 64},{17, 47 ,82},{18 ,35 ,87 ,10},{20, 04, 82 ,47 \
-
,65},{19 ,01, 23 ,75, 03, 34},{88 ,2, 77 ,73, 7, 63, 67},{99 ,65, 4 ,28, 6,\
-
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\
-
,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}};
-
int main(){
-
tot=a[0][0];
-
finder(0,1);
-
printf("%i",max);
-
}
-
void finder(int x,int y){
-
int i,temp=x;
-
if(y==14){
-
for(i=x;i<=x+1;i++){
-
tot=tot+a[y][i];
-
if(max<tot){max=tot;}
-
tot=tot-a[y][i];
-
}
-
}
-
else{
-
for(i=x;i<=x+1;i++){
-
tot=tot+a[y][i];
-
finder(i,y+1);
-
tot=tot-a[y][i];
-
}
-
}
-
}
2 1306
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.
I was able to find the errors,there must come a "else" after
"if",and also don't need to y--;now it works:) - #include<stdio.h>
-
void finder(int x,int y);
-
int max=0;
-
int tot;
-
int a[15][15]={{75},{95, 64},{17, 47 ,82},{18 ,35 ,87 ,10},{20, 04, 82 ,47 \
-
,65},{19 ,01, 23 ,75, 03, 34},{88 ,2, 77 ,73, 7, 63, 67},{99 ,65, 4 ,28, 6,\
-
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\
-
,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}};
-
int main(){
-
tot=a[0][0];
-
finder(0,1);
-
printf("%i",max);
-
}
-
void finder(int x,int y){
-
int i,temp=x;
-
if(y==14){
-
for(i=x;i<=x+1;i++){
-
tot=tot+a[y][i];
-
if(max<tot){max=tot;}
-
tot=tot-a[y][i];
-
}
-
}
-
else{
-
for(i=x;i<=x+1;i++){
-
tot=tot+a[y][i];
-
finder(i,y+1);
-
tot=tot-a[y][i];
-
}
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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.
...
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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
|
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;
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |