Beispiel #1
0
void ia_test_all(void) {
	ia_create_test();
	ia_range_test();
	ia_of_string_test();
	ia_fn_test();
	ia_of_da_test();
	ia_contains_test();
	ia_fill_test();
	ia_fill_from_to_test();
	ia_index_test();
	ia_index_from_test();
	ia_index_fn_test();
	ia_last_index_test();
	ia_last_index_from_test();
	ia_last_index_fn_test();
	ia_sort_test();
	ia_sort_dec_test();
	ia_insert_test();
	ia_remove_test();
	ia_each_test();
	ia_each_state_test();
	ia_foldl_test();
	ia_foldr_test();
	ia_filter_test();
	// ia_filter_state_test();
	ia_choose_test();
	ia_exists_test();
	ia_forall_test();
	
#if 0
	Array a = ia_fn(20, ia_rnd, 5);
	ia_println(a);

	// get number of different numbers in array
	ia_sort(a);
	ia_println(a);
	int n = a_length(a);
	int k = (n > 0) ? 1 : 0;
	for (int i = 1; i < n; i++) {
		if (ia_get(a, i-1) != ia_get(a, i)) {
			k++;
		}
	}
	printiln(k);
	a_free(a);
#endif

#if 0
	// timing
	int n = 2000000000;
	Array a = ia_create(n, 0);
	time_function(   ia_fill_fn(a, fn_id)   );
	a_free(a);
	a = ia_create(n, 0);
	time_function(   ia_id(a)   );
	a_free(a);
#endif
	
}
int main(void) {
    // every_other_test();
    // return 0;

    Array temps = ia_of_string(
                      "7, 6, 5, 6, 6, 5, 6, 6, 6, 7, 7, 9, 10,"
                      "11, 11, 12, 13, 12, 10, 9, 9, 8, 7, 4");
    ia_println(temps);
    printiln(a_length(temps));
    printiln(ia_get(temps, 0));
    printiln(ia_get(temps, 23));

    printsln("contains_negative_test:");
    contains_negative_test();

    printsln("every_other_test:");
    every_other_test();

    a_free(temps);
    return 0;
}
/* iteratively check the properties of n. Returns 1 if perfect, the second number of the pair
 if amicable, -1 if prime, a number between 1 and 200 if it reaches a perfect instead of a prime,
 and a negative number less than -1 if it loops in a triplet, quadruplet, etc. */
int checkProperties(ia *array, int n)
{
    int current = ia_get(array, n);

    ia_store(array, n, -1);

    int sum = sumDivisors(n);

    if   (sum == 1) current = -1;  // prime
    elif (sum == n) current = 1;   // perfect
    else
    {
// Return an array that contains every other element of
// the provided array, i.e., the result array contains
// the elements at the even indices of the provided array.
Array every_other(Array array) {
    int n = a_length(array);
    int m = (n + 1) / 2;
    Array result = ia_create(m, 0);
    // Iterate over the even indices in the original order.
    // Copy each element at an even index to the result array.
    for (int i = 0; i < n; i += 2) {
        assert(i < n);
        assert(i >= 0 && (i % 2) == 0);
        // printiln(i);
        ia_set(result, i / 2, ia_get(array, i));
    }
    // return ia_create(0, 0);
    return result;
}
bool contains_negative(Array array) {
    // Iterate over the elements of the array (any order).
    // For each array element, check whether it is negative.
    // If so return true, otherwise continue
    // until all of the elements have been inspected.
    // If there is no negative value, answer false.
    int n = a_length(array);
    for (int i = 0; i < n; i++) {
        assert(i < n);
        assert(i >= 0);
        if (ia_get(array, i) < 0) {
            return true;
        }
    }
    return false;
}