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>:ercolateUp ( 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.
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>:ercolateUp ( 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.