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

Join 135,924 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,609 people online right now. Registration is fast and FREE... Join Now!




functions

2 Pages V  1 2 >  
Reply to this topicStart new topic

functions

kckc314
10 May, 2008 - 01:07 AM
Post #1

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

can i declare several functions like this in C code. after the first functions has finished, then execute the second function, after that execute the subsequent functions. at the moment it's only execute the first function and it won't execute the rest of the functions. pls advise and thanks in advance.

sp1(sRow, sCol);
spmtn1(sRow, sCol);
spmw1(sRow, sCol);
spmtmw1(sRow, sCol);
User is offlineProfile CardPM
+Quote Post

KYA
RE: Functions
10 May, 2008 - 01:27 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,733



Thanked: 89 times
Dream Kudos: 1200
My Contributions
The function should return control back to main() after its execution. Is it a void function or does it return something? Post code smile.gif
User is online!Profile CardPM
+Quote Post

kckc314
RE: Functions
10 May, 2008 - 01:41 AM
Post #3

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

QUOTE(KYA @ 10 May, 2008 - 02:27 AM) *

The function should return control back to main() after its execution. Is it a void function or does it return something? Post code smile.gif

they are all void function. in the first function, the last execution is to print a msg printf(), after this statement i have put a return; statement. should i put return; or return(sRow, sCol) to bring the arguments back for the second function. or before the return; statement i have to include this statement "void spmtn1(sRow, sCol);" to call the second function from inside first function?

void sp1(sRow, sCol);
void spmtn1(sRow, sCol);
void spmw1(sRow, sCol);
void spmtmw1(sRow, sCol);
User is offlineProfile CardPM
+Quote Post

KYA
RE: Functions
10 May, 2008 - 02:38 AM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,733



Thanked: 89 times
Dream Kudos: 1200
My Contributions
void functions do not have return statements
User is online!Profile CardPM
+Quote Post

kckc314
RE: Functions
10 May, 2008 - 03:38 AM
Post #5

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

QUOTE(KYA @ 10 May, 2008 - 03:38 AM) *

void functions do not have return statements

so what should i do to make every function executed in turn?
User is offlineProfile CardPM
+Quote Post

KYA
RE: Functions
10 May, 2008 - 05:00 AM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,733



Thanked: 89 times
Dream Kudos: 1200
My Contributions
I have a hard time believing that main() only executes the first function and quits. How about some code? Are you prototyping functions correctly? etc...
User is online!Profile CardPM
+Quote Post

kckc314
RE: Functions
10 May, 2008 - 06:03 AM
Post #7

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

QUOTE(KYA @ 10 May, 2008 - 06:00 AM) *

I have a hard time believing that main() only executes the first function and quits. How about some code? Are you prototyping functions correctly? etc...

function prototype:
void sp1(int r, int c);
void spmtn1(int r, int c);
void spmw1(int r, int c);
void spmtmw1(int r, int c);

calling function:
sp1(sRow, sCol);
spmtn1(sRow, sCol);
spmw1(sRow, sCol);
spmtmw1(sRow, sCol);

function definition:
void sp1(int r, int c) {
void spmtn1(int r, int c) {
void spmw1(int r, int c) {
void spmtmw1(int r, int c) {

main function:
int main(int argc, char *argv[]) {

i simply comment out the first calling function and leave one of others open, that's why i know this problem.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Functions
10 May, 2008 - 06:04 AM
Post #8

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,733



Thanked: 89 times
Dream Kudos: 1200
My Contributions
That's still not "all" of it.

Prototypes and declarations outside of main are good and that's where they should be, but this:

cpp

calling function:
sp1(sRow, sCol);
spmtn1(sRow, sCol);
spmw1(sRow, sCol);
spmtmw1(sRow, sCol);


Why is this outside and what is its purpose in your code/project?
User is online!Profile CardPM
+Quote Post

kckc314
RE: Functions
10 May, 2008 - 06:39 AM
Post #9

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

QUOTE(KYA @ 10 May, 2008 - 07:04 AM) *

That's still not "all" of it.

Prototypes and declarations outside of main are good and that's where they should be, but this:

cpp

calling function:
sp1(sRow, sCol);
spmtn1(sRow, sCol);
spmw1(sRow, sCol);
spmtmw1(sRow, sCol);


Why is this outside and what is its purpose in your code/project?

the calling function are sitting inside the main() function not outside.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Functions
10 May, 2008 - 03:18 PM
Post #10

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,733



Thanked: 89 times
Dream Kudos: 1200
My Contributions
post code and ill test and fix it for you
User is online!Profile CardPM
+Quote Post

kckc314
RE: Functions
10 May, 2008 - 05:07 PM
Post #11

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

QUOTE(KYA @ 10 May, 2008 - 04:18 PM) *

post code and ill test and fix it for you

here is the code. besides the four functions calling issues, can you also help me to look into the algorithm and logic of spmtn1() and spmw1() functions as it's still not working and i have to submit the work today. pls i beg you. i know i am spending your time to do my work but i just can't solve the problem. appreciate your help. for spmw1() function, i find that the shortest path figure is correct but the weight figure is wrong. don't worry about the bottom spmtmw1() function, as i assume if i fix these two functions then i may be able to fix spmtmw1() function. when you test the C file, put the C file together with the attached test.txt file into a same folder, open up command prompt, browse to that folder where you saved those 2 files, on the command line type "a <test.txt 1 12 6 12", remember without the double quotation mark. if you run this numbers 1 and 6 being the column number 12 being the row number, the correct answer from spmtn1() should be "Shortest monotone path: 7", from spmw1() should be "Shortest path of minimal weight: 9 -- weight 4". pls appreciate your help.
CODE

/ ********************************************************************************
******************************************
*  Description:  The program will read data from a file that:                                                            *
*                - contains at most 24 lines, with at most 51 characters per line(excluding the new line character);     *
*                - contains only #s, simple spaces, digits and new line character;                                       *
*                - represents a square of size 12 x 12, delimited with #s, possibly with spaces around, where each       *
*                  point inside the square is associated either with a digit or with a # symbol.                         *
*                The program will also read four integers, entered from the keyboard, that represent the coordinates     *
*                of two points. For instance, a.out <test.txt 3 2 9 5 expects the program to read data from the file     *
*                test.txt, and perform some operation relative to the two points of respective coordinates (3, 2)        *
*                and (9, 5).                                                                                             *
*                1. Define a path as a sequence of points associated with digits (not #s), such that one can go from     *
*                   one point to the next by moving one cell horizontally (either left or right) or vertically (either   *
*                   up or down), without going through the same point twice. If A and B are the first and last point     *
*                   of a path, then say that the path links A to B. The first task is to compute the minimal length of   *
*                   a path that links two points, if such a path exists.                                                 *
*                2. Say that a path is monotone if for every part P of the path that forms either a horizontal or a      *
*                   vertical line segment, the numbers n1,...,nk associated with the points that make up P are such that *
*                   either n1<=...<=nk or n1>=...>=nk. The second task is to compute the minimal length of a monotone    *
*                   path that links two points in a square, if such a path exists.                                       *
*                3. Define the weight of a path to be the sum of the numbers associated with the points that make up     *
*                   the path. The third task is to compute the minimal length of a path of minimal weight that links     *
*                   two points, if such a path exists, together with the associated weight.                              *
*                4. The fourth task is to compute the minimal length of a monotone path of minimal weight that links     *
*                   two points, if such a path exists, together with the associated weight.                              *
*                                                                                                                        *
* *
********************************************************************************
******************************************/

#include <stdio.h>
#include <stdlib.h>

#define row 14      /* Number of rows on square */
#define col 14      /* Number of columns on square */

void sp1(int r, int c);
void spmtn1(int r, int c);
void spmw1(int r, int c);
void spmtmw1(int r, int c);

char sq[row][col];
char ch;
int vpath[row * col][2];  /* To keep a list of current track that have been visited for back tracking use */
int i = 13, j = 0, x = 0, y = 0, s = 0, m = 0, n = 0, mw = 0, mt = 0, mtmw = 0;
int sp[1] = {999}, spmtn[1] = {999}, spmw[1] = {999}, spmtmw[1] = {999};
int sRow = 0, sCol = 0, dRow = 0, dCol = 0, ctr = 0, ctr1 = 0, ctr2 = 0, ctr3 = 0;

int main(int argc, char *argv[]) {
    
    if (argc < 5) {
       printf("Not enough command line arguments entered. For instance a.out <test.txt 3 2 9 5 then press enter.");
       printf("Note that the four integer numbers are strictly between 1 to 12.\n");
       exit(0);
    }
    else {
         sRow = atoi(argv[2]);
         sCol = atoi(argv[1]);
         dRow = atoi(argv[4]);
         dCol = atoi(argv[3]);
    }
    
    /* Store the square grid into array */
    while ((ch = getchar()) != EOF) {
          if (ch == '#')
             sq[i][j++] = ch;
          else if (ch >= '0' && ch <= '9')
                  sq[i][j++] = ch - '0';
          if (j == col) {
             --i;
             j = 0;
          }
    }
    
    /*for (x = 13; x >= 0; x--) {
        for (y = 0; y < col; y++) {
            if (sq[x][y] == '#' || sq[x][y] == ' ')
               printf("%c ", sq[x][y]);
            else if (sq[x][y] >= 0 && sq[x][y] <= 9)
                    printf("%d ", sq[x][y]);
        }
        printf("\n");
    }
    printf("\n%d\n", sq[1][1] + sq[1][3]);*/
    
    if (sq[sRow][sCol] == '#' || sq[dRow][dCol] == '#') {
       printf("Wrong start or end point.\n");
       exit(0);
    }
    
    sp1(sRow, sCol);     /* Minimal paths */
    spmtn1(sRow, sCol);  /* Minimal monotone paths */
    spmw1(sRow, sCol);   /* Minimal paths of minimal weights */
    spmtmw1(sRow, sCol); /* Minimal monotone paths of minimal weights */

    system("PAUSE");
    return EXIT_SUCCESS;
}

void sp1(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 ctr with the value in *
      * the array, if the current value of ctr is less, update it and display message */
     if (r == dRow && c == dCol) {
        if (ctr < sp[s]) {
           sp[s] = ctr;
           printf("Shortest path: %d\n", sp[s]);
           return;
        }
     }
     else {
         ctr++;
         vpath[ctr][0] = r;
         vpath[ctr][1] = c;
     }

     /* To compute the minimal paths from the origin point to destination point */
     if (c > dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
        sp1(r, c - 1);
     else
     if (c < dCol && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
        sp1(r, c + 1);
     else
     if (r > dRow && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
        sp1(r - 1, c);
     else
     if (r < dRow && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ')
        sp1(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] != ' ')
             sp1(r + 1, c);
          else
          if (r - 1 > 0 && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
             sp1(r - 1, c);
          else
          if (c + 1 < col && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
             sp1(r, c + 1);
          else
          if (c - 1 > 0 && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
             sp1(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] != ' ')
        sp1(r + 1, c);
     else
     if (r - 1 > 0 && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
        sp1(r - 1, c);
     else
     if (c + 1 < col && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
        sp1(r, c + 1);
     else
     if (c - 1 > 0 && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
        sp1(r, c - 1);
     else {
          ctr--;
          if (ctr <= 0)
             printf("No path exists\n");
          else
              sp1(vpath[ctr][0], vpath[ctr][1]);
     }
    
     /* When all directions have been marked with empty space, nowhere else to move to */
     if (r + 1 < row && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ')
        sp1(r + 1, c);
     else
     if (r - 1 > 0 && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ')
        sp1(r - 1, c);
     else
     if (c + 1 < col && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ')
        sp1(r, c + 1);
     else
     if (c - 1 > 0 && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ')
        sp1(r, c - 1);
     else
         printf("No path exists\n");
}

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] != ' ' && sq[r][c] == ' ' && sq[r][c + 1] != ' ')
        spmtn1(r, c - 1);
     else
     if (c > dCol && sq[r][c - 1] != '#' && sq[r][c - 1] != ' ' && sq[r][c] == ' ' && sq[r][c + 1] == ' ' &&
        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] != ' ' && sq[r][c] == ' ' && sq[r][c - 1] != ' ')
        spmtn1(r, c + 1);
     else
     if (c < dCol && sq[r][c + 1] != '#' && sq[r][c + 1] != ' ' && sq[r][c] == ' ' && sq[r][c - 1] == ' ' &&
        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] != ' ' && sq[r][c] == ' ' && sq[r + 1][c] != ' ')
        spmtn1(r - 1, c);
     else
     if (r > dRow && sq[r - 1][c] != '#' && sq[r - 1][c] != ' ' && sq[r][c] == ' ' && sq[r + 1][c] == ' ' &&
        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] != ' ' && sq[r][c] == ' ' && sq[r - 1][c] != ' ')
        spmtn1(r + 1, c);
     else
     if (r < dRow && sq[r + 1][c] != '#' && sq[r + 1][c] != ' ' && sq[r][c] == ' ' && sq[r - 1][c] == ' ' &&
        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");
}

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 (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] != ' ')
        if (c > dCol && 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 (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] != ' ')
        if (c < dCol && 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 (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] != ' ')
        if (r > dRow && 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 (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] != ' ')
        if (r < dRow && 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]);
     }
}

void spmtmw1(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 ctr3 with the value in *
      * the array, if the current value of ctr3 is less, update it and display message together *
      * with the minimal weights information */
     if (r == dRow && c == dCol) {
        if (ctr3 < spmtmw[mt]) {
           spmtmw[mt] = ctr3;
           printf("Shortest monotone path of minimal weight: %d -- weight %d\n", spmtmw[mt], mtmw);
           return;
        }
     }
     else {
          mtmw += sq[r][c];
          ctr1++;
     }

     /* To compute the minimal monotone paths of minimal weights 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);
     }
}



Attached File(s)
Attached File  test.txt ( 516bytes ) Number of downloads: 13
User is offlineProfile CardPM
+Quote Post

kckc314
RE: Functions
10 May, 2008 - 05:30 PM