S
SB
I have a class called Process with the following...
class Process
{
private:
int processID; // 2 digit number starting at 10
int memorySize; // size of memory allocated to each process
int timeQuantum; // execution time of each process
public:
Process(); // default constructor
void setProcessID(); // sets the process id
int getProcessID() const; // returns the process id
void setMemorySize(int); // sets the memory size
int getMemorySize() const; // returns the memory size
void setTimeQuantum(); // sets the time quantum
int getTimeQuantum() const; // returns the time quantum
};
Then I have a Scheduler class which contains Process object. Here is the
Scheduler class...
class Scheduler
{
private:
Process p; // holds a Process object
int processes; // number of process input by user
vector<Process> schedulerVector; // vector containing the process id, mem
size and time quantum of each process
public:
Scheduler(); // default constructor
void setProcesses(); // sets the number of processes
int getProcesses() const; // returns the number of processes
Process createProcess(int); // creates a new Process object
void createVector(); // populates the vector with the processes
vector<Process> getVector() const; // returns the vector of Processes
void displayVector() const; // displays contents of the vector
void swapVectorElements(Process&, Process&);// swaps two Process objects in
the vector
void sortVector(vector<Process>&); // sorts the vector based on time
quantum
void displayTimeQuantum() const; // displays processes according to
execution time
};
In the Scheduler class I create a vector and populate it with Process
objects, like so...
void Scheduler::createVector()
{
// populate the vector using processes as the loop counter since
// size of the vector is bound to the number of processes
for (int i = 0; i < processes; i++)
{
Process p = createProcess(i); // create a new Process
schedulerVector.push_back(p); // add each Process to the vector
}
}
I want to sort the vector based on the timeQuantum field in the Process
class. When I call the sort function, it seems to work but when I display
the vector after sorting, it is in the same order as it was before sorting.
I'm using pass by reference to swap as well. Here is the code...
void Scheduler::swapVectorElements(Process& p1, Process& p2)
{
cout<<endl<<"Stub for swapVectorElements"<<endl;
Process temp;
temp = p1;
p1 = p2;
p2 = temp;
}
void Scheduler::sortVector(vector<Process>& v)
{
cout<<endl<<"Sorting the vector of size "<<v.size()<<"..."<<endl;
int i;
int j;
for (i = 0; i < v.size(); i++)
{
for (j = 0; j < v.size(); j++)
{
if (v[j].getTimeQuantum() > v[j+1].getTimeQuantum())
{
swapVectorElements(v[j], v[j+1]);
}
}
}
}
Can anyone see why this is not working?
Thanks!
class Process
{
private:
int processID; // 2 digit number starting at 10
int memorySize; // size of memory allocated to each process
int timeQuantum; // execution time of each process
public:
Process(); // default constructor
void setProcessID(); // sets the process id
int getProcessID() const; // returns the process id
void setMemorySize(int); // sets the memory size
int getMemorySize() const; // returns the memory size
void setTimeQuantum(); // sets the time quantum
int getTimeQuantum() const; // returns the time quantum
};
Then I have a Scheduler class which contains Process object. Here is the
Scheduler class...
class Scheduler
{
private:
Process p; // holds a Process object
int processes; // number of process input by user
vector<Process> schedulerVector; // vector containing the process id, mem
size and time quantum of each process
public:
Scheduler(); // default constructor
void setProcesses(); // sets the number of processes
int getProcesses() const; // returns the number of processes
Process createProcess(int); // creates a new Process object
void createVector(); // populates the vector with the processes
vector<Process> getVector() const; // returns the vector of Processes
void displayVector() const; // displays contents of the vector
void swapVectorElements(Process&, Process&);// swaps two Process objects in
the vector
void sortVector(vector<Process>&); // sorts the vector based on time
quantum
void displayTimeQuantum() const; // displays processes according to
execution time
};
In the Scheduler class I create a vector and populate it with Process
objects, like so...
void Scheduler::createVector()
{
// populate the vector using processes as the loop counter since
// size of the vector is bound to the number of processes
for (int i = 0; i < processes; i++)
{
Process p = createProcess(i); // create a new Process
schedulerVector.push_back(p); // add each Process to the vector
}
}
I want to sort the vector based on the timeQuantum field in the Process
class. When I call the sort function, it seems to work but when I display
the vector after sorting, it is in the same order as it was before sorting.
I'm using pass by reference to swap as well. Here is the code...
void Scheduler::swapVectorElements(Process& p1, Process& p2)
{
cout<<endl<<"Stub for swapVectorElements"<<endl;
Process temp;
temp = p1;
p1 = p2;
p2 = temp;
}
void Scheduler::sortVector(vector<Process>& v)
{
cout<<endl<<"Sorting the vector of size "<<v.size()<<"..."<<endl;
int i;
int j;
for (i = 0; i < v.size(); i++)
{
for (j = 0; j < v.size(); j++)
{
if (v[j].getTimeQuantum() > v[j+1].getTimeQuantum())
{
swapVectorElements(v[j], v[j+1]);
}
}
}
}
Can anyone see why this is not working?
Thanks!