From 9296a4bd149d3161fd06a28f00a18020b4bd1dc4 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Tue, 5 Nov 2024 16:42:49 +0530 Subject: [PATCH] Made some minor changes to B7 (page replacement in C++) code. - Changed variable names - Indentation and stuff - Added description --- Codes/Group B/Code-B7.cpp | 223 +++++++++++++++++++------------------- 1 file changed, 111 insertions(+), 112 deletions(-) diff --git a/Codes/Group B/Code-B7.cpp b/Codes/Group B/Code-B7.cpp index 8908ca2..6d72247 100644 --- a/Codes/Group B/Code-B7.cpp +++ b/Codes/Group B/Code-B7.cpp @@ -1,136 +1,135 @@ -#include +// Assignment-B7 - Page Replacement in C++ (LRU + Optimal) +// BEGINNING OF CODE +#include +#include +#include using namespace std; -int refstring[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1}; -int win, hit = 0, miss = 0, res[4], lastUsed[40]; +int refString[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1}; +int window = 0, hit = 0, miss = 0, currentPages[4], lastUsed[40]; +int len = sizeof(refString) / sizeof(refString[0]); int findLRU() { - int minIndex = 0; - for(int i = 1; i < win; i++){ - if(lastUsed[i] < lastUsed[minIndex]){ - minIndex = i; - } + int min_index = 0; + for (int i=1; i farthest){ - farthest = j; - maxIndex = i; - } - break; - } +int findOptimal(int current_index) { + int max_index = -1, farthest = current_index; + for (int i=0; i farthest) { + farthest = j; + max_index = i; } - if(j == len) - return i; + break; + } } - return maxIndex; + if (j == len) { + return i; + } + } + return max_index; } -void optimal(int rf[], int len){ - hit = 0; - miss = 0; +void Optimal() { + hit = 0; + miss = 0; - for(int i = 0; i < win; i++){ - res[i] = -1; + for (int i=0; i>win; - int len = sizeof(refstring)/sizeof(refstring[0]); - - cout<<"\nBy LRU algorithm: "; - lru(refstring, len); - display(); - - cout<<"\n\nBy Optimal Algorithm: "; - optimal(refstring, len); - display(); +int main() { + cout<>window; + LRU(); + display(); + Optimal(); + display(); } +// END OF CODE