The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 05-09-2008, 08:41 AM
kckc314 kckc314 is offline
Novice
Join Date: Apr 2008
Posts: 14
iTrader: (0)
kckc314 is on a distinguished road
recursion function in C

what's wrong with the following code as it is not working.
Code:
if (c> dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ') {
   if (sq[r][c] == ' ' && sq[r][c + 1] != ' ') {
      spmtn1(r, c - 1);
   }
}
if (c> dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ') {
   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] != ' ') { -->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
and this one also as it is not working.
Code:
if (c> dCol && ((sq[r][c - 1] != '#' && sq[r][c - 1] != ' ') || (sq[r + 1][c] != '#' && sq[r + 1][c] != ' ') || (sq[r][c + 1] != '#' && sq[r][c + 1] != ' ') || (sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')) && sq[r][c - 1] <= sq[r + 1][c] && sq[r][c - 1] <= sq[r][c + 1] && sq[r][c - 1] <= sq[r - 1][c])
   spmw1(r, c - 1);
else
if (c < dCol && ((sq[r][c + 1] != '#' && sq[r][c + 1] != ' ') || (sq[r - 1][c] != '#' && sq[r - 1][c] != ' ') || (sq[r][c - 1] != '#' && sq[r][c - 1] != ' ') || (sq[r + 1][c] != '#' && sq[r + 1][c] != ' ')) && sq[r][c + 1] <= sq[r - 1][c] && sq[r][c + 1] <= sq[r][c - 1] && sq[r][c + 1] <= sq[r + 1][c])
   spmw1(r, c + 1);
else
if

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 05-09-2008, 09:17 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Full Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 247
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
What is it meant to do? We need to see how the variables are defined too, there may be something wrong there. At the moment we can't help you much

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to MrPickle For This Useful Post:
TeraTask (05-09-2008)
  #3 (permalink)  
Old 05-09-2008, 06:04 PM
kckc314 kckc314 is offline
Novice
Join Date: Apr 2008
Posts: 14
iTrader: (0)
kckc314 is on a distinguished road
this is the complete part function which is related to the above first part of the code.
Code:
void spmtn1(int r, int c) {
     sq[r][c] = ' '; /* Mark the square we have visited so that it won't be visited again */
     /* 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 < spmtn[m]) {
           spmtn[m] = ctr1;
           printf("Shortest monotone path: %d\n", spmtn[m]);
           return;
        }
     }
     else
         ctr1++;
     /* To compute the minimal monotone paths from the origin point to destination point *
      * 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 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 (c > dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ') {
        if (sq[r][c] == ' ' && sq[r][c + 1] != ' ') {
           spmtn1(r, c - 1);
        }
     }
     if (c > dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ') {
        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] != ' ') {
        if (sq[r][c] == ' ' && sq[r][c - 1] != ' ') {
           spmtn1(r, c + 1);
        }
     }
     if (c < dCol && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ') {
        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] != ' ') {
        if (sq[r][c] == ' ' && sq[r + 1][c] != ' ') {
           spmtn1(r - 1, c);
        }
     }
     if (r > dRow && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ') {
        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] != ' ') {
        if (sq[r][c] == ' ' && sq[r - 1][c] != ' ') {
           spmtn1(r + 1, c);
        }
     }
     if (r < dRow && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ') {
        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 has been stopped by '#' or marked square that has been visited, so move in any direction we can */
          if (r + 1 < row && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ')
             spmtn1(r + 1, c);
          else
          if (r - 1 > 0 && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
             spmtn1(r - 1, c);
          else
          if (c + 1 < col && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
             spmtn1(r, c + 1);
          else
          if (c - 1 > 0 && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
             spmtn1(r, c - 1);
     }
     
     /* No monotone path exists */
     if (r + 1 < row && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ')
        spmtn1(r + 1, c);
     else
     if (r - 1 > 0 && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
        spmtn1(r - 1, c);
     else
     if (c + 1 < col && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
        spmtn1(r, c + 1);
     else
     if (c - 1 > 0 && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
        spmtn1(r, c - 1);
     else
         printf("No monotone path exists\n");
}
And this is the complete part function which is related to the above second part of the code.
Code:
void spmw1(int r, int c) {
     sq[r][c] = ' '; /* Mark the square we have visited so that it won't be visited again */
     /* Upon the destination point is found, check the current value of ctr2 with the value in *
      * the array, if the current value of ctr2 is less, update it and display message together *
      * with the minimal weights information */
     if (r == dRow && c == dCol) {
        if (ctr2 < spmw[n]) {
           spmw[n] = ctr2;
           printf("Shortest path of minimal weight: %d -- weight %d\n", spmw[n], mw);
           return;
        }
     }
     else {
         mw += sq[r][c];
         ctr2++;
         vpath[ctr2][0] = r;
         vpath[ctr2][1] = c;
     }
     /* To compute the minimal paths of minimal weights from the origin point to destination point *
      * First check the four surrounding points of origin point, up, down, left and right to ensure *
      * one or more point is not equal to '#' and marked square *
      * if (c < dCol) statement is evaluated to true, the next priority moving point to will be (r, c + 1) *
      * provided that it is not equal to '#', marked square and has the least or equal value among the *
      * surrounding points, otherwise it will move to different direction either horizontally or vertically. *
      * So do other if statements. */
     if (c > dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ' || sq[r + 1][c] != '#' && sq[r + 1][c] != ' ' ||
        sq[r][c + 1] != '#' && sq[r][c + 1] != ' ' || sq[r - 1][c] != '#' && sq[r - 1][c] != ' ' &&
        sq[r][c - 1] <= sq[r + 1][c] && sq[r][c - 1] <= sq[r][c + 1] && sq[r][c - 1] <= sq[r - 1][c])
        spmw1(r, c - 1);
     else
     if (c < dCol && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ' || sq[r - 1][c] != '#' && sq[r - 1][c] != ' ' ||
        sq[r][c - 1] != '#' && sq[r][c - 1] != ' ' || sq[r + 1][c] != '#' && sq[r + 1][c] != ' ' && 
        sq[r][c + 1] <= sq[r - 1][c] && sq[r][c + 1] <= sq[r][c - 1] && sq[r][c + 1] <= sq[r + 1][c])
        spmw1(r, c + 1);
     else
     if (r > dRow && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ' || sq[r][c - 1] != '#' && sq[r][c - 1] != ' ' ||
        sq[r + 1][c] != '#' && sq[r + 1][c] != ' ' || sq[r][c + 1] != '#' && sq[r][c + 1] != ' ' &&
        sq[r - 1][c] <= sq[r][c - 1] && sq[r - 1][c] <= sq[r + 1][c] && sq[r - 1][c] <= sq[r][c + 1])
        spmw1(r - 1, c);
     else
     if (r < dRow && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ' || sq[r][c + 1] != '#' && sq[r][c + 1] != ' ' ||
        sq[r - 1][c] != '#' && sq[r - 1][c] != ' ' || sq[r][c - 1] != '#' && sq[r][c - 1] != ' ' &&
        sq[r + 1][c] <= sq[r][c + 1] && sq[r + 1][c] <= sq[r - 1][c] && sq[r + 1][c] <= sq[r][c - 1])
        spmw1(r + 1, c);
     else {
          /* If has been stopped by '#' or marked square that has been visited, so move in any direction we can */
          if (r + 1 < row && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ')
             spmw1(r + 1, c);
          else
          if (r - 1 > 0 && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
             spmw1(r - 1, c);
          else
          if (c + 1 < col && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
             spmw1(r, c + 1);
          else
          if (c - 1 > 0 && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
             spmw1(r, c - 1);
     }
     
     /* When you reach a point will not be able to move anywhere else, this will cause the *
      * backtracking to execute */
     if (r + 1 < row && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ')
        spmw1(r + 1, c);
     else
     if (r - 1 > 0 && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
        spmw1(r - 1, c);
     else
     if (c + 1 < col && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
        spmw1(r, c + 1);
     else
     if (c - 1 > 0 && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
        spmw1(r, c - 1);
     else {
          ctr2--;
          if (ctr2 <= 0)
             printf("No path exists\n");
          else
              spmw1(vpath[ctr2][0], vpath[ctr2][1]);
     }
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 05-09-2008, 06:15 PM
MrPickle's Avatar
MrPickle MrPickle is offline
Full Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 247
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
From what I can gather you're trying to go through a multidimensional array and check if any of the points are equal to "#" or blank.

If you know the size of the array then it would probably be easier to use 2 for loops.
eg:
Code:
for(int a = 0; a < r; a++)
{
   for(int b = 0; b < c; b++)
   {
      //Perform whatever checks here
   }
}
I think this way would be faster too, don't quote me on that though.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 05-09-2008, 07:04 PM
kckc314 kckc314 is offline
Novice
Join Date: Apr 2008
Posts: 14
iTrader: (0)
kckc314 is on a distinguished road
i understand what you are saying. but in my 2-d array, there are some criterias to meet, i am not just checking it row by row and column by column till the end. if that's the case then i can use 2 nested for loops. according to the criterias, first if i go with a row or column, if i hit '#' or marked empty space then i have to turn horizontally or vertically within the square 2d array. that's why i find recursion function and bunch of if else statements best suit for this situation.

now my concern is, on the very first message that i posted, are there any errors related to basic concept in C? coz it doesn't do any job and produce output.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 12:49 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50