Hi. Why am I getting different output when I call the calculate() function?
The output on line 184 gives 1.0001, while the output from line 433 gives 1.
Thanks.
#include <iostream>
#include <string>
#include <limits>
#include <vector>
#include <iomanip>
using namespace std;
/**
* Global Variables
*/
string k;
int pmem, plen;
int dmem, dlen;
int cmem, clen;
string dname, pname, cname, drw, prw, crw;
/**
* Process Control Block Class
*/
class PCB
{
public:
PCB() : pid( 0 ), filename ( "InitialString" ), memoryLocation ( 0 ), rw ( "InitialString" ), tau ( 0 ), alpha ( 0.0 ) { }
void setPid( int p );
int getPid() const;
int incrementPid();
int decrementPid();
void setFilename( string f );
string getFilename() const;
void setMemoryStart( int m );
int getMemoryStart() const;
void setFileLength( int l );
int getFileLength() const;
void setRW( string r );
string getRW() const;
void setTime( int t );
int getTime() const;
void setTau( float t );
float getTau();
void setAlpha( float a );
float getAlpha() const;
void setCylin( int cc );
int getCylin() const;
float calculate();
private:
int pid;
string filename;
int memoryLocation;
string rw;
int fileLen;
int procTime;
float tau;
float alpha;
int cylinder;
};
struct computer
{
int printer;
int disk;
int cd;
int num;
vector<PCB> readyQueue;
vector<PCB> diskQueue;
vector<PCB> diskQueueCopy;
vector<PCB> printerQueue;
vector<PCB> printerQueueCopy;
vector<PCB> cdQueue;
vector<PCB> cdQueueCopy;
vector<int> time;
void screen();
void prompt();
void runningPrompt();
int findMin( vector<int> v );
int findPos( vector<int> vv );
void removeElem();
};
/**
* Sys Gen Process begins here. Prompt the user for input.
*/
void computer::prompt()
{
cout << "Welcome to the Sys Gen Process"<<endl;
cout<<"Please enter the number of printers in the system (between 1 and 9)"<<endl;
while( ! ( cin >> printer ) )
{
cout << "That was not an integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
while(printer<1 || printer>9)
{
cout<<"Sorry, that is an invalid value, please re-enter amount of printers:"<<endl;
while( ! ( cin >> printer ) )
{
cout << "That was not an integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
}
cout<<"Thank you. Please enter the number of disks in the system (between 1 and 9)"<<endl;
while( ! ( cin >> disk ) )
{
cout << "That was not an integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
while(disk<1 || disk>9)
{
cout<<"Sorry, that is an invalid value, please re-enter amount of disks:"<<endl;
while( ! ( cin >> disk ) )
{
cout << "That was not an integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
}
cout<<"Please enter the number of CD/RW's in the system (between 1 and 9)"<<endl;
while( ! ( cin >> cd ) )
{
cout << "That was not an integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
while(cd<1 || cd>9)
{
cout<<"Sorry, that is an invalid value, please re-enter amount of CD/RW's:"<<endl;
while( ! ( cin >> cd ) )
{
cout << "That was not an integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
}
}
/**
* This runningPrompt Function receives input from the keyboard regarding processes
*/
void computer::runningPrompt()
{
cout << "You may enter a command or type 'exit' to exit the system." << endl;
PCB process;
while (k != "exit")
{
cout << "Please enter command: ";
cin >> k;
if( k[0] == 'A' )
{
cout << "New Process created." << endl;
process.incrementPid();
cout << "Pid is " << process.getPid() << endl;
readyQueue.push_back( process );
int kk;
cout << "please enter the number of cylinders: " << endl;
cin >> kk;
process.setCylin( kk );
float k1;
cout << "Please enter the history parameter. " << endl;
cout << "Example: 0.9 " << endl;
cin >> k1;
process.setAlpha( k1 );
float k2;
cout << "Please enter the burst time in milliseconds. " << endl;
cout << "Example: enter 5 for 5 milliseconds, " << endl;
cin >> k2;
process.setTau( k2 );
cout << "Number: " << process.calculate() << endl;
}
if( k[0] == 't' )
{
if( !readyQueue.empty() )
{
cout << "Process terminating." << endl;
readyQueue.clear();
process.decrementPid();
}
else
{
cout << "Ready vector is empty." << endl;
}
}
if( k[0] == 'S' )
{
screen();
}
if( k[1] == '1' )
num = 1;
if( k[1] == '2' )
num = 2;
if( k[1] == '3' )
num = 3;
if( k[1] == '4' )
num = 4;
if( k[1] == '5' )
num = 5;
if( k[1] == '6' )
num = 6;
if( k[1] == '7' )
num = 7;
if( k[1] == '8' )
num = 8;
if( k[1] == '9' )
num = 9;
if ( k[0] == 'd' )
{
if( disk >= num )
{
if( !readyQueue.empty() )
{
cout << "This is a system call requesting Disk " << num << endl;
cout << "What is the filename? Please enter it: " << endl;
cin >> dname;
process.setFilename( dname );
cout << "What is the starting location in memory? Please enter it:" << endl;
while( ( ! ( cin >> dmem ) ) || dmem < 0 )
{
cout << "That was not a positive integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
process.setMemoryStart( dmem );
cout << "Please enter the length of your file in an integer value:" << endl;
while( ( ! ( cin >> dlen ) ) || dlen < 0 )
{
cout << "That was not positive integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
process.setFileLength( dlen );
cout << "Please enter r for read or w for write" << endl;
cin >> drw;
while( ! ( drw == "r" || drw == "w" ) )
{
cout << "Please enter a r or a w" << endl;
cin >> drw;
}
process.setRW( drw );
diskQueue.push_back( process );
diskQueueCopy.push_back( process );
process.decrementPid();
readyQueue.pop_back();
}
else
{
cout << "There are no processes in the ready vector." << endl;
}
}
else
{
cout << "Please enter a valid disk number" << endl;
}
}
if ( k[0] == 'p' )
{
if( printer >= num )
{
if( !readyQueue.empty() )
{
cout << "This is a system call requesting printer: " << num << endl;
cout << "What is the filename? Please enter it: " << endl;
cin >> pname;
process.setFilename( pname );
cout << "What is the starting location in memory? Please enter it:" << endl;
while( ( ! ( cin >> pmem ) ) || pmem < 0 )
{
cout << "That was not a positive integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
process.setMemoryStart( pmem );
cout << "Please enter the length of your file in an integer value:" << endl;
while( ( ! ( cin >> plen ) ) || plen < 0 )
{
cout << "That was not positive integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
process.setFileLength( plen );
printerQueue.push_back( process );
printerQueueCopy.push_back( process );
process.decrementPid();
readyQueue.pop_back();
}
else
{
cout << "There are no processes in the ready vector." << endl;
}
}
else
{
cout << "Please enter a valid printer number" << endl;
}
}
if ( k[0] == 'c' )
{
if( cd >= num )
{
if( !readyQueue.empty() )
{
cout << "This is a system call requesting CD/RW: " << num << endl;
cout << "What is the filename? Please enter it: " << endl;
cin >> cname;
process.setFilename( cname );
cout << "What is the starting location in memory? Please enter it:" << endl;
while( ( ! ( cin >> cmem ) ) || cmem < 0 )
{
cout << "That was not a positive integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
process.setMemoryStart( cmem );
cout << "Please enter the length of your file in an integer value:" << endl;
while( ( ! ( cin >> clen ) ) || clen < 0 )
{
cout << "That was not positive integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
process.setFileLength( clen );
cout << "Please enter r for read or w for write" << endl;
cin >> crw;
while( ! ( crw == "r" || crw == "w" ) )
{
cout << "Please enter a r or a w" << endl;
cin >> crw;
}
process.setRW( crw );
cdQueue.push_back( process );
cdQueueCopy.push_back( process );
process.decrementPid();
readyQueue.pop_back();
}
else
{
cout << "There are no processes in the ready vector." << endl;
}
}
else
{
cout << "Please enter a valid CD/RW number. " << endl;
}
}
if( k[0] == 'D' )
{
if( !diskQueue.empty() )
{
readyQueue.push_back( process );
process.incrementPid();
diskQueue.pop_back();
diskQueueCopy.pop_back();
}
else
{
cout << "The disk queue is empty. " << endl;
}
}
if( k[0] == 'P' )
{
if( !printerQueue.empty() )
{
readyQueue.push_back( process );
process.incrementPid();
printerQueue.pop_back();
printerQueueCopy.pop_back();
}
else
{
cout << "The printer queue is empty. " << endl;
}
}
if( k[0] == 'C' )
{
if( !cdQueue.empty() )
{
readyQueue.push_back( process );
process.incrementPid();
cdQueue.pop_back();
cdQueueCopy.pop_back();
}
else
{
cout << "The CD/RW vector is empty. " << endl;
}
}
}
}
/**
* User can get a "Screen Shot" but typing "S" at command line.
*/
void computer::screen()
{
PCB p1;
cout << "Taking Snapshot." << endl;
cout << "Please select r, d, p, or c." << endl;
string rpdc;
cin >> rpdc;
while ( ! ( rpdc[0] == 'r' || rpdc[0] == 'p' || rpdc[0] == 'd' || rpdc[0] == 'c' ) )
{
cout << "Please enter r, p, d, or c. Thank you." << endl;
cout << "Enter selection" << endl;
cin >> rpdc;
}
if( rpdc[0] == 'r' )
{
cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
<< left << setw(14) << "Memstart" << " R/W" << endl;
if( !readyQueue.empty() )
{
for( int i = 0; i < readyQueue.size(); i++ )
{
cout << left << setw(0) << "" << i+1 << left << setw(4) << " "
<< left << setw(14) << " " << left << setw(14) << " "
<< left << setw(14) << " " << " " << p1.calculate() << endl;
}
}
else
{
cout << left << setw(0) << "" << "0" << left << setw(4) << " "
<< left << setw(14) << " " << left << setw(14) << " "
<< left << setw(14) << " " << " " << endl;
}
}
if( rpdc[0] == 'p' )
{
cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
<< left << setw(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
if( !printerQueueCopy.empty() )
{
for( int i = 0; i < printerQueue.size(); i++ )
{
cout << setw(0) << "--p" << i+1 << setw(5) << " "
<< setw(14) << printerQueueCopy.front().getFilename() << setw(14) << printerQueueCopy.front().getMemoryStart()
<< setw(14) << printerQueueCopy.front().getFileLength() << " - " << endl;
printerQueueCopy.clear();
}
}
else
{
cout << left << setw(0) << "" << "0" << left << setw(4) << " "
<< left << setw(14) << " " << left << setw(14) << " "
<< left << setw(14) << " " << " " << endl;
}
printerQueueCopy = printerQueue;
}
if( rpdc[0] == 'd' )
{
cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
<< left << setw(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
if( !diskQueueCopy.empty() )
{
for( int i = 0; i < diskQueue.size(); i++ )
{
cout << setw(0) << "--d" << i+1 << setw(6) << " "
<< setw(14) << diskQueueCopy.front().getFilename() << setw(14) << diskQueueCopy.front().getMemoryStart()
<< setw(14) << diskQueueCopy.front().getFileLength() << diskQueueCopy.front().getRW() << endl;
diskQueueCopy.clear();
}
}
else
{
cout << left << setw(0) << "" << "0" << left << setw(4) << " "
<< left << setw(14) << " " << left << setw(14) << " "
<< left << setw(14) << " " << " " << endl;
}
diskQueueCopy = diskQueue;
}
if( rpdc[0] == 'c' )
{
cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
<< left << setw(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
if( !cdQueueCopy.empty() )
{
for( int i = 0; i < cdQueue.size(); i++ )
{
cout << left << setw(0) << "--c" << i+1 << left << setw(5) << " "
<< left << setw(14) << cdQueueCopy.front().getFilename() << left << setw(14) << cdQueueCopy.front().getMemoryStart()
<< left << setw(14) << cdQueueCopy.front().getFileLength() << cdQueueCopy.front().getRW() << endl;
cdQueueCopy.clear();
}
}
else
{
cout << left << setw(0) << "" << "0" << left << setw(4) << " "
<< left << setw(14) << " " << left << setw(14) << " "
<< left << setw(14) << " " << " " << endl;
}
cdQueueCopy = cdQueue;
}
}
/**
* Member function definititions follow:
*/
void PCB::setPid(int p)
{
pid = p;
}
int PCB::getPid() const
{
return pid;
}
int PCB::incrementPid()
{
pid++;
}
void PCB::setFilename( string f )
{
filename = f;
}
string PCB::getFilename() const
{
return filename;
}
int PCB::decrementPid()
{
pid--;
}
void PCB::setMemoryStart( int m )
{
memoryLocation = m;
}
int PCB::getMemoryStart() const
{
return memoryLocation;
}
void PCB::setFileLength( int l )
{
fileLen = l;
}
int PCB::getFileLength() const
{
return fileLen;
}
void PCB::setRW( string r )
{
rw = r;
}
string PCB::getRW() const
{
return rw;
}
void PCB::setTime( int t )
{
procTime = t;
}
int PCB::getTime() const
{
return procTime;
}
int computer::findMin( vector<int> v )
{
int minIndex = 0;
for( int i = 1; i < v.size( ); i++ )
if( v[ minIndex ] > v[ i ] )
minIndex = i;
return v[ minIndex ];
}
int computer::findPos( vector<int> v )
{
int minIndex = 0;
for( int i = 1; i < v.size( ); i++ )
if( v[ minIndex ] > v[ i ] )
minIndex = i;
return minIndex;
}
void computer::removeElem()
{
time.erase( time.begin() + ( findPos( time ) ) );
}
void PCB::setTau( float t )
{
tau = t;
}
float PCB::getTau()
{
return tau;
}
void PCB::setAlpha( float a )
{
alpha = a;
}
float PCB::getAlpha() const
{
return alpha;
}
void PCB::setCylin( int cc )
{
cylinder = cc;
}
int PCB::getCylin() const
{
return cylinder;
}
float PCB::calculate()
{
return (getAlpha()*getTau())+((1-getAlpha()*getTau()));
}
/**
* main() begins here--
*/
int main()
{
computer c;
c.prompt();
c.runningPrompt();
return 0;
}