/* Warren Layton
 * 1956280
 * CEG4153
 * Assignment 4
 * Monday November 25, 2002
 */

#include <stdio.h>

int main(int argc, char* argv[]) {
	float slope, rough;
	
	/* Lookup Table */
	float lookup[4][5] = {
		6,10,15,20,20,
		6,6,10,15,15,
		3,6,6,10,15,
		3,3,6,6,10};
	int i1, j1, i2, j2; /* Indexes for the lookup table */
	float tmp,output;
	int k;
	
	do {
		printf("Enter the slope (-45 to 45 degrees): ");
		scanf("%f", &slope);
	} while (slope > 45 || slope < -45);

	if(slope <= -30) {
		j1 = j2 = 0;
	} else if (slope >= -30 && slope < -15) {
		j1 = 0; j2 = 1;
		tmp = -(slope + 15)/15;
		for(k = 0; k < 4; k++) {
			lookup[k][0] = (1-tmp)*lookup[k][0];
			lookup[k][1] = tmp*lookup[k][1];
		}
	} else if (slope >= -15 && slope < 0) {
		j1 = 1; j2 = 2;
		tmp = -slope/15;
		for(k = 0; k < 4; k++) {
			lookup[k][1] = (1-tmp)*lookup[k][1];
			lookup[k][2] = tmp*lookup[k][2];
		}
	} else if (slope >= 0 && slope < 15) {
		j1 = 2; j2 = 3;
		tmp = slope/15;
		for(k = 0; k < 4; k++) {
			lookup[k][2] = (1-tmp)*lookup[k][2];
			lookup[k][3] = tmp*lookup[k][3];
		}
	} else if (slope >= 15 && slope < 30) {
		j1 = 3; j2 = 4;
		tmp = (slope - 15)/15;
		for(k = 0; k < 4; k++) {
			lookup[k][3] = (1-tmp)*lookup[k][3];
			lookup[k][4] = tmp*lookup[k][4];
		}
	} else {
		j1 = 4; j2 = 4;
	}
		
	
	do {
		printf("Enter the roughness (0 to 100%): ");
		scanf("%f", &rough);
	} while (rough > 100 || rough < 0);

	if(rough <= 20) {
		i1 = i2 = 0;
	} else if (rough >= 20 && rough < 40) {
		i1 = 0; i2 = 1;
		tmp = (rough - 20)/20;
		for(k = 0; k < 5; k++) {
			lookup[0][k] = (1-tmp)*lookup[0][k];
			lookup[1][k] = tmp*lookup[1][k];
		}
	} else if (rough >= 40 && rough < 60) {
		i1 = 1; i2 = 2;
		tmp = (rough - 40)/20;
		for(k = 0; k < 5; k++) {
			lookup[1][k] = (1-tmp)*lookup[1][k];
			lookup[2][k] = tmp*lookup[2][k];
		}
	} else if(rough >= 60 && rough < 80) {
		i1 = 2; i2 = 3;
		tmp = (rough - 60)/20;
		for(k = 0; k < 5; k++) {
			lookup[2][k] = (1-tmp)*lookup[2][k];
			lookup[3][k] = tmp*lookup[3][k];
		}
	} else {
		i1 = i2 = 3;
	}

	if (i1 == i2) {
		if(j1 == j2) {
			output = lookup[i1][j1];
		}
		else {
			output = lookup[i1][j1] + lookup[i1][j2];
		}
	} else if (j1 == j2) {
		output = lookup[i1][j1] + lookup[i2][j1];
	} else {
		output = lookup[i1][j1] + lookup[i1][j2]
			+ lookup[i2][j1] + lookup[i2][j2];
	}
	printf("Output: %f\n\n", output);
	for(i1 = 0; i1 < 4; i1++) {
		for(i2 = 0; i2 < 5; i2++) {
			printf("%.2f ",lookup[i1][i2]);
		}
		printf("\n");
	}

}

