/*
 * return the smallest of the elements in array x[]
 * there are n elements in x[] (x[0].. x[n-1])
 * n may be either odd or even
 * solve the problem recursively and 
 * use an "n / 2" type of decomposition
 */
int minRec2(int x[], int n) {
	if (n == 1) {
		return x[n - 1];
	}
	else {
		int compare1 = minRec2(x, n / 2);
		int compare2 = minRec2(x + n / 2, n - (n / 2));
		if (compare1 < compare2) {
			return compare1;
		}
		else {
			return compare2;
		}
	}
}
Пример #2
0
/*
 * return the smallest of the elements in array x[]
 * there are n elements in x[] (x[0].. x[n-1])
 * n may be either odd or even
 * solve the problem recursively and 
 * use an "n / 2" type of decomposition
 */
int minRec2(int x[], int n) {

	//Break to recursion, smallest is clearly the only element 
	if (n < 2) {
		return x[0];
	}

	//Some variables to represent the sizes of new recursions 
	int sizeA = n / 2;
	int sizeB = n - sizeA;

	//We do recursion by dividing our array in two 
	int smallA = minRec2(x, sizeA);
	int smallB = minRec2(x+sizeA, sizeB);

	//We return the smallest of two halves of an array 
	if (smallA < smallB) { return smallA; }
	else { return smallB; }
}
/*********************************************************************************************************************
                                                minRec2
This function returns the smallest of the elements in an array x[] of n elements (x[0].. x[n-1]) recursively using an
"n / 2" type of decomposition.
*********************************************************************************************************************/
int minRec2(int x[], int n) 
{
	// Base Case
	if (n <= 1)
	{
		return x[0];
	}

	// Inductive Step
	int leftMin = minRec2(x, n / 2);
	int rightMin = minRec2(x + (n / 2), n - (n / 2));
	if (leftMin < rightMin)
	{
		return leftMin;
	}
	else
	{
		return rightMin;
	}
}
int main(void) {
	const int magic_number = 13017;

	/* test min */
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	printf("the smallest of the first 10 natural numbers is: %d\n", minIt(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec1(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec2(a, 10));
	a[3] = -1;
	printf("now the smallest is %d\n", minIt(a, 10));
	printf("now the smallest is %d\n", minRec1(a, 10));
	printf("now the smallest is %d\n", minRec2(a, 10));
	
	/* test sqrt */
	printf("the sqrt of 25 is %g\n", sqrtIt(25.0, 0, 25.0));
	printf("the sqrt of 26 is %g\n", sqrtIt(26.0, 0, 26.0));
	printf("the sqrt of 2 is %g\n", sqrtIt(2.0, 0, 2.0));
	printf("the sqrt of 25 is %g\n", sqrtRec(25.0, 0, 25.0));
	printf("the sqrt of 26 is %g\n", sqrtRec(26.0, 0, 26.0));
	printf("the sqrt of 2 is %g\n", sqrtRec(2.0, 0, 2.0));

	/* test strCompare */
	char* s1; char* s2;
	s1 = "apple"; s2 = "apricot";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}
			
	s1 = "Apple"; s2 = "apple";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "baby boy"; s2 = "banana";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "a rather silly string"; s2 = "a rather silly string";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "12345"; s2 = "12345";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "Numbers: 12345"; s2 = "12345";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	s1 = "Texas"; s2 = "California";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	/* test strCompare2 */
	s1 = "apple"; s2 = "Apricot";
	if (strCompare2(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "Batman!"; s2 = "bat man";
	if (strCompare2(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "OMG, WTF?"; s2 = "oh my goodness, what the heck?";
	if (strCompare2(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	/* test maze */		
	srand(magic_number);
	makeMaze();
	recodeMaze();
	printf("recursive solution to the maze\n");
	printMaze();
	solveMazeRec(0, start_col);	
	printMaze();
	printf("\n\n\n");
	
	printf("iterative solution to the maze\n");
	srand(magic_number);
	makeMaze();
	recodeMaze();
	printMaze();
	solveMazeIt(0, start_col);
	printMaze();


	/* test Martian */
	Martian change1 = changeIt(15);
	printf("change 1 should be 0d, 3n, 0p and is: %dd %dn %dp\n", change1.dodeks, change1.nicks, change1.pennies);

	Martian change2 = changeIt(0);
	printf("change 2 should be 0d, 0n, 0p and is: %dd %dn %dp\n", change2.dodeks, change2.nicks, change2.pennies);

	Martian change3 = changeIt(17);
	printf("change 3 should be 1d, 1n, 0p and is: %dd %dn %dp\n", change3.dodeks, change3.nicks, change3.pennies);

	Martian change4 = changeIt(25);
	printf("change 4 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change4.dodeks, change4.nicks, change4.pennies);

	/* A very simple and obvious test of the general form of Martian
	 * be sure and test your solution more thoroughly!!!! */
	change4 = changeIt(25, 5, 12);
	printf("change 4 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change4.dodeks, change4.nicks, change4.pennies);
}
Пример #5
0
int main(void) {
	const int magic_number = time(NULL);

	/* test min */
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//	printf("the smallest of the first 10 natural numbers is: %d\n", minIt(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec1(a, 10));
	printf("the smallest of the first 10 natural numbers is: %d\n", minRec2(a, 10));
	a[3] = -1;
//	printf("now the smallest is %d\n", minIt(a, 10));
	printf("now the smallest is %d\n", minRec1(a, 10));
	printf("now the smallest is %d\n", minRec2(a, 10));
	
	/* test sqrt */
//	printf("the sqrt of 25 is %g\n", sqrtIt(25.0, 0, 25.0));
//	printf("the sqrt of 26 is %g\n", sqrtIt(26.0, 0, 26.0));
//	printf("the sqrt of 2 is %g\n", sqrtIt(2.0, 0, 2.0));
	// printf("the sqrt of 25 is %g\n", sqrtRec(25.0, 0, 25.0));
	// printf("the sqrt of 26 is %g\n", sqrtRec(26.0, 0, 26.0));
	// printf("the sqrt of 2 is %g\n", sqrtRec(2.0, 0, 2.0));
	//printf("sqrt of 2e60 is %.17g\n", sqrtRec(2.0 * pow10(60), 1.4142135623731, 1.5 * pow10(30)));

	/* test strCompare */
	char* s1; char* s2;
	s1 = "apple"; s2 = "apricot";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}
			
	s1 = "Apple"; s2 = "apple";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "baby boy"; s2 = "banana";
	if (strCompare(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "a rather silly string"; s2 = "a rather silly string";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "12345"; s2 = "12345";
	if (strCompare(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "Numbers: 12345"; s2 = "12345";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	s1 = "Texas"; s2 = "California";
	if (strCompare(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	/* test strCompare2 */
	s1 = "apple"; s2 = "Apricot";
	if (strCompare2(s1, s2) < 0) { 
		printf("\"%s\" is less than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be less than \"%s\"\n", s1, s2);
	}

	s1 = "Batman!"; s2 = "bat man";
	if (strCompare2(s1, s2) == 0) { 
		printf("\"%s\" is equal to \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be equal to \"%s\"\n", s1, s2);
	}

	s1 = "OMG, WTF?"; s2 = "oh my goodness, what the heck?";
	if (strCompare2(s1, s2) > 0) { 
		printf("\"%s\" is greater than \"%s\", very good\n", s1, s2);
	} else {
		printf("oops, \"%s\" should be greater than \"%s\"\n", s1, s2);
	}

	printf("********************************************\n");


	s1 = "!79857#*#*b*&atm#@@an 989^";
	s2 = "     b   a   t   m   a   ! n";

	if (strCompare2(s1, s2) == 0) {
		printf("Good\n");
	}
	else {
		printf("Bad\n");
	}

	s1 = "CAPITAL LETTERS DON'T MATTER FOR STRCOMPARE2";
	s2 = "capital letters don't matter for strcompare2";

	if (strCompare2(s1, s2) == 0) {
		printf("Good\n");
	}
	else {
		printf("Bad\n");
	}

	s1 = "asdf ASDF jkl JKL a   *&@#^&^#";
	s2 = "asdf ASDF jkl JKL b   *&@#^&^#";
	if (strCompare2(s1, s2) == -1) {
		printf("Good\n");
	}
	else {
		printf("Bad\n");
	}

	printf("Strong test case:\n");
	s1 = "strongg";
	s2 = "strong";
	printf("%d\n", strCompare(s1, s2));
	printf("%d\n", strCompare2(s1, s2));

	/* test maze */		
	srand(magic_number);
	makeMaze();
	recodeMaze();
	printf("recursive solution to the maze\n");
	fflush(stdout);
	solveMazeRec(0, start_col);
	printMaze();
	printf("\n\n\n");
	
//	printf("iterative solution to the maze\n");
//	srand(magic_number);
//	makeMaze();
//	recodeMaze();
//	solveMazeIt(0, start_col);
//	printMaze();
/*
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");

	// for (double j = 3; j < 2e3; j+= 2) {
	// 	printf("the sqrt of %g is %g expected: %g\n", j, sqrtRec(j, 0, 9000), sqrt(j));
	// 	double_equals(sqrtRec(j, 0, 9000), sqrt(j));
	// }


	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");

	//printf("%.17g\n", sqrtRec(27.0, 0.0, 50.0));
*/
	/* test Martian */
/*	Martian change1 = change(15);
	printf("change 1 should be 0d, 3n, 0p and is: %dd %dn %dp\n", change1.dodeks, change1.nicks, change1.pennies);

	int c = 0;
int d = 0;
int n = 0;
int p = 0;
int i = 0;
int iterate = 20;
FILE *fp;
fp = fopen("output.txt", "r");

while (!feof(fp) && i < iterate) {
  change1 = change(i);
  fscanf(fp, "%d", &c);
  fscanf(fp, "%d", &d);
  fscanf(fp, "%d", &n);
  fscanf(fp, "%d", &p);
  printf("change: %d %d %d %d\n", i ,change1.dodeks, change1.nicks, change1.pennies);
  printf("python: %d %d %d %d\n", c, d, n, p);
  assert(change1.pennies == p);
  assert(change1.nicks == n);
  assert(change1.dodeks == d);
  i++;
}
fclose(fp);

	Martian change2 = change(0);
	printf("change 2 should be 0d, 0n, 0p and is: %dd %dn %dp\n", change2.dodeks, change2.nicks, change2.pennies);

	Martian change3 = change(17);
	printf("change 3 should be 1d, 1n, 0p and is: %dd %dn %dp\n", change3.dodeks, change3.nicks, change3.pennies);

	Martian change4 = change(25);
	printf("change 4 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change4.dodeks, change4.nicks, change4.pennies);
*/
    //************************************************* PERSONAL TESTS *****************************************************//
    Martian change5 = change(16);
    printf("change 5 should be 0d, 3n, 1p and is: %dd %dn %dp\n", change5.dodeks, change5.nicks, change5.pennies);

	/* A very simple and obvious test of the general form of Martian
	 * be sure and test your solution more thoroughly!!!! */
	Martian change6 = change(25, 5, 12);
	printf("change 6 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change6.dodeks, change6.nicks, change6.pennies);

    Martian change7 = change(16, 5, 12);
    printf("change 7 should be 0d, 3n, 1p and is: %dd %dn %dp\n", change7.dodeks, change7.nicks, change7.pennies);

    Martian change8 = change(20, 5, 10);
    printf("change 8 should be 2d, 0n, 0p and is: %dd %dn %dp\n", change8.dodeks, change8.nicks, change8.pennies);

    Martian change9 = change(21, 5, 10);
    printf("change 9 should be 2d, 0n, 1p and is: %dd %dn %dp\n", change9.dodeks, change9.nicks, change9.pennies);

/*
    printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	n = 5;
	int z[] = {0, 0, 0, 0, 0};
	printf("Min of z: %d\n", minRec1(z, n));
	printf("Min of z: %d\n", minRec2(z, n));
	int oi[] = {-5, 0, 0, 0, -4};
	printf("Min of oi: %d\n", minRec1(oi, n));
	printf("Min of oi: %d\n", minRec2(oi, n));
	int koi[] = {-5, -234, -2525, 2, 4};
	printf("Min of koi: %d\n", minRec1(koi, n));
	printf("Min of koi: %d\n", minRec2(koi, n));
	char* str1 = "Genji";
	char* str2 = "GENJI";
	printf("%d\n", strCompare(str1, str2));
	printf("%d\n", strCompare2(str1, str2));
	str1 = " hehe xD";
	str2 = "hehe xD";
	printf("%d\n", strCompare(str1, str2));
	printf("%d\n", strCompare2(str1, str2));
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");

	Martian change45 = change(28);
    printf("%dd %dn %dp\n", change45.dodeks, change45.nicks, change45.pennies);

    */
}