I have to devise a table look-up method that shortens the process of computing t
ID: 3680155 • Letter: I
Question
I have to devise a table look-up method that shortens the process of computing the function values of ackernn by storing intermediate results for ackermann and using these values when they are available. display the number of hits to the table lookup when does the table lookup method. this is what i have so far if anyone could help that would be great. Eack time the table starts it creats a new table btw it doesnt have to keep the old numbers stored. so when i look up 2,2 it starts a new one and when i run it again and do 3.2 it starts new and doesnt use the numbers stored for 2.2
package Ack;
import java.util.Scanner;
public class AckTable1
{
public static int[][] a = new int[4][10000];
public static int i;
public static int j;
String answer;
public void AckTableMethod1() throws InterruptedException
{
do
{
Scanner keyboard = new Scanner(System.in);
int m,n,result;
System.out.println("Enter m and n vaule that you want to lookup.");
m=keyboard.nextInt();
n=keyboard.nextInt();
result=ackermannfunction(m,n);
System.out.println("result = " + result);
entrytable(m,n,result);
System.out.println("Do you want to look up another number? yes or no");
answer = keyboard.next();
}while(answer.equalsIgnoreCase("yes"));
}
public static int ackermannfunction(int mvalue, int nvalue)
{
int value;
if(mvalue==0)
{
return nvalue++;
}
else if(mvalue>0&&nvalue==0)
{
value = lookup(mvalue,nvalue);
if(value==0)
{
return ackermannfunction(mvalue-1,1);
}
else
{
return value;
}
}
else if(mvalue>0&&nvalue>0)
{
value=lookup(mvalue,nvalue);
if(value==0)
{
return ackermannfunction(mvalue-1,ackermannfunction(mvalue,nvalue-1));
}
else
{
return value;
}
}
return nvalue;
}
public static int lookup(int mvalue,int nvalue)
{
int hit=0;
int miss=0;
for(i=0;i<mvalue;i++)
{
for(j=0;j<nvalue;j++)
{
if(a[i][j]==a[mvalue][nvalue])
{
return a[mvalue][nvalue];
}
}
}
return 0;
}
public static void entrytable(int m,int n,int result1)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==result1)
{
System.out.println("for"+m+ "and" +n+"value =" +a[i][j]);
}
else
{
a[m][n]=result1;
}
}
}
}
}
Explanation / Answer
The intention of what you want to do is not understandable from the problem description. Are you looking for a cache like machanism so that once computed values need not to be computed again by just storing the previosuly stored value in a table and retrieve it later when again encountered.