0

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.

2
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by dyingatmidnight
0

2-D array constructor...

table = new int*[size];
for(int i = 0; i < size; i++)
{
    table[i] = new int[size];
}

2-D destructor...

for(int i = 0; i < size; i++)
{
    delete table[i];
}

delete table;

Keep in mind that you CANNOT assume that all size * size elements will be contiguous! If you need them to be contiguous, you'll need a different solution.

0

That does seem to have gotten rid of my errors, thanks again. Only my output isn't as it should be. After adding in your solution is compiles and runs however - and I believe it has something to do with pointers only my grasp on them isn't quite firm yet - the output is a little garbled. I think I know what's happening but I'm afraid I'm lacking knowledge in how to fix it.

Here's what it should be:

[IMG]http://i55.tinypic.com/f9icfd.png[/IMG]

And here's what I get with the above code in C++

[IMG]http://i56.tinypic.com/in8pp2.png[/IMG]

0

I think I screwed up on the destructor...

for(int i = 0; i < size; i++)
{
    delete [](table[i]);
}
delete []table;

That looks a little better, but it's been a while, so I'm not guaranteeing.


Irrelevant to your problem, but worth pointing out.

As to your problem, C++ does not initialize variables. It looks like table[][] is not initialized. You should initialize everything to 0 in your constructor. That may or may not be your problem, but it sure looks you have uninitialized variables.

[EDIT]
You have thirty minutes to edit a post. After that, the edit option goes away.
[/EDIT]

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.