-6

I am currently trying to output the direction using Boolean values.

1 is true. 0 is false.

1 signifies the elevator going up. 0 signifies the elevator going down.

I want the output to be :

Node # : Timestamp. Current(User Floor). Destination(User Floor). Direction(User is heading).

Node 0 : 1 3 7 true
Node 1 : 1 2 9 true
Node 2 : 1 7 9 true
Node 3 : 2 4 6 true
Node 4 : 2 4 8 true
Node 5 : 2 1 17 true 
Node 6 : 5 1 15 true
Node 7 : 5 5 1 false
Node 8 : 6 17 4 false
Node 9 : 6 4 17 true

Instead I am getting as output:

Node 0 : 1 3 7 205
Node 1 : 1 2 9 205
Node 2 : 1 7 9 205
Node 3 : 2 4 6 205
Node 4 : 2 4 8 205
Node 5 : 2 1 17 205 
Node 6 : 5 1 15 205
Node 7 : 5 5 1 205
Node 8 : 6 17 4 205
Node 9 : 6 4 17 205

The code itself compiles with no errors. I have no clue where my logic went wrong. When I was trying to figure out a way to assign the direction of the user's making requests for the elevator to service it(the request the user makes).

reqNode *temp = new reqNode;

if(temp->start < temp->destination)
temp->set_dir(true);
else
temp->set_dir(false);

Was the logic I used.

the T1.txt file used:

1 3 7
1 2 9
1 7 9
2 4 6
2 4 8
2 1 17
5 1 15
5 5 1
6 17 4
6 4 17

Here are my files(the code in entirety)

binaryHeap.h file:

#include <vector>
using namespace std;


template <class Comparable>
class BinaryHeap
{

public:
BinaryHeap( ): Array( 11 ), theSize( 0 ){}


bool isEmpty( ) const
{
    return theSize == 0;
}

const Comparable & findMin( ) const
{
    if( isEmpty( ) )
    {
    cout << "heap empty" << endl; //throw UnderflowException( );
    //break;
    }
return Array[ 1 ];
}

void insert( const Comparable & x)
{
    Array[ 0 ] = x;   // initialize sentinel

    if( theSize + 1 == Array.size( ) )
      Array.resize( Array.size( ) * 2 + 1 );

    // Percolate up
    int hole = ++theSize;
        for( ; x < Array[ hole / 2 ]; hole /= 2 )
           Array[ hole ] = Array[ hole / 2 ];
           Array[ hole ] = x;
}

void deleteMin( )
{
    if( isEmpty( ) )
    {
    cout << "heap empty" << endl; //throw UnderflowException( );
    break;
    }
 Array[ 1 ] = Array[ theSize-- ];
 percolateDown( 1 );
}

void deleteMin( Comparable & minItem )
{
  minItem = findMin( );
  Array[ 1 ] = Array[ theSize-- ];
  percolateDown( 1 );
}

void makeEmpty( )
{
    theSize = 0;
}

private:

int theSize;  // Number of elements in heap
vector<Comparable> Array;    // The heap Array

void buildHeap( )
{
    for( int i = theSize / 2; i > 0; i-- )
    percolateDown( i );
}

void percolateDown( int hole )
{
  int child;
  Comparable tmp = Array[ hole ];

  for( ; hole * 2 <= theSize; hole = child )
  {
    child = hole * 2;
    if( child != theSize && Array[ child + 1 ] < Array[ child ] )
        child++;
    if( Array[ child ] < tmp )
        Array[ hole ] = Array[ child ];
    else
        break;
  }
 Array[ hole ] = tmp;
}
};

reqnode.h file:

class reqNode//create a node that takes in several properties.
{
public:

reqNode()
:direction(true)
{   
//default constructor
//initialize the memeber variables of this class.
priority = start = destination
= timestamp = start_time
= finish_time = -1;
set_dir(true);
}

reqNode(const reqNode &copy){       //copy constructor
priority = copy.priority;
start = copy.start;
destination = copy.destination;
timestamp = copy.timestamp;
start_time = copy.start_time;
finish_time = copy.finish_time;
}

reqNode & operator=(const reqNode & copy){ // operation overload
priority = copy.priority;
start = copy.start;
destination = copy.destination;
timestamp = copy.timestamp;
start_time = copy.start_time;
finish_time = copy.finish_time;

return *this;
}

bool operator<(const reqNode &rhs) const// operation overload
{
if(this->priority < rhs.priority)
return true;
else
return false;
}
void setPriority(int x){//assigning the priority to of whatever I declare "x" to be.
priority = x;
}

void calculate_priority()
{
if( start < destination )
priority = destination;  // Requests for higher floors will have lower priorities due to min heap structure
else
priority = 20 - destination;  // Requests for higher floors will have higher priorities
}

void set_dir(bool dir)
{
direction = dir;
}

//declare the member variables.
int priority, start, destination, 
timestamp, start_time, finish_time;
bool direction;
};

Elevator2.cpp file :

#include <iostream>
#include <fstream>
#include <stdio.h> //allows for the usage of getchar
#include "Elevator2.h"

void Elevator::displayMessage()
{
cout <<"Welcome to University Towers " << endl << endl << endl<<        "----------------------------" << endl;
}

double Elevator::get_total_cost()
{
return total_cost;
}

double Elevator::get_total_wait_time()
{
return total_wait_time;
}

bool Elevator::get_direction()
{
return direction;
}

void Elevator::change_direction()
{
direction = !direction;
}

void Elevator::set_direction(bool new_direction)
{
direction = new_direction;
}

int Elevator::get_elevator_floor()
{
return elevator_floor;
}

void Elevator::increment_elevator_floor()
{
if(direction) //if direction is TRUE (elevator moving upward) increment
elevator_floor++;
else
elevator_floor--;
}

void Elevator::set_elevator_floor(int new_floor)
{
elevator_floor = new_floor;
}

void Elevator::readRequests()
{
ifstream myStream("T1.txt");

while(!myStream.eof())
{
int timestamp ,currentFloor, destinationFloor;


myStream >> timestamp >> currentFloor >> destinationFloor;
//cout<< endl <<"The current timestamp is "<< timestamp << "The current floor is " <<  currentFloor 
//  << " and the destination floor is " << destinationFloor << endl << endl;
//cout<< endl;

reqNode *temp = new reqNode;

//initialize request node object
temp->timestamp = timestamp;
temp->start = currentFloor;
temp->destination = destinationFloor;
temp->start_time = -1;
temp->finish_time = -1;

//temp->direction = ( (currentFloor < destinationFloor) ? 1 : 0 );
temp->calculate_priority();

if(temp->start < temp->destination)
temp->set_dir(true);
else
temp->set_dir(false);

request.push(*temp);//push nodes into the request bank
}
int i = 0;
while( !request.empty() )
{

 cout << "Node " << i << " : " << request.front().timestamp << " " <<  request.front().start << " " << request.front().destination
<< " " <<  request.front().direction << endl;

//printf_s("%d\n", request.front().direction);

request.pop();//popping the request in order to test
i++;
}

//bool test = false;

//cout << test;

}

Elevator2.h file:

#include <iostream>
#include "binaryHeap.h"
#include "reqnode.h"
#include <queue>
using namespace std;

class Elevator
{
public:

//Elevator();//Default constructor
Elevator(int initial_floor)//Each object starts with a initial floor aka the current  floor.
:total_wait_time(0), total_cost(0), elevator_floor(initial_floor),   direction(true)//initialize each object accordingly.
{
readRequests();//for each elevator object...created(i.e - request read, there is a  node created).
}

void displayMessage();//display greeting text.
double get_total_cost();//returns the number of floors the elevator travels.
double get_total_wait_time();//returns the number of floors the elevator travels to  get to user.

bool get_direction();  // get the current direction of the elevator.
void set_direction(bool);  // sets direction to desired direction of the user.

void change_direction();  // reverses direction of the elevator.

int get_elevator_floor();// return the elevator floor(currently on).
void set_elevator_floor(int); // sets according to the user's request.

void increment_elevator_floor();  // moves elevator one floor determined by direction(dependent on the set direction bool).

protected://only calls from inherited/friendship classes are aloud. Never calls from the Driver.

queue<reqNode> waiting_queue;//queue stores reqNode objects, we name this queue - waiting queue.
queue<reqNode> request;// ""                  ""                                - request.
BinaryHeap<reqNode> service_queue;// ""                        ""               - service queue.
queue<reqNode>  finished;//""                      ""                           - finished.

private:

double total_wait_time;//number of floors the elevator travels to get to user.
double total_cost;//number of floors the elevator travels while servicing.
int elevator_floor;//initial floor value.
bool direction;//iterating through the floors for the sabbath algorithm
void readRequests();//read the in the requests per user and store them(by way of the request queue/bank).  
};

lazy.cpp file:

#include "lazy.h"

void Lazy::setAttributes(reqNode Nodey) 
{

int costToserve = abs(Nodey.start - Nodey.destination);//the floors between user's start_floor and user's destination_floor.
int directionOf = (Nodey.start - Nodey.destination) * -1;//the direction the elevator is heading.
int priority = abs(Nodey.destination - get_elevator_floor());//already in the servicing queue.

}

void Lazy::simulation()
{
time = 0;

while( !request.empty() )  //  while request remain, do:
{
//  check for and read new requests into incoming/waiting queue
while(request.front().timestamp == time)  //  schedule new requests
{
waiting_queue.push(request.front());  // places next request into waiting queue
request.pop();
}

scheduler();  // inputs waiting requests into servicing queue based on elevator's current state

if( !service_queue.isEmpty() )  //  if( !service_queue.isEmpty() )
{
if( service_queue.findMin().direction != get_direction() )  //  if(top node of service queue's direction == current direction of the elevator)
           {                                    
change_direction();//then we change the direction.
}//  else set direction to top node's direction

increment_elevator_floor();  // increments/decrements elevator floor by one.


while( service_queue.findMin().destination == get_elevator_floor() )//check top node to see if it is done;  current floor is destination
{
reqNode * temp;

service_queue.deleteMin(*temp); // min item is serviced; pop from heap
temp->finish_time = time; 
finished.push(*temp);  //  place serviced node to finished bank
}

}
increment_elevator_floor();
time++;
}
}

void Lazy::scheduler()
{
// UPDATE:  PRIORITY TO BE CALCULATED BY REFERENCE POINT (DETERMINED BY DIRECTION) IN DRIVER BY CALL TO REQNODE METHOD

int size = waiting_queue.size(); //set to wait queue's size

if( !service_queue.isEmpty() )
{
for(int i = 0; i < size; i++)//check waiting queue for( i = 0, i < size, i ++)
{
if( waiting_queue.front().start == get_elevator_floor() && waiting_queue.front().direction == get_direction() )//if node's floor == current floor 
{
waiting_queue.front().start_time = time;
service_queue.insert( waiting_queue.front() );  //put in service queue
}
else
{
reqNode *temp = &waiting_queue.front(); //put back in queue
waiting_queue.push(*temp);
waiting_queue.pop();
}
}
}
else
{

if( !waiting_queue.empty() )//check waiting queue (if not empty)
{
/*  Elevator should take the direction of next request waiting and begin moving in that direction.
Elevator should pick up requests that are on the way and in same direction as next waiting request
*/
set_direction(waiting_queue.front().direction);

for(int i = 0; i < size; i++)//check waiting queue for( i = 0, i < size, i ++)
{
if( waiting_queue.front().start == get_elevator_floor() && waiting_queue.front().direction == get_direction() )//if node's floor == current floor 
{
waiting_queue.front().start_time = time;
service_queue.insert( waiting_queue.front() );  //put in service queue
}
else
{
reqNode *temp = &waiting_queue.front(); //put back in queue
waiting_queue.push(*temp);
waiting_queue.pop();
}
}   
}

}

}

lazy.h file:

#include "Elevator2.h"

class Lazy : public Elevator
{   
public:
Lazy(int initial_floor)//initialize the same way you did your base class. same parameters.
:Elevator(initial_floor){}

void simulation();//carries out the normal elevator simulation

private:

void scheduler();//scheduler to handle. 
void setAttributes(reqNode);//set attributes of each request node.
int time;
};   

Ok that's all the files. WOW I think my space bar key is broke from all that 4x spacing! GEEZ.

Finally the driver2.cpp: (where all the magic happens)

#include <iostream>
#include <fstream>
#include <string>
#include "lazy.h"
#include <algorithm>
using namespace std;



void setNode(reqNode nizzode, int priority)
{
nizzode.priority = priority;
}


int main()
{
Lazy lazy(1);
}

That's it.

Now my code takes in the 10 user requests from T1.txt file and generates a request node that has the attributes Timestamp(the floor in which it was requested at), and the Current floor(the originating floor of the user making the reques), and finally the Destination floor(user's desired destination). I also thought I had the logic down to have the user's direction based on the current floor and destination floor. Boy was I wrong. Help would be gladly appreciated!

Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • 1
    Any non-zero value is considered `true`. – Some programmer dude Oct 30 '12 at 16:38
  • 2
    now add your Makefile as well as configure script. you may add some README also – Neel Basu Oct 30 '12 at 16:39
  • 3
    I'll bet you can get this code down to a dozen lines that compile, run, and illustrate the problem. At that point, either the answer will be obvious or you'll find people willing to read it and identify the problem. – Pete Becker Oct 30 '12 at 16:40
  • [`std::cout << std::boolalpha << request.front().direction`](http://en.cppreference.com/w/cpp/io/manip/boolalpha) – Mooing Duck Oct 30 '12 at 16:40
  • 1
    @NeelBasu - no, please! Less is better! 99% of the code here has nothing to do with the problem. – Pete Becker Oct 30 '12 at 16:41
  • 2
    You have a memory leak in your `while(!myStream.eof())` loop. You call `new`, but nenver call `delete`. Solution: replace `reqNode *temp = new reqNode;` with `reqNode temp;` and make the syntax changes that that implies. – Robᵩ Oct 30 '12 at 16:47
  • way too much code. http://sscce.org/ – njzk2 Oct 01 '14 at 20:44
  • @NeelBasu people never get jokes in comments... – nekomatic Oct 03 '14 at 08:14
  • Related: *[What is the printf format specifier for bool?](https://stackoverflow.com/questions/17307275/what-is-the-printf-format-specifier-for-bool)*. See also [the linked questions](https://stackoverflow.com/questions/linked/17307275?sort=votes). – Peter Mortensen Aug 07 '23 at 20:19

1 Answers1

10

205 is hex of 0xCD which is one of the possible fillers for uninitialized data in Visual Studio when working with a debug build.
You need to debug your code and trace back where this uninitialized data comes from.
Nobody here will debug it for you.

shoosh
  • 76,898
  • 55
  • 205
  • 325
  • I know this is old. However, I agree with shoosh. It's most likely where a bool variable hasn't been initialized to either true or false. I've seen something similar. – iCode Feb 26 '16 at 15:22