Beispiel #1
0
int main(int argc, char **argv)
{
    srand(time(NULL));
    pi = 4.0 * atan(1);
    min_x = 0.0;
    max_x = 3.0 * pi;
    if (argc > 1) {
        if (strcmp(argv[1], "-n") == 0) {
            no_random = 1;
        } else {
            print_usage(argv[0]);
            return 1;
        }
    }

    Table my_table = createMyTable();
    Table doubled_table = doubleTable(&my_table, min_x);
    Table left_table = createTable(min_x, max_x, 0);
    Table middle_table = createTable(min_x, max_x, numof_parts / 2);
    Table right_table = createTable(min_x, max_x, numof_parts - 1);

    printReport(&my_table, "Initial table");
    printReport(&doubled_table, "Doubled table");
    printReport(&left_table, "Left table");
    printReport(&middle_table, "Middle table");
    printReport(&right_table, "Right table");

    resetFunction(&right_table, my_g);
    resetFunction(&middle_table, my_g);
    resetFunction(&left_table, my_g);
    resetFunction(&doubled_table, my_g);
    resetFunction(&my_table, my_g);

    printReport(&my_table, "Initial table");
    printReport(&doubled_table, "Doubled table");
    printReport(&left_table, "Left table");
    printReport(&middle_table, "Middle table");
    printReport(&right_table, "Right table");

    disposeTable(&right_table);
    disposeTable(&middle_table);
    disposeTable(&left_table);
    disposeTable(&doubled_table);
    disposeTable(&my_table);

    reportMaxError();

    return 0;
}
int main(void)
{
	HashTable ht = newTable(SIZE);
	
	insertItem(ht, "Friend"); // insert items into ht
	insertItem(ht, "Yolo");
	insertItem(ht, "Lookin fors for the holidays"); 

	showHashTable(ht);
	removeItem(ht, "Friend"); // remove item in ht
	removeItem(ht, "asbdfb"); // remove item not in ht
	showHashTable(ht);

	insertItem(ht, "Matthew");
	insertItem(ht, "Matthew"); // collision
	showHashTable(ht);

	Item *it = searchItem(ht, "Matthew"); // search for item in ht
	assert(it != NULL);
	
	it = searchItem(ht, "friend"); // search for item not in ht
	assert(it == NULL);
	printf("it = %p\n", it);
		
	ht = hashTableExpand(ht, 30);
	showHashTable(ht);

	disposeTable(ht);
	return EXIT_SUCCESS;
}
Beispiel #3
0
long double calcMaxError(Function f, Polynomial *polynomial
        , size_t numof_xs, size_t numof_tests, long double min_x, long double max_x)
{
    long double const step = (max_x - min_x) / numof_xs;
    Table table;
    initTable(&table, numof_xs, f);
    long double curr_x = min_x;
    for (size_t i = 0; i < numof_xs; ++i) {
        tableAppend(&table, curr_x + frand() * step);
        curr_x += step;
    }
    makeLagrangePolynomial(polynomial, &table);
    disposeTable(&table);

    long double const test_step = (min_x - max_x) / numof_tests;
    long double max_error = 0.0;
    long double curr_test = min_x;
    for (size_t i = 0; i < numof_tests; ++i) {
        long double const x = curr_test + frand() * test_step;
        long double const y = calcValue(polynomial, x);
        long double const right_y = f(x);
        long double const error = y - right_y;
        max_error = fabsl(error) > fabsl(max_error) ? error : max_error;
        curr_test += test_step;
    }
    return max_error;
}
// this function doubles the number of slots in the hash table
HashTable hashTableExpand(HashTable ht, int n)
{
	int i = 0;
	HashTable newht = newTable(n); // create the new hash table
	assert(newht != NULL); 
	newht->nslots = n; // new ht now has 2x the num slots
	newht->nitems = 0; // nitems is 0

	// insert all the values into the new ht
	for (i = 0; i < ht->nslots; i++){
		if (ht->data[i] != NULL){ insertItem(newht, ht->data[i]); }
	}

	disposeTable(ht);
	return newht;
}