#include<iostream.h> #include<graphics.h> #include<conio.h> class fifo_alg { char ref[100]; int frame,fault,front,rear,x,y; char *cir_que; public: fifo_alg() { front=rear=-1; fault=0; x=5,y=50; } void getdata(); void insert(char ch); void chart(); void page_fault(); }; void fifo_alg::getdata() { cout<<"Enter Page reference string : "; cin.getline(ref,50); cout<<"Enter no. of frames : "; cin>>frame; } void fifo_alg::chart() { char d[2]; int f=front,r=rear,flag=1; for(int i=0;i<frame;i++) { if(f==r&&flag==0) { d[0]=0; if(f==frame) f=0; } else { if(f==r) flag=0; d[0]=cir_que[f]; f=(f+1)%frame; d[1]=0; } rectangle(x,y+i*20,x+20,y+(i+1)*20); outtextxy(x+10,y+i*20+10,d); } x+=20; } void fifo_alg::insert(char ch) { if(front==-1) { cir_que[0]=ch; front=0,rear=0; } else { rear=(rear+1)%frame; cir_que[rear]=ch; } fault++; } void fifo_alg::page_fault() { cir_que=new char[frame]; int flag=0; for(int i=0;ref[i]!=0;i++) { if(ref[i]>57||ref[i]<48) continue; if(front==-1) insert(ref[i]); else { if(cir_que[rear]==ref[i]) flag=1; else { for(int y=front;y!=rear;y=(y+1)%frame) if(cir_que[y]==ref[i]) { flag=1; break; } } if(flag==0) { if((rear+1)%frame==front) front=(front+1)%frame; insert(ref[i]); } } if(flag==0) chart(); flag=0; } cout<<"Number of page faults : "<<fault; } int main() { fifo_alg page; page.getdata(); int gdriver = DETECT,gmode; initgraph(&gdriver, &gmode, ""); page.page_fault(); getch(); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}