1

I'm working on my university assignment which requires me to program an eviction algorithm. I am new to programming and have only done Python before. Below is what I have done so far. The code compiles fine but I am not getting the output that I was expected to get.

getting user input and then calling the method for no eviction:

System.out.println();
        System.out.println("Cache content: ");
        print_array(org_cache, size);
        System.out.println("Request sequence: ");
        print_array(request, count);


        try {
            copy_array(org_cache, cache, size);
            System.out.println("no_evict");
            no_evict(cache, size, request, count);
        }
        catch (Exception e) {
            System.out.println("ERROR: no_evict");

the method:

    static void no_evict(int[] cache, int c_size, int[] request, int r_size) {
    int i = 0;
    boolean found = false;
    String result = "";
    String resultHit = "";
    String resultMiss = "";

    for(int x = 0; x < r_size; x++) { //for loop goes through every requests
        while(i < c_size || found == false) { //while loop brings a page through every cache value
        if(request[x] == cache[i]){
            found = true;
        } else {
            i += 1;
            }
        }
    if(found == true) {
        result += "h";
        resultHit += "h";
    } else {
        result += "m";
        resultMiss += "m";
        x += 1; //proceeds to next value in request sequence
        }
    }
    System.out.println(result);
    System.out.println(resultHit.length() + "h " + resultMiss.length() + "m");
}

it does not output the result string but instead outputs this:

Cache content:
20 30 10 5 40
Request sequence:
20 30 10
no_evict
MilkTable
  • 41
  • 5
  • 1
    Perhaps `autoFlush` was set false and you're missing a `System.out.flush();` at the end. Or maybe there is an infinite loop in your algorithm. You may benefit from running a debugger, or else adding even more `println` tracing. https://stackoverflow.com/questions/7166328/when-why-to-call-system-out-flush-in-java – Will Cain Feb 26 '20 at 20:45

3 Answers3

1

You will notice that your program doesn't actually exit. This is because it's stuck in an infinite loop, just as what would happen in Python.

If you run it in a debugger or hit Ctrl-\ in a console, you'll see that it's stuck in this loop:

    while(i < c_size || found == false) { //while loop brings a page through every cache value
    if(request[x] == cache[i]){
        found = true;
    } else {
        i += 1;
        }
    }

The condition is true as long as i < c_size because you used ||, logical "or". You probably intended to use &&, so that find = true will break the loop.

PS: The Java compiler doesn't care about indenting, but humans do. Please use your editor's indenting function to make the code easier to read.

that other guy
  • 101,688
  • 11
  • 135
  • 166
  • Yep, thank you very much! I expected the || to act as the and statement in Python and clearly I was wrong. – MilkTable Feb 26 '20 at 20:52
0

As @that other guy mentioned, you are using || instead of &&. But you don't need the second qualifier. Just use:

i = 0;
while(i < c_size ) {
   if(request[x] == cache[i]){
      found = true;
      break;
   } else {
      i += 1;
   }
}

or

   for ( int i=0; i < c_size; i++ ) {
      if ( request[x] < cache[i] ) {
         found = true;
         break;
      }
   }
FredK
  • 4,036
  • 1
  • 6
  • 11
0

There is another problem with your algorithm: you initialise i right at the beginning but never reset it to zero.

You probably want to move the int i = 0 statement between the for and the while statement.

Andreas Dolk
  • 108,221
  • 16
  • 168
  • 253