-5

I have an maze represented as a square array of integers. I have to find the least-cost path (sum of integers) through the maze, moving orthogonally.

12323
12323
11232
21111

For instance, the track through the maze above will be all the 1's:

X2323
X2323
XX232
2XXXX

I wrote a recursive function to find the path, but it always return 0 instead of the number of steps.

I set breakpoint and find out that the line below always executes (comparison is true) even when the array location matches num.

if(*((int*)table)!=num)
    return 0;

I think I have some problems with the int**

code:

    case 3:// from the main. arrTask3 is initialized By get2Darray. 
                printf("enter the size of the array\n");
                scanf("%d",&arrTask3Size);
                arrTask3Size=getarrsize(arrTask3Size);
                Get2Darr((int**)arrTask3, arrTask3Size, 0, 0);
                num=*((int*)arrTask3);
                start=(int**)arrTask3;
                finish=(int**)(arrTask3)+arrTask3Size*arrTask3Size;

               Task3He(num, arrTask3Size, arrTask3, finish, start, 0, 's');

void Get2Darr(int** arr2D,int size,int row, int col){
    int num;
    if(row<size){
        if(col<size){
            printf("enter the element at cell [%d][%d]\n",row,col);
            scanf("%d",&num);
            *((int*)arr2D+row*size+col)=num;
            Get2Darr(arr2D, size, row, col+1);
        }
        else
            Get2Darr(arr2D, size, row+1, 0);
    }
}

    int Task3He(int num,int N ,int **table,int **Finish,int **Start,int count,char prvstep){
        int tempup,tempdo,templ,tempr;
        if((int)(table-Finish)==0&&num==*((int*)table))
            return count;
        if((int)(table-Finish)==0)
            return 0;
        if((int)table<(int)Start||(int)table>(int)Finish)
            return 0;
        if(*((int*)table)!=num)
            return 0;
        if(prvstep=='u'){
        tempup=Task3He(num,N, (int**)table-N, Finish, Start, count+1,'u');
        if (tempup!=0) return 1;
        templ=Task3He(num,N, (int**)table-1, Finish, Start, count+1,'l');
        if (templ!=0) return 1;
        tempr=Task3He(num,N, (int**)table+1, Finish, Start, count+1,'r');
        if (tempr!=0) return 1;
        }
        if(prvstep=='d'){
            tempdo=Task3He(num,N, (int**)table+N, Finish, Start, count+1,'d');
            if (tempdo!=0) return 1;
            templ=Task3He(num,N, (int**)table-1, Finish, Start, count+1,'l');
            if (templ!=0) return 1;
            tempr=Task3He(num,N, (int**)table+1, Finish, Start, count+1,'r');
            if (tempr!=0) return 1;
        }
        if(prvstep=='l'){
            tempup=Task3He(num,N, (int**)table-N, Finish, Start, count+1,'u');
            if (tempup!=0) return 1;
            tempdo=Task3He(num,N, (int**)table+N, Finish, Start, count+1,'d');
            if (tempdo!=0) return 1;
            templ=Task3He(num,N, (int**)table-1, Finish, Start, count+1,'l');
            if (templ!=0) return 1;
        }
        if(prvstep=='r'){
            tempup=Task3He(num,N, (int**)table-N, Finish, Start, count+1,'u');
            if (tempup!=0) return 1;
            tempdo=Task3He(num,N, (int**)table+N, Finish, Start, count+1,'d');
            if (tempdo!=0) return 1;
            tempr=Task3He(num,N, (int**)table+1, Finish, Start, count+1,'r');
            if (tempr!=0) return 1;
        }
        if(prvstep=='s'){
        tempup=Task3He(num,N, (int**)table-N, Finish, Start, count+1,'u');
        if (tempup!=0) return 1;
        tempdo=Task3He(num,N, (int**)table+N, Finish, Start, count+1,'d');
        if (tempdo!=0) return 1;
        templ=Task3He(num,N, (int**)table-1, Finish, Start, count+1,'l');
        if (templ!=0) return 1;
        tempr=Task3He(num,N, (int**)table+1, Finish, Start, count+1,'r');
        if (tempr!=0) return 1;
        }
        return 0;
        }
Prune
  • 76,765
  • 14
  • 60
  • 81
Dor
  • 1
  • 2
  • 3
    Time to learn hot to debug your code... – LPs Aug 29 '16 at 07:22
  • First thing i saw is that you are loosing `arrTask3Size` value, whatever it is, by doing this `arrTask3Size=getarrsize(arrTask3Size)`; where the function `getarrsize` does something with `arrTask3Size`, then returns some value, and erases previos value of `arrTask3Size`. But.. then again maybe you wanted it to be that way – Eugene Anisiutkin Aug 29 '16 at 07:23
  • 3
    There are **way too many** casts in your code. Casts hide compiler warnings and are a strong indication that you are doing it wrong. For path finding problem no casts should be necessary. – user694733 Aug 29 '16 at 07:24
  • @EugeneAnisiutkin yep this function just make sure the end user enter a number <20.. its ok. LP'S -hope you could help me... – Dor Aug 29 '16 at 07:28
  • My comment was supposed to give you the way to do the job on yourself. Search a guide like [this one](https://beej.us/guide/bggdb/) and debug your application yourself. That's the correct way to solve problem like yours. Do not assume someone else can do it for you: do it. – LPs Aug 29 '16 at 07:41
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your code and accurately describe the problem. – Prune Aug 29 '16 at 16:27

1 Answers1

0

Take a look at your immediate problem:

if(*((int*)table)!=num)
    return 0;

table is a 2-D int array. You cast it to a 1-D int array. Then you dereference it as if it actually were a 1-D array. That gives you the address of table[0], the top row. You now compare that against your parameter num.

I'm not sure what num is, since you haven't bothered to document your program or otherwise explain the interface. However, your English usage suggests that num is a small positive integer. This means that it will not be numerically equal to a memory address. The "not equal" comparison will always be true.

I can't fix this for you, since I'm unclear on how your program is supposed to work. I strongly recommend that you learn the building blocks of programming before you tackle something this involved.

Among other things, learn how to use basic data types naturally, rather than casting them to other types -- as one commenter already mentioned, a type-cast usually means that you have an innate error in your design. For instance, look at this comparison:

(int)(table-Finish)==0

In words, what are you trying to do with this statement? Semantically, this simply compares the starting addresses of a pair of 2-D arrays. You could do that with the straightforward

table == Finish

For some reason, you've found it necessary to subtract the addresses, convert the difference to integer (it's already an integer type), and then compare the converted difference to 0. You used three operations instead of the straightforward one.

Also, learn incremental programming: write a few lines, debug them, and do not go on until you're sure they work. The code above has many problems, making it hard to read and hard to debug. Most especially, you should not have written most of this code when you had not yet debugged the ability to reference a single array location. Another commenter also noted that you seem to overwrite the array size -- if you can't get the input correctly, how is the rest of the program supposed to work?

Prune
  • 76,765
  • 14
  • 60
  • 81