#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <iostream>
#include "time.h"
#include <fstream>
using namespace std;
int main()
{
srand ( time(NULL) ); //start the seed thing
int arrayz[10010]; //reference string
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int q = 0;
int r = 0;
int tablez[1][1]; //the page tables
//int tablez[2][2]; //the page tables
//int tablez[3][3]; //the page tables
//int tablez[4][4]; //the page tables
//int tablez[5][5]; //the page tables
//int tablez[6][6]; //the page tables
//int tablez[7][7]; //the page tables
//int tablez[8][8]; //the page tables
int pagefaults[8]; //the counters
int done[8];
int tableentry[8][8];
int tablecheck[8];
for (i = 0; i < 8; i++)
{
tablecheck[i] = 0;
}
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
tableentry[i][j] = 0;
}
}
for (i = 0; i < 9; i++) //make each table done
{
done[i] = 2;
}
for (i = 1; i < 9; i++) //populate the page fault tables with garbage
{
for (j = 0; j < (i+1); j++)
{
tablez[i][j] = 11;
}
}
for ( i = 10000; i < 10010; i++) //populate the refence string end with garbage
{
arrayz[i] = 11;
}
ofstream writer;
writer.open("output.txt");
for (i = 0; i < 10000; i++)
{
j = rand() % 10;
arrayz[i] = j;
writer << arrayz[i] << endl;
}
writer.close();
for (i = 0; i < 10000; i++) //for every number
{
for (j = 1; j < 9; j++) //go through every page table
{
if (done[j-1] == 0) //if the last table did not have any matches for the number,
//set the pagefault number for that table up one and then replace something
//with whatever caused the pagefault
{
pagefaults[j-1]++; //say this table pagefaulted and then
//insert optimal algorithm here// //replace one of the things
for (l = 0; l < j; l++) //for every entry in the table
{
for (m = i; m < 10000; m++) //check each table entry against each value that is coming
{
if (tablez[j-1][l] != arrayz[m])
{
tableentry[j-1][l]++; //how many spaces ahead in arrayz[] that this entry occurs
}
else
{
continue;
}
}
} //so by this point one of the table entries is the highest number
//backup
for (r = 0; r < j; r++)
{
tablecheck[r] = tableentry[j-1][r];
}
for (n = l; n > 0; n--) //so now we decide which one is biggest by putting it in tableentry[j-1][0]
{
if (tableentry[j-1][n] > tableentry[j-1][n-1])
{
tableentry[j-1][n-1] = tableentry[j-1][n];
}
}
//so now the one farthest out in arrayz[i] is
for (q = 0; q < l; q++)
{
if (tablecheck[q] == tableentry[j-1][0])
{
tablez[j-1][q] = arrayz[i]; //the thing is finally replaced
}
}
/////////////////////////////////
}
done[j-1] = 0;
for (k = 0; k < j; k++) //and go through every entry in it
{
if (tablez[j][k] == arrayz[i]) //if a match is found
{
done[j] += 2;
}
}
}
}
for (i = 0; i < 8; i++)
{
cout << "Page table " << i << " had " << pagefaults[j-1] << "pagefaults." << endl;
}
return 0;
}