Hello all, I am currently in the process of learning C++ for a work term position I am on. After hitting a wall trying to teach myself from books I decided it might be easier to translate some old Java programs from school into C++ to get a better understanding of the differences in syntax. Most notably with the creation of a two dimensional array. The Java program is a lab from school, we had to write an LRU - Least Recently Used - program.
Here's the Java program:
class LRU{
private int size;
private int[][] table;
private int[] priority;
public LRU(int size){
this.size = size;
table = new int[size][size];
priority = new int[size];
}
public void reference(int x){
System.out.println("Page Referenced: " + x);
for(int j = 0; j < size; j++){
table[x][j] = 1;
}
for(int i = 0; i < size; i++){
table[i][x] = 0;
}
int Dec = 0;
int Sum =0;
int i = 0;
int y = size -1;
while(i < size){
for(int j = 0; j < size; j++){
if(table[i][j] == 1){
Sum = (int) Math.pow(2, y);
Dec = Dec + Sum;
}
priority[i] = Dec;
y--;
}
Dec = 0;
Sum = 0;
y = size -1;
i++;
}
}
public int findPage(){
int leastUsed = priority[0];
for(int i = 0; i < size; i++){
if(priority[i] < leastUsed)
leastUsed = i; //assumes pages are numbered starting at zero
}
return leastUsed;
}
public void print(){
System.out.println("Table:");
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
System.out.print(table[i][j]);
}
System.out.println();
}
System.out.println("Priority:");
for(int i = 0; i < size; i++){
System.out.print(" " + priority[i]);
}
System.out.println();
System.out.println("Index:");
System.out.println(findPage());
System.out.println();
}
}
And here's the C++ program with the slight changes I've made in trying to convert it.
#include <iostream>
#include <math.h>
using namespace std;
class LRU{
public:
LRU(int size){
this->size = size;
table = new int[size][size];
priority = new int[size];
}
~LRU(){
delete[] priority;
delete[] table;
}
void reference(int x){
cout << "Page Referenced: " << x;
for(int j = 0; j < size; j++){
table[x][j] = 1;
}
for(int i = 0; i < size; i++){
table[i][x] = 0;
}
int Dec = 0;
int i = 0;
int Sum =0;
int y = size -1;
while(i < size){
for(int j = 0; j < size; j++){
if(table[i][j] == 1){
Sum = (int) pow(2, y);
Dec = Dec + Sum;
}
priority[i] = Dec;
y--;
}
Dec = 0;
Sum = 0;
y = size -1;
i++;
}
}
int findPage(){
int leastUsed = priority[0];
for(int i = 0; i < size; i++){
if(priority[i] < leastUsed)
leastUsed = i; //assumes pages are numbered starting at zero
}
return leastUsed;
}
void print(){
cout << "Table:";
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
cout << table[i][j];
}
cout << endl;
}
cout << "Priority:";
for(int i = 0; i < size; i++){
cout << " " << priority[i];
}
cout << endl;
cout << "Index:";
cout << findPage();
cout << endl;
}
private:
int size;
int **table;
int *priority;
};
And finally these are the errors I'm getting when I try to compile:
------ Build started: Project: LRU, Configuration: Debug Win32 ------
LRU.cpp
c:\documents and settings\dee\my documents\visual studio 2010\projects\lru\lru\lru.cpp(10): error C2540: non-constant expression as array bound
c:\documents and settings\dee\my documents\visual studio 2010\projects\lru\lru\lru.cpp(10): error C2440: '=' : cannot convert from 'int (*)[1]' to 'int **'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\dee\my documents\visual studio 2010\projects\lru\lru\lru.cpp(29): error C2668: 'pow' : ambiguous call to overloaded function
c:\program files\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)'
c:\program files\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)'
c:\program files\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)'
while trying to match the argument list '(int, int)'
c:\documents and settings\dee\my documents\visual studio 2010\projects\lru\lru\lru.cpp(38): warning C4258: 'i' : definition from the for loop is ignored; the definition from the enclosing scope is used
c:\documents and settings\dee\my documents\visual studio 2010\projects\lru\lru\lru.cpp(19) : definition of 'i' ignored
c:\documents and settings\dee\my documents\visual studio 2010\projects\lru\lru\lru.cpp(23) : definition of 'i' used
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Thanks in advance for any light you can shed on this.