-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOptimal_replacement.cpp
63 lines (62 loc) · 1.48 KB
/
Optimal_replacement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
int p[]={1,3,0,3,5,6,3};
int f=3;
int ans[3]={-1,-1,-1};
int x=0,ctr=0,counter=0;
int opt[3]={INT_MIN,INT_MIN,INT_MIN};
int m=10000,mindex=INT_MAX;
for(int k=0;k<7;k++){
int flag=0;
for(int i=0;i<f;i++){
if(p[k]==ans[i]){
flag=1;
for(int q=0;q<3;q++){
counter=0;
for(int r=k;r<7;r++){
if(ans[q]!=p[r])
counter++;
}
if(counter<m){
m=counter;
mindex=q;
}
}
}
}
if(x==3){
x=0;
}
if(flag==0){
cout<<endl<<p[k]<<" -- ";
if(x==mindex){
x++;
if(x==3){
x=0;
}
}
ans[x]=p[k];
x++;
ctr++;
}
if(flag==1){
cout<<endl<<p[k]<<" -- ";
}
if(flag==1){
cout<<"No Page Fault";
}
if(flag==0){
for(int i=0;i<f;i++){
if(ans[i]==-1){
cout<<" ";
}
else{
cout<<ans[i]<<" ";
}
}
}
}
cout<<"\nTotal page faults: "<<ctr<<endl;
return 0;
}