Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 86,241 C++ Programmers. There are 2,265 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

stack overflow

 
Reply to this topicStart new topic

stack overflow

kckc314
post 7 May, 2008 - 10:10 PM
Post #1


D.I.C Head

**
Joined: 5 Apr, 2008
Posts: 75



this whole part of code stack overflow. My intention was if this line "if (c > dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')" is false then jump to the next else if statement. If this line "if (c> dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')" is true, when the origin point start moving or when turning point takes place, this line "if (sq[r][c] == ' ' && sq[r][c + 1] != ' ')" will always be true, IE execute this line "spmtn1(r, c - 1)". when the point moves some where in the middle of the line, no turning takes place, then this line "if (sq[r][c] == ' ' && sq[r][c + 1] == ' ')" will become true, the previous one will become false. then do the check on this line "if ((sq[r][c]>= sq[r][c + 1] && sq[r][c - 1]>= sq[r][c]) || (sq[r][c] <= sq[r][c + 1] && sq[r][c - 1] <= sq[r][c]))", if it's true then execute this line "spmtn1(r, c - 1);", if it's false then jump to next else if statement.
CODE

void spmtn1(int r, int c) {
     sq[r][c] = ' '; /* Mark the square we have visited */

     /* Upon the destination point is found, check the current value of ctr1 with the value in *
      * the array, if the current value of ctr1 is less, update it and display message */
     if (r == dRow && c == dCol) {
        if (ctr1 < sp[m]) {
           sp[m] = ctr1;
           printf("Shortest monotone path: %d\n", sp[m]);
           return;
        }
     }
     else
         ctr1++;

     /* To find the shortest monotone path from the origin point to destination point */
     if (c > dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
        /* Either the point before the origin point or before the turning point has not been visited, *
         * only the origin point or turning point has been visited, so the next point it moving to can be *
         * less than or greater than or equal to the origin or turning point */
        if (sq[r][c] == ' ' && sq[r][c + 1] != ' ')
           spmtn1(r, c - 1);
        /* If the previous two points have been visited, then check if the previous point is greater than or equal *
         * to the one before then the next point it moving to must be greater than or equal to the previous point, *
         * so does less than or equal to. If not then the line will turn vertically or horizontally to next direction */
        if (sq[r][c] == ' ' && sq[r][c + 1] == ' ')
           if ((sq[r][c] >= sq[r][c + 1] && sq[r][c - 1] >= sq[r][c]) || (sq[r][c] <= sq[r][c + 1] && sq[r][c - 1] <= sq[r][c]))
              spmtn1(r, c - 1);
     else
     if (c < dCol && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
        /* Either the point before the origin point or before the turning point has not been visited, *
         * only the origin point or turning point has been visited, so the next point it moving to can be *
         * less than or greater than or equal to the origin or turning point */
        if (sq[r][c] == ' ' && sq[r][c - 1] != ' ')
           spmtn1(r, c + 1);
        /* If the previous two points have been visited, then check if the previous point is greater than or equal *
         * to the one before then the next point it moving to must be greater than or equal to the previous point, *
         * so does less than or equal to. If not then the line will turn vertically or horizontally to next direction */
        if (sq[r][c] == ' ' && sq[r][c - 1] == ' ')
           if ((sq[r][c] >= sq[r][c - 1] && sq[r][c + 1] >= sq[r][c]) || (sq[r][c] <= sq[r][c - 1] && sq[r][c + 1] <= sq[r][c]))
              spmtn1(r, c + 1);
     else
     if (r > dRow && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
        /* Either the point before the origin point or before the turning point has not been visited, *
         * only the origin point or turning point has been visited, so the next point it moving to can be *
         * less than or greater than or equal to the origin or turning point */
        if (sq[r][c] == ' ' && sq[r + 1][c] != ' ')
           spmtn1(r - 1, c);
        /* If the previous two points have been visited, then check if the previous point is greater than or equal *
         * to the one before then the next point it moving to must be greater than or equal to the previous point, *
         * so does less than or equal to. If not then the line will turn vertically or horizontally to next direction */
        if (sq[r][c] == ' ' && sq[r + 1][c] == ' ')
           if ((sq[r][c] >= sq[r + 1][c] && sq[r - 1][c] >= sq[r][c]) || (sq[r][c] <= sq[r + 1][c] && sq[r - 1][c] <= sq[r][c]))
              spmtn1(r - 1, c);
     else
     if (r < dRow && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ')
        /* Either the point before the origin point or before the turning point has not been visited, *
         * only the origin point or turning point has been visited, so the next point it moving to can be *
         * less than or greater than or equal to the origin or turning point */
        if (sq[r][c] == ' ' && sq[r - 1][c] != ' ')
           spmtn1(r + 1, c);
        /* If the previous two points have been visited, then check if the previous point is greater than or equal *
         * to the one before then the next point it moving to must be greater than or equal to the previous point, *
         * so does less than or equal to. If not then the line will turn vertically or horizontally to next direction */
        if (sq[r][c] == ' ' && sq[r - 1][c] == ' ')
           if ((sq[r][c] >= sq[r - 1][c] && sq[r + 1][c] >= sq[r][c]) || (sq[r][c] <= sq[r - 1][c] && sq[r + 1][c] <= sq[r][c]))
              spmtn1(r + 1, c);
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


kckc314
post 8 May, 2008 - 07:13 PM
Post #2


D.I.C Head

**
Joined: 5 Apr, 2008
Posts: 75

QUOTE(kckc314 @ 7 May, 2008 - 10:10 PM) *

this whole part of code stack overflow. My intention was if this line "if (c > dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')" is false then jump to the next else if statement. If this line "if (c> dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')" is true, when the origin point start moving or when turning point takes place, this line "if (sq[r][c] == ' ' && sq[r][c + 1] != ' ')" will always be true, IE execute this line "spmtn1(r, c - 1)". when the point moves some where in the middle of the line, no turning takes place, then this line "if (sq[r][c] == ' ' && sq[r][c + 1] == ' ')" will become true, the previous one will become false. then do the check on this line "if ((sq[r][c]>= sq[r][c + 1] && sq[r][c - 1]>= sq[r][c]) || (sq[r][c] <= sq[r][c + 1] && sq[r][c - 1] <= sq[r][c]))", if it's true then execute this line "spmtn1(r, c - 1);", if it's false then jump to next else if statement.
CODE

void spmtn1(int r, int c) {
     sq[r][c] = ' '; /* Mark the square we have visited */

     /* Upon the destination point is found, check the current value of ctr1 with the value in *
      * the array, if the current value of ctr1 is less, update it and display message */
     if (r == dRow && c == dCol) {
        if (ctr1 < sp[m]) {
           sp[m] = ctr1;
           printf("Shortest monotone path: %d\n", sp[m]);
           return;
        }
     }
     else
         ctr1++;

     /* To find the shortest monotone path from the origin point to destination point */
     if (c > dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
        /* Either the point before the origin point or before the turning point has not been visited, *
         * only the origin point or turning point has been visited, so the next point it moving to can be *
         * less than or greater than or equal to the origin or turning point */
        if (sq[r][c] == ' ' && sq[r][c + 1] != ' ')
           spmtn1(r, c - 1);
        /* If the previous two points have been visited, then check if the previous point is greater than or equal *
         * to the one before then the next point it moving to must be greater than or equal to the previous point, *
         * so does less than or equal to. If not then the line will turn vertically or horizontally to next direction */
        if (sq[r][c] == ' ' && sq[r][c + 1] == ' ')
           if ((sq[r][c] >= sq[r][c + 1] && sq[r][c - 1] >= sq[r][c]) || (sq[r][c] <= sq[r][c + 1] && sq[r][c - 1] <= sq[r][c]))
              spmtn1(r, c - 1);
     else
     if (c < dCol && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
        /* Either the point before the origin point or before the turning point has not been visited, *
         * only the origin point or turning point has been visited, so the next point it moving to can be *
         * less than or greater than or equal to the origin or turning point */
        if (sq[r][c] == ' ' && sq[r][c - 1] != ' ')
           spmtn1(r, c + 1);
        /* If the previous two points have been visited, then check if the previous point is greater than or equal *
         * to the one before then the next point it moving to must be greater than or equal to the previous point, *
         * so does less than or equal to. If not then the line will turn vertically or horizontally to next direction */
        if (sq[r][c] == ' ' && sq[r][c - 1] == ' ')
           if ((sq[r][c] >= sq[r][c - 1] && sq[r][c + 1] >= sq[r][c]) || (sq[r][c] <= sq[r][c - 1] && sq[r][c + 1] <= sq[r][c]))
              spmtn1(r, c + 1);
     else
     if (r > dRow && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
        /* Either the point before the origin point or before the turning point has not been visited, *
         * only the origin point or turning point has been visited, so the next point it moving to can be *
         * less than or greater than or equal to the origin or turning point */
        if (sq[r][c] == ' ' && sq[r + 1][c] != ' ')
           spmtn1(r - 1, c);
        /* If the previous two points have been visited, then check if the previous point is greater than or equal *
         * to the one before then the next point it moving to must be greater than or equal to the previous point, *
         * so does less than or equal to. If not then the line will turn vertically or horizontally to next direction */
        if (sq[r][c] == ' ' && sq[r + 1][c] == ' ')
           if ((sq[r][c] >= sq[r + 1][c] && sq[r - 1][c] >= sq[r][c]) || (sq[r][c] <= sq[r + 1][c] && sq[r - 1][c] <= sq[r][c]))
              spmtn1(r - 1, c);
     else
     if (r < dRow && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ')
        /* Either the point before the origin point or before the turning point has not been visited, *
         * only the origin point or turning point has been visited, so the next point it moving to can be *
         * less than or greater than or equal to the origin or turning point */
        if (sq[r][c] == ' ' && sq[r - 1][c] != ' ')
           spmtn1(r + 1, c);
        /* If the previous two points have been visited, then check if the previous point is greater than or equal *
         * to the one before then the next point it moving to must be greater than or equal to the previous point, *
         * so does less than or equal to. If not then the line will turn vertically or horizontally to next direction */
        if (sq[r][c] == ' ' && sq[r - 1][c] == ' ')
           if ((sq[r][c] >= sq[r - 1][c] && sq[r + 1][c] >= sq[r][c]) || (sq[r][c] <= sq[r - 1][c] && sq[r + 1][c] <= sq[r][c]))
              spmtn1(r + 1, c);


hi guys, i have fixed the stack overflow issues and it doesn't happen for the time being. now the problem is this part of code doesn't do anything. no output solution. pls advise.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pertheusual
post 8 May, 2008 - 08:05 PM
Post #3


D.I.C Head

**
Joined: 26 Jan, 2008
Posts: 212

Okay, first off, it's pretty much impossible to read that code because of the comment density.

Anyway, I'm gonna go ahead and say that your issue is probably due to lack of curly brackets. Your tabbing makes it seem like you have a few large groups of ifs with several ifs inside each one, but you don't have brackets, so they will not be interpreted that way.

Per
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

kckc314
post 8 May, 2008 - 09:56 PM
Post #4


D.I.C Head

**
Joined: 5 Apr, 2008
Posts: 75

QUOTE(pertheusual @ 8 May, 2008 - 08:05 PM) *

Okay, first off, it's pretty much impossible to read that code because of the comment density.

Anyway, I'm gonna go ahead and say that your issue is probably due to lack of curly brackets. Your tabbing makes it seem like you have a few large groups of ifs with several ifs inside each one, but you don't have brackets, so they will not be interpreted that way.

Per

i have grabbed part of the code and change it in the following way:
CODE

if (c < dCol && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ') { -->if this line is false then jump to the next else if, but if it is true
        if (sq[r][c] == ' ' && sq[r][c - 1] != ' ') {-->if this line is true then execute the statement, the below if statements won't be executed, but if it is false
           spmtn1(r, c + 1);
        }
     }
        if (c < dCol && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ') {-->as this line still remain as true, same first if statement as above
        if (sq[r][c] == ' ' && sq[r][c - 1] == ' ') {-->if the above second if statement is false then this line will always be true
           if ((sq[r][c] >= sq[r][c - 1] && sq[r][c + 1] >= sq[r][c]) || (sq[r][c] <= sq[r][c - 1] && sq[r][c + 1] <= sq[r][c])) {-->then check this line, if it is true then execute the statement, if it is false then jump to the next else if
              spmtn1(r, c + 1);
           }
        }
        }
else
if

i think still having errors according to the logic as it still not working. pls advise. thanks in advance.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
Time is now: 5/16/08 08:17AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month