C++ Programming Homework

profileajayreddy222
Problems.zip

Problems/.DS_Store

__MACOSX/Problems/._.DS_Store

Problems/problem2/fifo.cpp

Problems/problem2/fifo.cpp

//
//  main.cpp
//  FIFO
//
//  Created by Zhimin Gao on 10/3/19.
//  Copyright © 2019 Zhimin Gao. All rights reserved.
//

#include   < iostream >
#include   < chrono >
#include   "fifo.h"

using   namespace  std ;
using   namespace  std :: chrono ;

void  FIFO :: CreateProcess ()   {
    cout  <<   "Enter number of process: " ;
    cin  >>  num_proc ;
    
     for   ( int  i = 1 ;  i <= num_proc ;   ++ i )   {
        PCB  *  new_proc  =  NULL ;
        new_proc  =   new  PCB ;
         if ( NULL  ==  new_proc )   {
            cerr  <<   "Error due to allocating memory for new PCB"   <<  endl ;
             return ;
         }
        
        cout  <<   "Enter the name of "   <<  i  <<   "th process: " ;
        cin  >>  new_proc -> name ;
        
        cout  <<   "Enter the arriving time of "   <<  i  <<   "th process: " ;
        cin  >>  new_proc -> ArriveTime ;
        
        cout  <<   "Enter the service time of "   <<  i  <<   "th process:" ;
        cin  >>  new_proc -> ServiceTime ;
        
        new_proc -> next  =  NULL ;
        
         //Insert the new process into the correct position so the head arrived earliest, then its next, then its next's next, then so on.

         //TO-DO: Complete your codes here.

         //Set other arrtributes for the new process.
        new_proc -> StartTime = 0 ;
        new_proc -> FinshTime = 0 ;
        new_proc -> TurnAroundTime = 0 ;
        new_proc -> state =   'W' ;
        new_proc -> RunTime = 0 ;
        new_proc -> NeedTime = 0 ;
     }
}

void  FIFO :: Schedule ()   {
    PCB  *  current_proc  =  head ;
    PCB  *  new_proc  =  NULL ;
     while ( current_proc )   {
         if ( current_proc -> state  ==   'W' )   {
            new_proc  =  current_proc ;
             RunProcess ( new_proc );
         }
        current_proc  =  current_proc -> next ;
        head  =  current_proc ;
        free ( new_proc );
        new_proc  =  NULL ;
     }
    
    cout  <<   "Average turn-around time is "   <<   ( double ) sum_turn_around_time / ( double ) num_proc  <<  endl ;
}

void  FIFO :: RunProcess ( PCB  *  proc )   {
     //get current time
    proc -> StartTime   =  time ;
    cout  <<   "["   <<  proc -> StartTime   <<   "]: Current process "   <<  proc -> name  <<   " starts."   <<  endl ;
    
    time += proc -> ServiceTime ;
    proc -> state  =   'R' ;   // Set the currect state to running
    proc -> FinshTime   =  time ;   //Set the finish time
    proc -> TurnAroundTime   =  proc -> FinshTime   -  proc -> ArriveTime ;   //Set the whole process time
    
    sum_turn_around_time  +=   ( double ) proc -> TurnAroundTime ;
    
}

int  main ( int  argc ,   const   char   *  argv [])   {
    FIFO fifo ;
    fifo . CreateProcess ();
    fifo . Schedule ();
     return   0 ;
}

__MACOSX/Problems/problem2/._fifo.cpp

Problems/problem2/fifo.h

// // fifo.h // Programming Assisgnment 2 // // Created by Zhimin Gao on 10/3/19. // Copyright © 2019 Zhimin Gao. All rights reserved. // #ifndef fifo_h #define fifo_h #include "pcb.h" class FIFO : public Scheduler{ private: double sum_turn_around_time; //sum of total turn-around time int num_proc; //number of processes PCB * head; //the head of the queue PCB * tail; //the tail of the queue long time; //indicate the current time public: FIFO() { sum_turn_around_time = 0; num_proc = 0; head = NULL; tail = NULL; } void CreateProcess(); void Schedule(); void RunProcess(PCB * proc); }; #endif /* fifo_h */

__MACOSX/Problems/problem2/._fifo.h

Problems/problem2/pcb.h

// // pcb.h // Programming Assisgnment 2 // // Created by Zhimin Gao on 10/3/19. // Copyright © 2019 Zhimin Gao. All rights reserved. // #ifndef pcb_h #define pcb_h #include <iostream> #include <string> using namespace std; typedef struct PCB_ { string name; //Process's name char state; //Process's state: R=running; W=waiting long ArriveTime; //Process's arrival time long StartTime; //Process's start time long FinshTime; //Process's finish time long ServiceTime; //Process's execution time double TurnAroundTime; //Process's turn-around time long RunTime; //Time that a process has run long NeedTime; //Time that a process still needs to run struct PCB_ *next; //Points to the next process in list }PCB; class Scheduler { public: virtual void RunProcess(PCB * proc) = 0; virtual void Schedule() = 0; virtual void CreateProcess() = 0; }; #endif /* pcb_h */

__MACOSX/Problems/problem2/._pcb.h

__MACOSX/Problems/._problem2

Problems/problem3/lfu.h

// // lfu.h // Programming Assisgnment 2 // // Created by Zhimin Gao on 10/4/19. // Copyright © 2019 Zhimin Gao. All rights reserved. // #ifndef lfu_h #define lfu_h #include <map> using namespace std; #define DEFAULT_MAX_SIZE 3 class HitRate { public: int Key; int HitCount; long LastHitTime; HitRate(): Key(0), HitCount(0), LastHitTime(0) {} HitRate(int key, int hitcount, long lasttime): Key(key), HitCount(hitcount), LastHitTime(lasttime) {} }; class LFUCache { private: int capacity = DEFAULT_MAX_SIZE; //Store the frequency and last hit time map<int,HitRate> cache; //Store the KV map<int, int> KV; public: LFUCache(int cap) : capacity(cap) {} void set(int key, int value); int get(int key); }; #endif /* lfu_h */

__MACOSX/Problems/problem3/._lfu.h

Problems/problem3/lfu.cpp

Problems/problem3/lfu.cpp

//
//  main.cpp
//  LFUCache
//
//  Created by Zhimin Gao on 10/4/19.
//  Copyright © 2019 Zhimin Gao. All rights reserved.
//

#include   < iostream >
#include   "lfu.h"

static   bool  compare ( pair < int ,   HitRate >  l ,  pair < int ,   HitRate >  r )   {
     //First compare the numbers of use
     int  hr  =  l . second . HitCount   -  r . second . HitCount ;
     if ( hr  !=   0 )
         return  l . second . HitCount   <  r . second . HitCount ;
     else   {    //If numbers of use are the same, compare the last use time.
         return  l . second . LastHitTime   <  r . second . LastHitTime ;
     }
}

//Get the Least Frequently Used entry "min"
HitRate  getMin ( map < int , HitRate >  mymap )   {
    pair < int ,   HitRate >  min  =   * min_element ( mymap . begin (),  mymap . end (),   & compare );
     return  min . second ;
}

void   LFUCache :: set ( int  key ,   int   value )   {
/***Implement your codes here ***/
}

int   LFUCache :: get ( int  key )   {
/***Implement your codes here ***/
}

int  main ( int  argc ,   const   char   *  argv [])   {
     LFUCache  cache ( 3 );
    
    cache . set ( 2 ,   2 );
    cache . set ( 1 ,   1 );
    
    cout  <<  cache . get ( 2 )   <<  endl ;
    cout  <<  cache . get ( 1 )   <<  endl ;
    cout  <<  cache . get ( 2 )   <<  endl ;
    
    cache . set ( 3 ,   3 );
    cache . set ( 4 ,   4 );
    
    cout  <<  cache . get ( 3 )   <<  endl ;
    cout  <<  cache . get ( 2 )   <<  endl ;
    cout  <<  cache . get ( 1 )   <<  endl ;
    cout  <<  cache . get ( 4 )   <<  endl ;
    
     return   0 ;
}

__MACOSX/Problems/problem3/._lfu.cpp

__MACOSX/Problems/._problem3

__MACOSX/._Problems