GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Jan-2006, 09:18
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Programs failing to execute...?


I'm attempting to write a program for the queens problem (which I've not completed yet) but the problem is that although it has no compile-time errors but when I run the executable from the command prompt nothing happens and I return to the prompt . Whats going on? I'm clue less.
Please help.

Regards,

Netnut.
  #2  
Old 11-Jan-2006, 09:29
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Programs failing to execute...?


Quote:
Originally Posted by netnut
I'm attempting to write a program for the queens problem (which I've not completed yet) but the problem is that although it has no compile-time errors but when I run the executable from the command prompt nothing happens and I return to the prompt . Whats going on? I'm clue less.
Please help.

Regards,

Netnut.

Hi, netnut.

As far as I understood, you have done some printing to the console in your code and that's what you expect to see, right?
Well, my best guess is that your program doesn't run on the branches doing the printing, and it just encountered an error and finished (perhaps due to a check you did).
Anyway, without seeing your code we can only make supposition.
It really doesn't matter that your code is not complete, and we don't expect (or care) it to be perfect especially in this early stage, but if you could just post what you have now, we could make a more accurate guess of what is wrong.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 11-Jan-2006, 10:24
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Programs failing to execute...?


Quote:
Originally Posted by netnut
I'm attempting to write a program for the queens problem (which I've not completed yet) but the problem is that although it has no compile-time errors but when I run the executable from the command prompt nothing happens and I return to the prompt . Whats going on? I'm clue less.
Please help.

Regards,

Netnut.

I will answer your question with a question. (Let's have a little role-playing music please.)

I have a long complicated program that has 325 functions. When I compile the program, I get no compiler errors or warning messages whatsoever. When I execute the program nothing happens. (That is, it just returns to the command prompt and does not print out anything.) Please tell me what is going on.

Well, the first thing you might want to see is the code. Not all 10,000 lines of course. You might tell me to try boil it down to a smaller number of functions with fewer total lines of code that still exhibits the problem.

OK, I say, here is a much reduced program:

CPP / C++ / C Code:
#include <stdio.h>
int main()
{
  void init(void);
  void run(void);
  void report(void);

  /* some stuff here */
  init();

  /* more stuff here */

  run();

  /* more stuff here */

  report();

  /* more stuff here */

  printf("Program complete. Goodbye\n");

  return 0;
}

void report()
{
  printf("Reporting the results:\n");
  /* there was lots of stuff here */
}

Furthermore, I say that the other functions (init() and run()) are quite long and complex, and they contain proprietary code, so I can't post them. (I was given these functions from the Lead Software Engineer of another project, and I was told that they work correctly.) I don't see any way that they could be inhibiting the output that my program has clearly indicated. Please tell me what is going on. Why doesn't the program at least print the "goodbye" message?

Well, If I won't show you the other functions, how can you help me? The answer is: you can't.

Then how can I debug this blasted thing? Answer: I must do it myself.

As a starter, you tell me to put print statements at the beginning and before and after each function call. Something like this:

CPP / C++ / C Code:
#include <stdio.h>
int main()
{
  void init(void);
  void run(void);
  void report(void);

  /* put this before any executable code */

  printf("Main program.\n");

  /* some stuff here */

  printf("calling init()\n");
  init();
  printf("retured from init()\n");

  /* more stuff here */

  printf("calling run()\n");
  run();
  printf("returned from run()\n");

  /* more stuff here */

  printf("calling report()\n");
  report();
  printf("returned from report()\n");

  /* more stuff here */

  printf("Program complete. Goodbye\n");

  return 0;
}

void report()
{
  printf("Reporting the results:\n");
  /* there was a lot of other stuff here */
}

Guess what? Now when I run the program I see the following, and then it goes back to the command line:

Code:
Main program. calling init() retured from init() calling run()

What happened? It never returned from run(). So, what do I do now?

Well, I go into run(), and I put a printf() statement at the beginning to show that it entered the routine. I sprinkle printf() statement throughout the function to see if I can tell how far it got. In particular, before any function invocations, even library functions (memcpy(), scanf(), strcat(), ... anything). I also put printf statements before any conditional statements (if, while, etc) so show what the values of the variables are at each point so that I can see, not only that the program got that far, but can show what the program is seeing as it goes through the logic.

This kind of stuff is easier and quicker to do than it is to describe. It is also quicker, I claim, than posting a request for help and waiting for a helpful response. (But it's always OK to ask; I am just trying to let you see how to make best use of your time.)

It is called debugging. Of all the time that I spend in programming activities, I would estimate that I spend 10% writing code and 80% debugging. (And the other 30% of my time documentating. And the other 40% of my time arguing with the Marketing department.)

Of course if my programming were perfect, I might spend less time debugging.

Interestingly enough, the percentages might not change much, since may be harder to find bugs in a "good" program than in an obviously "bad" program. In any case, I think that debugging skills are at least eight times as important as programming skills at any level of proficiency. Just my opinion, of course. Your Mileage May Vary.

Regards,

Dave

P.S. The same approach that I suggested here also can give clues about what the heck is wrong with programs that exhibit other run-time errors that prevent successful completion of the program (segment fault; access violation, floating point overflow, etc.)
  #4  
Old 11-Jan-2006, 23:57
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: Programs failing to execute...?


Well, thanks Dave & LuciWiz (not necessarily in that order).

Maybe I titled the thread wrong. 'Cuz I wanted out of this thread was discussion as to how to avoiding these nuisances from happening. I definately did not want submit my code for reviews or to be debugged.
As of now I have corrected the problem (without knowing what the problem was!) and the program is executing fine giving the expected output etc. etc.

I have one more question though: Why did the program terminate in less than a second on the first instance, but when I included a single cout statement in the recursive function I had to stop it ...it was running without giving me an impression of ever stopping?
  #5  
Old 12-Jan-2006, 08:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Programs failing to execute...?


Quote:
Originally Posted by netnut
Well, thanks Dave & LuciWiz (not necessarily in that order).

Maybe I disguised the purpose of my post too well. My points were:

1. You didn't give enough information for anyone to help you directly. (We could have guessed at reasons, but no one could actually debug your program without seeing more.) If you want us to give us something (help) you might have to give us more than you did (information).

2. You can sometimes get to the bottom of things yourself. (But it is always OK to ask; especially if the program got "fixed" by some technique but you really don't understand what is happining.)

Try the following:

CPP / C++ / C Code:
#include <stdio.h>
int main()
{
  void init(void);

  init();
  printf("returned from init()\n");

  return 0;
}

void init()
{
  init();
}

What happens? The answer is: "It depends." For some compilers and operating systems the program returns almost immediately to the command line. But the behavior is undefined (and, after all, how could any language definition, like the C Standard, require any particular behavior in cases where a buggy program causes a program stack overflow?)

Now try this, modified in accordance to the debugging principles that I tried to suggest:

CPP / C++ / C Code:
#include <stdio.h>
int main()
{
  void init(void);

  printf("Main program.\n");

  printf("calling init()\n");
  init();
  printf("returned from init()\n");

  return 0;
}

void init()
{
  static unsigned int count = 0;
  printf("Entering init(), count = %u\n", count++);
  init();
}

Run this. Don't stop the printout, just let it run. Eventually it will overflow the program stack and whatever happened in the first case will now happen. It probably took about as many recursive calls to overflow the stack as before, but now it takes longer since the program is printing out something every time.

Regards,

Dave
  #6  
Old 12-Jan-2006, 11:39
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: Programs failing to execute...?


Thanks Dave (& in that particular order , i think my statement didn't convey the right message that I intended it to) I got ur point and as always ur posts are very enlightening this one is no exception. Thanks again.

Ok, here is the code that I have developed, now I'd like suggestions from you all to optimize it (dunno why need optimization 'cuz it takes less than a second to run on my system), infact I'd like to learn to optimize (never done it before I guess)

Remember: The problem is to arrange 8 queens on a 8x8 chessboard (I'll generalize later and arrange N-queens on a NxN chessboard). As far as I see many solutions generated are symmetrical, is there a way to avoid those? Would that be optimization? So that I only get "unique" soulutions?
CPP / C++ / C Code:
#include<iostream>
using namespace std;
class queen{
	int r,c;          /* ROW , COLUMN*/
	public:
	void setR(int a){ r = a; }
	void setC(int a){ c = a; }
	int row(){ return r+1;}
	int col(){ return c+1;}
};
queen a[8];             /* ARRAY OF 8 QUEENS                   */
void show();            /* PRINTS OUT POSITIONS OF ALL QUEENS  */
bool conflict(int);     /* CHECKS TO SEE IF COLLISION OCCOURS  */
void arrange(int c){    /* ARRANGES THE Cth QUEEN ON THE BOARD */
	if(c==8){
		show();
		return;
	}
	for(int i=0;i<8;i++){
		a[c].setR(i);
		if(!conflict(c)) arrange(c+1);
	}
}
void show(){

	for(int i=0;i<8;i++)
		cout<<'('<<a[i].row()<<','<<a[i].col()<<')'<<' ';
     cout<<endl;
}
bool conflict(int c){

    /******CHECKING 2 SEE NO 2 QUEENS ARE IN THE SAME ROW*****/

    for(int i=c-1;i>=0;i--)
		if(a[c].row()==a[i].row())
            return true;               
 	int i,j;

     /*              ******CHECKING ALL DIAGONALS*********/

     /* Diagonal North West*/
	for(i=a[c].row()-1,j=a[c].col()-1;i>0&&j>0;i--,j--)
		for(int t=0;t<c;t++)
			if(a[t].row()==i&&a[t].col()==j) return true;
	/*Diagonal North East*/
	for(i=a[c].row()-1,j=a[c].col()+1;i>0&&j<=8;i--,j++)
		for(int t=0;t<c;t++)
			if(a[t].row()==i&&a[t].col()==j) return true;
	/*Diagonal South West*/
	for(i=a[c].row()+1,j=a[c].col()-1;i<=8&&j>0;i++,j--)
		for(int t=0;t<c;t++)
			if(a[t].row()==i&&a[t].col()==j) return true;
	/*Diagonal South East*/
	for(i=a[c].row()+1,j=a[c].col()+1;i<=8&&j<=8;i++,j++)
		for(int t=0;t<c;t++)
			if(a[t].row()==i&&a[t].col()==j) return true;
	
	return false;	
}
int main(){
     /*placing all queens in the first row and cols 0-7*/
     for(int i=0;i<8;i++){
	a[i].setC(i);
	a[i].setR(0);
    }
    arrange(0);
    return 0;
}

Regards,

Netnut.

EDIT: This is the first time that I have posted a code I hope I did it the right way, didn't I?
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with simple C programs... Whats wrong? ovadoggvo C Programming Language 4 19-May-2005 08:15
1 line php cover function changes result of called programs schgid MySQL / PHP Forum 0 03-May-2005 11:15
Piping external program's stderr? teerik C Programming Language 2 01-Feb-2005 13:55
help in c programs kitin21 C Programming Language 4 08-Sep-2004 08:38
Apache prompt user for download JSp instead execute them ! Saad Apache Web Server Forum 0 09-Feb-2004 11:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:44.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.