Like Tree10Likes
  • 1 Post By grumpy
  • 5 Post By phantomotap
  • 2 Post By jimblumberg
  • 2 Post By phantomotap

Debug this Please. ( C, C++ Mixup)

This is a discussion on Debug this Please. ( C, C++ Mixup) within the C++ Programming forums, part of the General Programming Boards category; I just recently(as in a few hours ago) decided to take up a small program my dad put up for ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    4

    Debug this Please. ( C, C++ Mixup)

    I just recently(as in a few hours ago) decided to take up a small program my dad put up for me. It's the N-Queens Problem(Google it).

    The code is below. Now I know that the code is pretty redundant but I honestly don't care too much about that.


    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <math.h>
    
    
    int b[8][8];
    
    void setzero(int x)//This sets the remaining non-conflicted spaces in the board to zero. x is the row number.
    {
    int v,w;
    for (v=x;v<=8;v++)
    {
    for (w=1;w<=8;w++)
    {
    if(b[v][w]>=x)
    b[v][w]=0;
    }
    }
    }
    void options(int y,int z)//This function is just to remove the places on the board which are crossed out by any one queen.
    {
    int d,e,f,g;
    
    
    for (d=1;d<=8;d++)//Getting the row.
    if (b[d][z]!=9)
    if (b[d][z]==0)
    b[d][z]=y;
    
    
    for (e=1;e<=8;e++)//Getting the Column.
    if (b[y][e]!=9)
    if (b[y][e]==0)
    b[y][e]=y;
    
    
    for (f=1;f<=8;f++)//For the diagonals.
    for (g=1;g<=8;g++)
    {
    if ((f+g)==(y+z))
    {if (b[f][g]==0)
    b[f][g]=y;
    }
    else if(fabs(f-g)==fabs(z-y))
    {
    if (b[f][g]==0)
    b[f][g]=y;
    }
    }}
    
    
    void main()
    {
    
    
    int i,j,k,l,m,n,o,p,q,r,s,t,u,v,w;
    //Declaring a buttload of variables.
    
    for (i=1;i<=8;i++)//Row 1
    {
    int ctr = 1;
    setzero(ctr);
    
    
    b[1][i]=9;
    
    
    options(1,i);
    
    
    for(j=1;j<=8;j++)//Row 2
    {
    ctr = 2;
    setzero(ctr);
    
    
    if (b[2][j] == 0)
    {
    b[2][j]=9;
    options(2,j);
    
    
    for(k=1;k<=8;k++)//Row 3
    {
    ctr = 3;
    setzero(ctr);
    
    
    if(b[3][k]==0)
    {
    b[3][k]=9;
    options(3,k);
    for(l=1;l<=8;l++)//Row 4
    {
    ctr = 4;
    setzero(ctr);
    
    
    if (b[4][l]==0)
    {
    b[4][l]=9;
    options(4,l);
    
    
    for(m=1;m<=8;m++)//Row 5
    {
    ctr = 5;
    setzero(ctr);
    
    
    if (b[5][m]==0)
    {
    b[5][m]=9;
    options(5,m);
    
    
    for(n=1;n<=8;n++)//Row 6
    {
    ctr = 6;
    setzero(ctr);
    
    
    if (b[6][n]==0)
    {
    b[6][n]=9;
    options(6,n);
    
    
    for(o=1;o<=8;o++)//Row 7
    {
    ctr = 7;
    setzero(ctr);
    
    
    if (b[7][o]==0)
    {
    b[7][o]=9;
    options(7,o);
    
    
    for(p=1;p<=8;p++)//Row 8
    {
    ctr = 8;
    setzero(ctr);
    if (b[8][p]==0)
    {
    b[8][p] = 9;
    s=0;
    for(q=1;q<=8;q++)//To check whether i got a solution at all or not. Solution means 8 queens should be there on the board. S is the number of queens.
    {
    for(r=1;r<=8;r++)
    {
    if (b[q][r]==9)
    s=s+1;
    }
    }
    if (s == 8)
    {
    for (t=1;t<=8;t++)//If 8 queens are there, this prints the column number in each row seperated with a '-'.
    for (u=1;u<=8;u++)
    if (b[t][u]==9)
    cout<<u<<"-";
    
    
    }}}}}}}}}}}}}}}}}
    Now I'm not getting any compile errors but when I try to run the program, it just blinks out and then comes back to the same screen. Try running this in your C++ Compiler. I don't get an output. I'm not getting any errors either.

    EDIT: Code edited.


    Any ideas?
    Last edited by sridoodla; 05-13-2012 at 06:36 AM. Reason: Criticism.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Lemme guess... TurboC++, right?
    Devoted my life to programming...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    The functions are part of the class now, you can't call them like this.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    5,866
    You're not succeeding in running the program.

    The linker errors you're seeing about undefined symbols means that your code is calling functions that don't exist. The executable is not being created (the messages about undefined symbols is the linker's way of explaining that) so cannot be executed.

    In your main() function, there are a number of calls of setzero() taking one argument and of options() as a function taking two arguments. These calls are in main(), not in the context of any class. In particular, they have no relationship to any object of type queens, so do not match the member functions queens::setzero() or queens::options() respectively.

    Also, main() returns int, not void. Anyone (including a book) that has suggested main() returns void is wrong.

    The above are problems I can identify in your code. The fix, however, is your problem .... after all, that depends on what you are trying to do with your code, and I have as much skill in mindreading as I do a propensity for walking on water (and my propensity for that is none).

    Your code layout is absolutely atrocious. If you want people to explain problems to you in future, try posting small, complete, and READABLE samples of code. Also, don't expect forum members to be mind-readers. You have some idea of what you intend your code to do, but forum members do not unless you provide useful information. Posting a chunk of badly formatted code is not an effective way of eliciting help.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    4
    Quote Originally Posted by grumpy View Post
    You're not succeeding in running the program.

    The linker errors you're seeing about undefined symbols means that your code is calling functions that don't exist. The executable is not being created (the messages about undefined symbols is the linker's way of explaining that) so cannot be executed.

    In your main() function, there are a number of calls of setzero() taking one argument and of options() as a function taking two arguments. These calls are in main(), not in the context of any class. In particular, they have no relationship to any object of type queens, so do not match the member functions queens::setzero() or queens:ptions() respectively.

    Also, main() returns int, not void. Anyone (including a book) that has suggested main() returns void is wrong.

    The above are problems I can identify in your code. The fix, however, is your problem .... after all, that depends on what you are trying to do with your code, and I have as much skill in mindreading as I do a propensity for walking on water (and my propensity for that is none).

    Your code layout is absolutely atrocious. If you want people to explain problems to you in future, try posting small, complete, and READABLE samples of code. Also, don't expect forum members to be mind-readers. You have some idea of what you intend your code to do, but forum members do not unless you provide useful information. Posting a chunk of badly formatted code is not an effective way of eliciting help.
    "Grumpy"

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    4
    Code Edited. Thanks Grumpy. I second that but I have a totally unrelated exam coming up tomorrow and couldn't spare much time for this.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,815
    Code:
    }}}}}}}}}}}}}}}}}
    O_O

    Well there's your problem.

    How in Yog Sothoth's foul name are you following the logic of that code?

    I can't tell where anything begins or ends.

    Soma
    antred, iMalc, GReaper and 2 others like this.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    4
    Quote Originally Posted by phantomotap View Post
    O_O

    Well there's your problem.

    How in Yog Sothoth's foul name are you following the logic of that code?

    I can't tell where anything begins or ends.

    Soma
    Firstly, I used a lot of different variables as I wasn't sure as to how to go about with only a handful considering the number of for loops I used as I wasn't sure about the scope of each variable and I didn't want to go back and forth adding and removing variables.

    Secondly, the number of for loops are a necessity and not bad code on my part.

    The n-Queens problem asks you to arrange n Queens on a NxN chessboard such that each queen is "Safe". Now that is only possible for 4x4, 8x8, 16x16 etc.

    I used the 8x8 problem.

    My code follows that I needed to check the possibility of a solution from each and every point on the board. I couldn't think of a very effective algorithm, I admit. But I'm damn sure this works.

    I set up a for loop for the first row in the chess board. Then I initiate every value in the chessboard to 0(setzero).Keeping the row constant and the column number in the FOR loop, I assigned the block a queen ( Numeral Nine). Now, I remove all the pieces on the chessboard which are not "safe" due to the presence of this queen on that block by setting those particular blocks the numeral (x in my program) of the row number of the queen responsible(options). Then I set up another for loop for the second row. Following similar logic all the way till the eighth row. Now once I come to the eighth row, I don't need to check the options available. Here, I checked for the availability of a valid solution by counting the number of queens in the array. If 8, I put a print command to that to print the column number of the queen in each row.Reference the vague comments in the code provided. I used the concept of backtracking to solve this problem as is the only way to go about it. I could probably have used a function for the repetitive for loops but I think I'm justified if I say that I'm not good at that. Hehe. Hopefully this explains my code.


    Also, I'm following the logic of the code as I wrote it.
    My father tells me that only the programmer and the computer can understand the code if not assisted by comments. ^_^
    My bad there. Hehe. Hopefully you guys can help me out.

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Do yourself a favour and choose one of these: Indent style - Wikipedia, the free encyclopedia
    Even you yourself will be completely unable to figure out where you need to change something in case something goes wrong.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    And unless you start using meaningful variable names you will still be lost even with proper indentation. What are you going to do when you run out of single letters to use as names? And a few functions would probably be of great use.

    Jim
    Salem and iMalc like this.

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,815
    Also, I'm following the logic of the code as I wrote it.
    O_o

    Then why do you need help?

    If you do need help, you aren't the only one who needs to understand it.

    Learn to write code that is clean, consistent, and meaningful or to be a lonely developer.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Unsigned / int mixup (?)"
    By Hennet in forum C Programming
    Replies: 3
    Last Post: 10-26-2011, 05:09 AM
  2. debug dev-c++
    By mrsirpoopsalot in forum Tech Board
    Replies: 33
    Last Post: 11-06-2006, 08:55 PM
  3. Please help me to debug
    By wml in forum C Programming
    Replies: 4
    Last Post: 03-15-2003, 10:54 AM
  4. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-10-2002, 11:40 PM
  5. debug...
    By jk81 in forum C Programming
    Replies: 2
    Last Post: 10-24-2002, 08:56 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21