mercredi 25 septembre 2019

C++ Dual Stack randNum and clock() not functioning properly(HELP)

Alright so the code pretty much allocates space for a dualstack, has some structs that are pushed into the stack. Its all based on Star Wars so there are multiple ships coming in to fix, Tie Fighters, and Star Destroyers. Whether they are a TF or SD is based on a step function which is not working properly because of the rand() function not working properly. Its the same number over and over and over, and Im not sure why as it should change every loop. Also my times are very messed up as with the clock() function and im clueless about it so please help. Comment if you have any questions. I will be standing by. Thanks!

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAX = 20;
int vmr = 0;
int seed;
int upper;
int lower;
float randNum;
float randNumArr;
int dagabacount = 0;
int numberArrivals;
clock_t start;
double duration;
int fixedvehicles = 0;

//struct for vmr
struct vmr{
    char type;
    char name;
    int repairTime;
    int startTime;
    int finishTime;
};

//declaring dualstack
class repairStack
{
    private:
        int topT;
        int topS;
        int ele[MAX];

    public:
        repairStack();
        void pushTie   (struct vmr);
        void pushStar  (struct  vmr);
        int  popTie    (int *vmr); 
        int  popStar   (int *vmr);
}; 

//initializing dualstack
repairStack::repairStack()
{
    topT = -1;
    topS = MAX;
}

//pushing to tiestack
void repairStack::pushTie( struct vmr )
{
    if( topS == topT + 1 )
    {
        cout<<"\nStack Overflow tStack" << endl;
        dagabacount++;
        return;
    }

    topT++;
    ele[topT] = vmr;

    cout<<"\nInserted item in tStack : "<< vmr;    
}

//pushing to starstack
void repairStack::pushStar( struct vmr )
{
    if( topS == topT + 1 )
    {
        cout<<"\nStack Overflow sStack"<< endl;
        dagabacount++;
        return;
    }

    topS--;
    ele[topS] = vmr;

    cout<<"\nInserted item in sStack : "<< vmr;    
}

//popping to tiestack
int repairStack::popTie( int *vmr )
{
    if( topT == -1 )
    {
        cout<<"\nStack Underflow tStack"<< endl;
        return -1;
    }

    *vmr = ele[topT--];
    return 0;
}

//popping to starstack
int repairStack::popStar( int *vmr )
{
    if( topS == MAX )
    {
        cout<<"\nStack Underflow sStack"<< endl;
        return -1;
    }

    *vmr = ele[topS++];
    return 0;
}

int getNumArrivals(){
        srand((unsigned)time(NULL));
        randNumArr = float(rand()) / (float(RAND_MAX) + 1.0);
          if(randNumArr < 0.25){
             return 1;
          }
          else if(.25 <= randNumArr < .50){
              return 2;
          }
          else if(.50 <= randNumArr < .75){
              return 3;
          }
          else{
             return 4;
          }
}

int main()
{ 
    //asking user for bounds
    cout << "Please give seed value: ";
    cin >> seed;
    cout << "Please give lower bound of stack: ";
    cin >> lower;
    cout << "Please give upper bound of stack: ";
    cin >> upper;
    cout << MAX<< endl;

    //initalizing stack 's'
    repairStack s = repairStack();

    //initializing clock to 0 for first arrivals
    start = clock();
    duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
    cout<<"printf: "<< duration <<'\n';

    while(dagabacount < 5){
        //getting numberArrivals either 1,2,3,4
        numberArrivals = getNumArrivals();
        for(int i = 0; i < numberArrivals; i++){
        //getting random number between 0 and 1 to see if its T or S
          srand((unsigned)time(0));
          randNum = float(rand()) / (float(RAND_MAX) + 1.0);
          //if tie then push tie
          if(randNum < 0.75){
            struct vmr Tie_A;
            Tie_A.type='T';
            Tie_A.repairTime = 3;
            Tie_A.startTime = clock();
            Tie_A.finishTime = 0;
            Tie_A.name = 'A';
            s.pushTie(Tie_A);
          }
          //if star then push star
          else{
            struct vmr StarA;
            StarA.type='S';
            StarA.repairTime = 7;
            StarA.startTime = clock();
            StarA.finishTime = 0;
            StarA.name = 'A';
            s.pushStar(StarA);
          }
        }

        //fixing star if star stack is not empty, if so, fix a tiefighter if theres one in stack
        if(s.popStar(&vmr) == 0){
            cout << "fixed star" << endl;
            fixedvehicles++;
        }
        else{
            if(s.popTie(&vmr) == 0){
                cout << "fixed tie" << endl;
                fixedvehicles++;
            }
            else{
                cout << "Underflow" << endl;
            }
        }

    }
        cout << "5 Vehicles Rejected" << endl;
        cout << "# of Vehicles Serviced: " << fixedvehicles << endl;
        cout << "Average Time of Repair: " << endl;
    return 0;
}



Aucun commentaire:

Enregistrer un commentaire