Well I have this code on the knight's tour, where the user will input the knight's starting position. But the problem is, there's too many 9 when I print the output. What's wrong with my algo??

#include <stdio.h>

main()
{

// INITIALYZING THE CHESS BOARD
char c, d; // c is for the column and d is for the row
int a, b; // a is for the column and b is for the row
int j=1; // j is the counter for the board
int board[8][8]={0};
// INITIALYZING THE PRIORITY BOARD
int priority_array[8][8] = {{1,2,3,3,3,3,2,1}, {2,3,4,4,4,4,3,2}, {3,4,5,5,5,5,4,3}, {3,4,5,5,5,5,4,3}, {3,4,5,5,5,5,4,3}, {3,4,5,5,5,5,4,3}, {2,3,4,4,4,4,3,2}, {1,2,3,3,3,3,2,1}};
// STARTING POINT
    printf("Starting Point: ");
    scanf("%c%c", &c, &d);

    //if statements are to be used
    if (d=='8')
    {
        b=0;

        if (c=='A')
        {
            a=0;
            board[b][a]=1;
            priority_array[b][a]=9;
        }

        else if (c=='B')
        {
            a=1;
            board[b][a]=1;
            priority_array[b][a]=9;
        }

        else if (c=='C')
        {
            a=2;
            board[0][2]=1;
            priority_array[0][2]=9;
        }

        else if (c=='D')
        {
            a=3;
            board[0][3]=1;
            priority_array[0][3]=9;
        }

        else if (c=='E')
        {
            a=4;
            board[0][4]=1;
            priority_array[0][4]=9;
        }

        else if (c=='F')
        {
            a=5;
            board[0][5]=1;
            priority_array[0][5]=9;
        }

        else if (c=='G')
        {
            a=6;
            board[0][6]=1;
            priority_array[0][6]=9;
        }

        else if (c=='H')
        {
            a=7;
            board[0][7]=1;
            priority_array[0][7]=9;
        }

        else
        {return 0;}
    }

    else if (d=='7')
    {
        b=1;
        if (c=='A')
        {
            a=0;
            board[1][0]=1;
            priority_array[1][0]=9;
        }

        else if (c=='B')
        {
            a=1;
            board[1][1]=1;
            priority_array[1][1]=9;
        }

        else if (c=='C')
        {
            a=2;
            board[1][2]=1;
            priority_array[1][2]=9;
        }

        else if (c=='D')
        {
            a=3;
            board[1][3]=1;
            priority_array[1][3]=9;
        }

        else if (c=='E')
        {
            a=4;
            board[1][4]=1;
            priority_array[1][4]=9;
        }

        else if (c=='F')
        {
            a=5;
            board[1][5]=1;
            priority_array[1][5]=9;
        }

        else if (c=='G')
        {
            a=6;
            board[1][6]=1;
            priority_array[1][6]=9;
        }

        else if (c=='H')
        {
            a=7;
            board[1][7]=1;
            priority_array[1][7]=9;
        }

        else
        {return 0;}
    } 

    else if (d=='6')
    {
        b=2;
        if (c=='A')
        {
            a=0;
            board[2][0]=1;
            priority_array[2][0]=9;
        }

        else if (c=='B')
        {
            a=1;
            board[2][1]=1;
            priority_array[2][1]=9;
        }

        else if (c=='C')
        {
            a=2;
            board[2][2]=1;
            priority_array[2][2]=9;
        }

        else if (c=='D')
        {
            a=3;
            board[2][3]=1;
            priority_array[2][3]=9;
        }

        else if (c=='E')
        {
            a=4;
            board[2][4]=1;
            priority_array[2][4]=9;
        }

        else if (c=='F')
        {
            a=5;
            board[2][5]=1;
            priority_array[2][5]=9;
        }

        else if (c=='G')
        {
            a=6;
            board[2][6]=1;
            priority_array[2][6]=9;
        }

        else if (c=='H')
        {
            a=7;
            board[2][7]=1;
            priority_array[2][7]=9;
        }

        else
        {return 0;}
    }

    else    if (d=='5')
    {
        b=3;
        if (c=='A')
        {
            a=0;
            board[3][0]=1;
            priority_array[3][0]=9;
        }

        else if (c=='B')
        {
            a=1;
            board[3][1]=1;
            priority_array[3][1]=9;
        }

        else if (c=='C')
        {
            a=2;
            board[3][2]=1;
            priority_array[3][2]=9;
        }

        else if (c=='D')
        {
            a=3;
            board[3][3]=1;
            priority_array[3][3]=9;
        }

        else if (c=='E')
        {
            a=4;
            board[3][4]=1;
            priority_array[3][4]=9;
        }

        else if (c=='F')
        {
            a=5;
            board[3][5]=1;
            priority_array[3][5]=9;
        }

        else if (c=='G')
        {
            a=6;
            board[3][6]=1;
            priority_array[3][6]=9;
        }

        else if (c=='H')
        {
            a=7;
            board[3][7]=1;
            priority_array[3][7]=9;
        }

        else
        {return 0;}
    }

    else    if (d=='4')
    {
        b=4;
        if (c=='A')
        {
            a=0;
            board[4][0]=1;
            priority_array[4][0]=9;
        }

        else if (c=='B')
        {
            a=1;
            board[4][1]=1;
            priority_array[4][1]=9;
        }

        else if (c=='C')
        {
            a=2;
            board[4][2]=1;
            priority_array[4][2]=9;
        }

        else if (c=='D')
        {
            a=3;
            board[4][3]=1;
            priority_array[4][3]=9;
        }

        else if (c=='E')
        {
            a=4;
            board[4][4]=1;
            priority_array[4][4]=9;
        }

        else if (c=='F')
        {
            a=5;
            board[4][5]=1;
            priority_array[4][5]=9;
        }

        else if (c=='G')
        {
            a=6;
            board[4][6]=1;
            priority_array[4][6]=9;
        }

        else if (c=='H')
        {
            a=7;
            board[4][7]=1;
            priority_array[4][7]=9;
        }

        else
        {return 0;}
    }

    else    if (d=='3')
    {
        b=5;
        if (c=='A')
        {
            a=0;
            board[5][0]=1;
            priority_array[5][0]=9;
        }

        else if (c=='B')
        {
            a=1;
            board[5][1]=1;
            priority_array[5][1]=9;
        }

        else if (c=='C')
        {
            a=2;
            board[5][2]=1;
            priority_array[5][2]=9;
        }

        else if (c=='D')
        {
            a=3;
            board[5][3]=1;
            priority_array[5][3]=9;
        }

        else if (c=='E')
        {
            a=4;
            board[5][4]=1;
            priority_array[5][4]=9;
        }

        else if (c=='F')
        {
            a=5;
            board[5][5]=1;
            priority_array[5][5]=9;
        }

        else if (c=='G')
        {
            a=6;
            board[5][6]=1;
            priority_array[5][6]=9;
        }

        else if (c=='H')
        {
            a=7;
            board[5][7]=1;
            priority_array[5][7]=9;
        }

        else
        {return 0;}
    }

    else    if (d=='2')
    {
        b=6;
        if (c=='A')
        {
            a=0;
            board[6][0]=1;
            priority_array[6][0]=9;
        }

        else if (c=='B')
        {
            a=1;
            board[6][1]=1;
            priority_array[6][1]=9;
        }

        else if (c=='C')
        {
            a=2;
            board[6][2]=1;
            priority_array[6][2]=9;
        }

        else if (c=='D')
        {
            a=3;
            board[6][3]=1;
            priority_array[6][3]=9;
        }

        else if (c=='E')
        {
            a=4;
            board[6][4]=1;
            priority_array[6][4]=9;
        }

        else if (c=='F')
        {
            a=5;
            board[6][5]=1;
            priority_array[6][5]=9;
        }

        else if (c=='G')
        {
            a=6;
            board[6][6]=1;
            priority_array[6][6]=9;
        }

        else if (c=='H')
        {
            a=7;
            board[6][7]=1;
            priority_array[6][7]=9;
        }

        else
        {return 0;}
    }

    else    if (d=='1')
    {
        b=7;
        if (c=='A')
        {
            a=0;
            board[7][0]=1;
            priority_array[7][0]=9;
        }

        else if (c=='B')
        {
            a=1;
            board[7][1]=1;
            priority_array[7][1]=9;
        }

        else if (c=='C')
        {
            a=2;
            board[7][2]=1;
            priority_array[7][2]=9;
        }

        else if (c=='D')
        {
            a=3;
            board[7][3]=1;
            priority_array[7][3]=9;
        }

        else if (c=='E')
        {
            a=4;
            board[7][4]=1;
            priority_array[7][4]=9;
        }

        else if (c=='F')
        {
            a=5;
            board[7][5]=1;
            priority_array[7][5]=9;
        }

        else if (c=='G')
        {
            a=6;
            board[7][6]=1;
            priority_array[7][6]=9;
        }

        else if (c=='H')
        {
            a=7;
            board[7][7]=1;
            priority_array[7][7]=9;
        }

        else
        {return 0;}
    }

    else
    {return 0;}

// INITIALYZE THE DECISION ARRAY
    int decision_array[8]={0};


do
{

// PUTTING THE VALUES IN EACH OF THE DATA IN THE DECISION_ARRAY

    if (b-1<0||b-1>7||a+2<0||a+2>7){priority_array[b-1][a+2]=9;}
    decision_array[0]=priority_array[b-1][a+2];

    if (b-2<0||b-2>7||a+1<0||a+1>7){priority_array[b-2][a+1]=9;}
    decision_array[1]=priority_array[b-2][a+1];

    if (b-2<0||b-2>7||a-1<0||a-1>7){priority_array[b-2][a-1]=9;}
    decision_array[2]=priority_array[b-2][a-1];

    if (b-1<0||b-1>7||a-2<0||a-2>7){priority_array[b-1][a-2]=9;}
    decision_array[3]=priority_array[b-1][a-2];

    if (b+1<0||b+1>7||a-2<0||a-2>7){priority_array[b+1][a-2]=9;}
    decision_array[4]=priority_array[b+1][a-2];

    if (b+2<0||b+2>7||a-1<0||a-1>7){priority_array[b+2][a-1]=9;}
    decision_array[5]=priority_array[b+2][a-1];

    if (b+2<0||b+2>7||a+1<0||a+1>7){priority_array[b+2][a+1]=9;}
    decision_array[6]=priority_array[b+2][a+1];

    if (b+1<0||b+1>7||a+2<0||a+2>7){priority_array[b+1][a+2]=9;}
    decision_array[7]=priority_array[b+1][a+2];

// CHOOSING PROCESS
    int e, f, i, choice1, choice1_index, choice2;

    choice1=decision_array[0];

    for (i=0; i<8; i++)
    {
        if(choice1>=decision_array[i])
        {
            choice1=decision_array[i];  
            choice1_index=i;
        }   
    }

//  CHOOSING THE INDEX OF THE PRIORITY ARRAY
    if(choice1_index==0)
    { 
        f=b-1; e=a+2;
        choice2=priority_array[b-1][a+2];
    }
    else if(choice1_index==1)
    {
        f=b-2; e=a+1;
        choice2=priority_array[b-2][a+1];
    }
    else if(choice1_index==2)
    {
        f=b-2; e=a-1;
        choice2==priority_array[b-2][a-1];
    }
    else if(choice1_index==3)
    {
        f=b-1; e=a-2;
        choice2==priority_array[b-1][a-2];
    }
    else if(choice1_index==4)
    {
        f=b+1; e=a-2;
        choice2==priority_array[b+1][a-2];
    }
    else if(choice1_index==5)
    {
        f=b+2; e=a-1;
        choice2==priority_array[b+2][a-1];
    }
    else if(choice1_index==6)
    {
        f=b+2; e=a+1;
        choice2==priority_array[b+2][a+1];
    }
    else
    {
        f=b+1; e=a+2;
        choice2==priority_array[b+1][a+2];
    }

// f IS FOR ROW AND e IS FOR COLUMN 

    j++;
    board[f][e]=j;
    priority_array[f][e]=9;


    b=f; a=e;

}while(decision_array[0]!=9||decision_array[1]!=9||decision_array[2]!=9||decision_array[3]!=9||decision_array[4]!=9||decision_array[5]!=9||decision_array[6]!=9||decision_array[7]!=9);
printf("\n");

int g, h;
    for(g=0; g<8; g++){
        for(h=0; h<8; h++){
            if(board[g][h]<10)
            {
                printf("0%d ", board[g][h]);
            }
            else
            {
                printf("%d ", board[g][h]);
            }
        }
        printf("\n");

    }


return 0;
}

Asking people to debug 600+ lines of code isn't fair. Write out the algorithm you are using (pseudo code and logic you used), and where you think the problem is occuring in your code. Then we may be able to help.

I tested the code and Think the problem lies here:

// INITIALYZE THE DECISION ARRAY
    int decision_array[8]={0};

do
{

/*// PUTTING THE VALUES IN EACH OF THE DATA IN THE DECISION_ARRAY


    if (b-1<0||b-1>7||a+2<0||a+2>7){priority_array[b-1][a+2]=9;}
    decision_array[0]=priority_array[b-1][a+2];

    if (b-2<0||b-2>7||a+1<0||a+1>7){priority_array[b-2][a+1]=9;}
    decision_array[1]=priority_array[b-2][a+1];

    if (b-2<0||b-2>7||a-1<0||a-1>7){priority_array[b-2][a-1]=9;}
    decision_array[2]=priority_array[b-2][a-1];

    if (b-1<0||b-1>7||a-2<0||a-2>7){priority_array[b-1][a-2]=9;}
    decision_array[3]=priority_array[b-1][a-2];

    if (b+1<0||b+1>7||a-2<0||a-2>7){priority_array[b+1][a-2]=9;}
    decision_array[4]=priority_array[b+1][a-2];

    if (b+2<0||b+2>7||a-1<0||a-1>7){priority_array[b+2][a-1]=9;}
    decision_array[5]=priority_array[b+2][a-1];

    if (b+2<0||b+2>7||a+1<0||a+1>7){priority_array[b+2][a+1]=9;}
    decision_array[6]=priority_array[b+2][a+1];

    if (b+1<0||b+1>7||a+2<0||a+2>7){priority_array[b+1][a+2]=9;}
    decision_array[7]=priority_array[b+1][a+2];

// CHOOSING PROCESS
    int e, f, i, choice1, choice1_index; 
    int choice2;

    choice1=decision_array[0];

    for (i=0; i<8; i++)
    {
        if(choice1>=decision_array[i])
        {
            choice1=decision_array[i];  
            choice1_index=i;
        }   
    }

//  CHOOSING THE INDEX OF THE PRIORITY ARRAY
    if(choice1_index==0)
    { 
        f=b-1; e=a+2;
        choice2=priority_array[b-1][a+2];
    }
    else if(choice1_index==1)
    {
        f=b-2; e=a+1;
        choice2=priority_array[b-2][a+1];
    }
    else if(choice1_index==2)
    {
        f=b-2; e=a-1;
        choice2=priority_array[b-2][a-1];
    }
    else if(choice1_index==3)
    {
        f=b-1; e=a-2;
        choice2=priority_array[b-1][a-2];
    }
    else if(choice1_index==4)
    {
        f=b+1; e=a-2;
        choice2=priority_array[b+1][a-2];
    }
    else if(choice1_index==5)
    {
        f=b+2; e=a-1;
        choice2=priority_array[b+2][a-1];
    }
    else if(choice1_index==6)
    {
        f=b+2; e=a+1;
        choice2=priority_array[b+2][a+1];
    }
    else
    {
        f=b+1; e=a+2;
        choice2=priority_array[b+1][a+2];
    }

// f IS FOR ROW AND e IS FOR COLUMN 

    j++;
    board[f][e]=j;
    priority_array[f][e]=9;


    b=f; a=e;

}while(decision_array[0]!=9||decision_array[1]!=9||decision_array[2]!=9||decision_array[3]!=9||decision_array[4]!=9||decision_array[5]!=9||decision_array[6]!=9||decision_array[7]!=9);
printf("\n");

int g, h;
    for(g=0; g<8; g++){
        for(h=0; h<8; h++){
            if(board[g][h]<10)
            {
                printf("0%d ", board[g][h]);
            }
            else
            {
                printf("%d ", board[g][h]);
            }
        }
        printf("\n");

    }

Edited 1 Year Ago by Lan_1

Better, but not good enough as yet. First, in the "Choosing Index" block on line 49, use a switch statement instead of a bunch of conditional statements, as in switch(choice1_index). Also, in lines 40-47, after you have captured the value of decision_array[0], you start your index at 0 instead of 1, resulting in a redundant (and possibly incorrect) choice1_index value. That's all I have time for now.

This article has been dead for over six months. Start a new discussion instead.