cout stops working!! #%#$% Student going INSANE!!!

S

sandy

I am working on a simulation. It requires that I place an Arrival Event
into a priority Queue. I then pull it from the priority Q, make some
decisions do some stuff and add a new Arrival Event (every Arrival that
get's pulled generates a new arrival.)

Since I have to be able to loop and create new events each loop I
defined my event:

Event *E

and in each loop I create a new pointer. the beginings of it looks like
this:

// dimension my Event E
Event *E;

E = new Event;

// Calculate a new execution Interval
Interval = GenRandom(AE);
E->LoadEvent(0, Interval, 'A', Interval);
//EventCounter is now 1 (one event has been created)
EventCounter = 1;

// Add the Event to priority Q
Result = myPQ.Insert(*E);

As soon as I do the myPQInsert(*E) I lose the ability to output to the
screen. I am including the Priority Q below (in full) but here are the
two functions that are the problem:

<code>
// Insert
//
// Time complexity: O(log n)

template <class Etype>
int
PQueue<Etype>::Insert ( const Etype & Element )
{
// This line prints so we get to here
std::cout << "Into Insert" << std::endl;

if ( CurrentSize == MaxSize )
{
DoubleArray ( );
}
// this line prints so we get to here
std::cout << " after double array check" << std::endl;

Array[++CurrentSize] = Element; // Place Element at the end
// This line prints as well
std::cout << "after placing element at end" << std::endl;

PercolateUp ( CurrentSize );
// This line does NOT print so problem seems to be in Percolate up
std::cout << "after percolate up" << std:: endl;

return 1;
}

I added comments to show which lines are printing. As you can see it is
only the last line, which occurs after PercolateUp that does not print.

Here is PercolateUp:

template <class Etype>
void
PQueue<Etype>::percolateUp ( int Child )
{
std::cout << "Into Percolate Up" << std::endl;
int Parent = Child/2;
std::cout << "Found Parent" << std::endl;
Etype Temp = Array[Child];
std::cout << "Set temp Etype" << std::endl;

while ( Parent > 0 )
{
if ( Temp < Array[Parent] )
{
std::cout << "Temp is < Array[Parent]" << std::endl;
Array[Child] = Array[Parent];
Child = Parent;
Parent = Child/2;
}
else
break;
}

std::cout << "Just before setting Array[Child] to temp" <<
std::endl;
Array[Child] = Temp;
// this is the last working line.
std::cout << "Finished with Percolate up" << std::endl;
}

Notice: The very last cout in Percolate up still works. When we return
to the calling function, Insert, cout no longer works.

I am using Dev C++ so if I compile and run (f9) the screen asks for
input and shows output until it gets to the insert into the PriorityQ,
then it just flashes and is gone. If I open a command window and
execute the exe file I can see the outputs. From main, and from the
insert function any cout commands after that last 'Finished with
Percolate up' are ignored and nothing comes out.

Prior to today I had set up my main so that I could do:
myPQ.Insert(E) // no *. The problem was in declaring my PQ I had to do
this:

typedef PQueue<Event*> PQ;
PQ myPQ;

When I did that cout worked, but because I was sending a pointer, not
an object, I could not do a < comparison to set priority, as I needed
to.

now I do this:

typedef PQueue<Event> PQ;
PQ myPQ;

Anyway the stupid thing i due first thing Monday morning. I thought I
had it pretty much working (except that everything came out in the
wrong order) but the teaching assistant was able to point out the
problem. (A pointer is a number so it will compare but not using the
overloaded < from the class, just the memory address).

I don't know if I have explained this well enough but I can't figure
this out.
 
T

Tom Smith

(e-mail address removed) wrote:

<code>
// Insert
//
// Time complexity: O(log n)

template <class Etype>
int
PQueue<Etype>::Insert ( const Etype & Element )
{
// This line prints so we get to here
std::cout << "Into Insert" << std::endl;

if ( CurrentSize == MaxSize )
{
DoubleArray ( );
}
// this line prints so we get to here
std::cout << " after double array check" << std::endl;

Array[++CurrentSize] = Element; // Place Element at the end
// This line prints as well
std::cout << "after placing element at end" << std::endl;

PercolateUp ( CurrentSize );
// This line does NOT print so problem seems to be in Percolate up
std::cout << "after percolate up" << std:: endl;

return 1;
}

I added comments to show which lines are printing. As you can see it is
only the last line, which occurs after PercolateUp that does not print.

Here is PercolateUp:

template <class Etype>
void
PQueue<Etype>::percolateUp ( int Child )
{
std::cout << "Into Percolate Up" << std::endl;
int Parent = Child/2;
std::cout << "Found Parent" << std::endl;
Etype Temp = Array[Child];
std::cout << "Set temp Etype" << std::endl;

while ( Parent > 0 )
{
if ( Temp < Array[Parent] )
{
std::cout << "Temp is < Array[Parent]" << std::endl;
Array[Child] = Array[Parent];
Child = Parent;
Parent = Child/2;
}
else
break;
}

std::cout << "Just before setting Array[Child] to temp" <<
std::endl;
Array[Child] = Temp;
// this is the last working line.
std::cout << "Finished with Percolate up" << std::endl;
}

Notice: The very last cout in Percolate up still works. When we return
to the calling function, Insert, cout no longer works.

Are you sure you return to the calling function? I find it hard to believe that
you've managed to break cout; what seems much more likely to me is that your
program crashes at some point in the return process.

Tom
 
S

Salt_Peter

I am working on a simulation. It requires that I place an Arrival Event
into a priority Queue. I then pull it from the priority Q, make some
decisions do some stuff and add a new Arrival Event (every Arrival that
get's pulled generates a new arrival.)

Since I have to be able to loop and create new events each loop I
defined my event:

Event *E

and in each loop I create a new pointer. the beginings of it looks like
this:

// dimension my Event E
Event *E;

E = new Event;

// Calculate a new execution Interval
Interval = GenRandom(AE);
E->LoadEvent(0, Interval, 'A', Interval);
//EventCounter is now 1 (one event has been created)
EventCounter = 1;

// Add the Event to priority Q
Result = myPQ.Insert(*E);

As soon as I do the myPQInsert(*E) I lose the ability to output to the
screen. I am including the Priority Q below (in full) but here are the
two functions that are the problem:

<code>
// Insert
//
// Time complexity: O(log n)

template <class Etype>
int
PQueue<Etype>::Insert ( const Etype & Element )
{
// This line prints so we get to here
std::cout << "Into Insert" << std::endl;

if ( CurrentSize == MaxSize )

Shouldn't the check be for CurrentSize + 1 ?
{
DoubleArray ( );
}
// this line prints so we get to here
std::cout << " after double array check" << std::endl;

Array[++CurrentSize] = Element; // Place Element at the end

Arrays are zero-indexed. Drop element in last slot, then increment
counter. Otherwise you would have an interlaced Array.

Array[CurrentSize++] = Element;
// This line prints as well
std::cout << "after placing element at end" << std::endl;

PercolateUp ( CurrentSize );

This does not make sense, CurrentSize is pointing at one past the last
element.
PercolateUp( CurrentSize - 1);
// This line does NOT print so problem seems to be in Percolate up
std::cout << "after percolate up" << std:: endl;

return 1;
}

I added comments to show which lines are printing. As you can see it is
only the last line, which occurs after PercolateUp that does not print.

Here is PercolateUp:

template <class Etype>
void
PQueue<Etype>::percolateUp ( int Child )
{
std::cout << "Into Percolate Up" << std::endl;
int Parent = Child/2;

That makes no sense.
std::cout << "Found Parent" << std::endl;
Etype Temp = Array[Child];

Temp is getting either an unitialized element or is out of bounds.
std::cout << "Set temp Etype" << std::endl;

while ( Parent > 0 )
{
if ( Temp < Array[Parent] )
{
std::cout << "Temp is < Array[Parent]" << std::endl;
Array[Child] = Array[Parent];
Child = Parent;
Parent = Child/2;
}
else
break;
}

std::cout << "Just before setting Array[Child] to temp" <<
std::endl;
Array[Child] = Temp;
// this is the last working line.
std::cout << "Finished with Percolate up" << std::endl;
}

Notice: The very last cout in Percolate up still works. When we return
to the calling function, Insert, cout no longer works.

I am using Dev C++ so if I compile and run (f9) the screen asks for
input and shows output until it gets to the insert into the PriorityQ,
then it just flashes and is gone. If I open a command window and
execute the exe file I can see the outputs. From main, and from the
insert function any cout commands after that last 'Finished with
Percolate up' are ignored and nothing comes out.

Prior to today I had set up my main so that I could do:
myPQ.Insert(E) // no *. The problem was in declaring my PQ I had to do
this:

typedef PQueue<Event*> PQ;
PQ myPQ;

When I did that cout worked, but because I was sending a pointer, not
an object, I could not do a < comparison to set priority, as I needed
to.

now I do this:

typedef PQueue<Event> PQ;
PQ myPQ;

Anyway the stupid thing i due first thing Monday morning. I thought I
had it pretty much working (except that everything came out in the
wrong order) but the teaching assistant was able to point out the
problem. (A pointer is a number so it will compare but not using the
overloaded < from the class, just the memory address).

I don't know if I have explained this well enough but I can't figure
this out.

We can't really help you except for the obvious. You might consider
using std::cout to output your array indexing mechanism.
 
S

sandy

Salt_Peter said:
I am working on a simulation. It requires that I place an Arrival Event
into a priority Queue. I then pull it from the priority Q, make some
decisions do some stuff and add a new Arrival Event (every Arrival that
get's pulled generates a new arrival.)

Since I have to be able to loop and create new events each loop I
defined my event:

Event *E

and in each loop I create a new pointer. the beginings of it looks like
this:

// dimension my Event E
Event *E;

E = new Event;

// Calculate a new execution Interval
Interval = GenRandom(AE);
E->LoadEvent(0, Interval, 'A', Interval);
//EventCounter is now 1 (one event has been created)
EventCounter = 1;

// Add the Event to priority Q
Result = myPQ.Insert(*E);

As soon as I do the myPQInsert(*E) I lose the ability to output to the
screen. I am including the Priority Q below (in full) but here are the
two functions that are the problem:

<code>
// Insert
//
// Time complexity: O(log n)

template <class Etype>
int
PQueue<Etype>::Insert ( const Etype & Element )
{
// This line prints so we get to here
std::cout << "Into Insert" << std::endl;

if ( CurrentSize == MaxSize )

Shouldn't the check be for CurrentSize + 1 ?
{
DoubleArray ( );
}
// this line prints so we get to here
std::cout << " after double array check" << std::endl;

Array[++CurrentSize] = Element; // Place Element at the end

Arrays are zero-indexed. Drop element in last slot, then increment
counter. Otherwise you would have an interlaced Array.

Array[CurrentSize++] = Element;
// This line prints as well
std::cout << "after placing element at end" << std::endl;

PercolateUp ( CurrentSize );

This does not make sense, CurrentSize is pointing at one past the last
element.
PercolateUp( CurrentSize - 1);
// This line does NOT print so problem seems to be in Percolate up
std::cout << "after percolate up" << std:: endl;

return 1;
}

I added comments to show which lines are printing. As you can see it is
only the last line, which occurs after PercolateUp that does not print.

Here is PercolateUp:

template <class Etype>
void
PQueue<Etype>::percolateUp ( int Child )
{
std::cout << "Into Percolate Up" << std::endl;
int Parent = Child/2;

That makes no sense.
std::cout << "Found Parent" << std::endl;
Etype Temp = Array[Child];

Temp is getting either an unitialized element or is out of bounds.
std::cout << "Set temp Etype" << std::endl;

while ( Parent > 0 )
{
if ( Temp < Array[Parent] )
{
std::cout << "Temp is < Array[Parent]" << std::endl;
Array[Child] = Array[Parent];
Child = Parent;
Parent = Child/2;
}
else
break;
}

std::cout << "Just before setting Array[Child] to temp" <<
std::endl;
Array[Child] = Temp;
// this is the last working line.
std::cout << "Finished with Percolate up" << std::endl;
}

Notice: The very last cout in Percolate up still works. When we return
to the calling function, Insert, cout no longer works.

I am using Dev C++ so if I compile and run (f9) the screen asks for
input and shows output until it gets to the insert into the PriorityQ,
then it just flashes and is gone. If I open a command window and
execute the exe file I can see the outputs. From main, and from the
insert function any cout commands after that last 'Finished with
Percolate up' are ignored and nothing comes out.

Prior to today I had set up my main so that I could do:
myPQ.Insert(E) // no *. The problem was in declaring my PQ I had to do
this:

typedef PQueue<Event*> PQ;
PQ myPQ;

When I did that cout worked, but because I was sending a pointer, not
an object, I could not do a < comparison to set priority, as I needed
to.

now I do this:

typedef PQueue<Event> PQ;
PQ myPQ;

Anyway the stupid thing i due first thing Monday morning. I thought I
had it pretty much working (except that everything came out in the
wrong order) but the teaching assistant was able to point out the
problem. (A pointer is a number so it will compare but not using the
overloaded < from the class, just the memory address).

I don't know if I have explained this well enough but I can't figure
this out.

We can't really help you except for the obvious. You might consider
using std::cout to output your array indexing mechanism

I will gladly output my array indexing mechanism...
except I don't know what you mean.

I know what an array index is, I am not sure I understand what you mean
by mechanism however. The Priority Q is using an array to store it's
objects (as you saw) and when the array is full it doubles in size.

If it helps I can tell you that it is having the problem at the FIRST
insert, not after several.

I will look at your comments some more and see if I can get my very
tired brain to understand it. (I gave up at about 3:00 a.m.). I will
now look up 'interlaced array' (among others) to see if that is what is
happening.

Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top