Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.24/5 (5 votes)
See more:
import java.util.Random;
import java.util.Scanner;

public class Main {
	public static Color[][] memory;
	public static Color[] cache;

	public static void main(String[] args) {
		shmain();
	}

	public static void shmain() {

		int N, M, K;
		Scanner s = new Scanner(System.in);

		int AMAT = 0, hit_time = 0, miss_rate = 0, miss_penalty = 1, common_strikes = 0;

		do {
			System.out.println("Enter N (should be power of 2):");
			N = s.nextInt();
		} while (isPowerOfTwo(N));

		do {
			System.out.println("Enter M (should be power of 2):");
			M = s.nextInt();
		} while (isPowerOfTwo(M));

		memory = new Color[N][M];

		do {
			System.out.println("Enter K (should be power of 2 and less than  NxM / 4) :");
			K = s.nextInt();
		} while (isPowerOfTwo(K) || K > (N * M / 4));

		cache = new Color[K];
		Random rand = new Random();

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				memory[i][j] = new Color();
				memory[i][j].c = Math.abs(rand.nextInt() % 2);
				memory[i][j].m = Math.abs(rand.nextInt() % 2);
				memory[i][j].y = Math.abs(rand.nextInt() % 2);
				memory[i][j].k = Math.abs(rand.nextInt() % 2);
			}
		}

		for (int i = 0; i < K; i++) {
			cache[i] = new Color();
		}

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				for (int c = 0; c < K; c++) {
					common_strikes++;
					if (cache[c] == null) {
						miss_rate++;
					} else if (memory[i][j] == cache[c]) {
						hit_time++;
					} else {
						miss_rate++;
						changeCache(N, M, K, i, j);
					}
				}
			}
		}

		System.out
				.println("Hit times: " + hit_time + " " + " \nmiss times: " + miss_rate + " \nnall checks:" + common_strikes
						+ "\nhit rate: " +((double)hit_time/(double)common_strikes) + "\nmiss rate: " +((double)miss_rate/(double)common_strikes)
						);
	}

	public static void changeCache(int n, int m, int k, int mi, int mj) {

		if (m == k) {
			for (int i = 0; i < k; i++) {
				cache[i] = memory[mi][i];
			}
		} else if (m > k) {

			if (mj + k <= m) {
				int z = 0;
				for (int j = mj; j < mj + k; j++) {
					if (j <= k)
						cache[z] = memory[mi][j];
					z++;
				}
			}

			if (mj + k > m) {
				int z = 0;
				for (int i = mj; i < m; i++) {
					cache[z] = memory[mi][i];
					z++;
				}
				z = 0;
				
			}
		} else {

			Color[] mass_odnom = new Color[n * m];
			int ka = 0;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					mass_odnom[ka] = memory[i][j];
					k++;
				}
			}

			ka = 0;
			for (int i = mj * mi + mj; i < mj * mi + mj + k; i++) {
				cache[k] = mass_odnom[i];
				ka++;
			}

		}

	}

	public static boolean isPowerOfTwo(int x) {
		return !((x != 0) && ((x & (~x + 1)) == x));
	}
}


What I have tried:

Help me please, dear kind people
how to solve this code?

cache = new Color[K];
		Random rand = new Random();

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				memory[i][j] = new Color();
				memory[i][j].c = Math.abs(rand.nextInt() % 2);
				memory[i][j].m = Math.abs(rand.nextInt() % 2);
				memory[i][j].y = Math.abs(rand.nextInt() % 2);
				memory[i][j].k = Math.abs(rand.nextInt() % 2);
			}
		}

		for (int i = 0; i < K; i++) {
			cache[i] = new Color();
		}

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				for (int c = 0; c < K; c++) {
					common_strikes++;
					if (cache[c] == null) {
						miss_rate++;
					} else if (memory[i][j] == cache[c]) {
						hit_time++;
					} else {
						miss_rate++;
						changeCache(N, M, K, i, j);
					}
				}
			}
		}

		System.out
				.println("Hit times: " + hit_time + " " + " \nmiss times: " + miss_rate + " \nnall checks:" + common_strikes
						+ "\nhit rate: " +((double)hit_time/(double)common_strikes) + "\nmiss rate: " +((double)miss_rate/(double)common_strikes)
						);
	}
Posted
Updated 10-Oct-18 4:52am

1 solution

j2c[^] is able to convert java code to roughly equivalent C++ code. The converted code will have to be tested to ensure that it works as desired.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900