#include <stdio.h>
#include <stdlib.h>
#include "test.h"

/*
 *  This lab consists of lots of practice writing loops.
 *  The first section asks you to compute a single value to return.
 * 
 *  In the second section you will modify an array in place,
 *  without creating new storage.
 * 
 *  In the third section, you will return new arrays created
 *  using malloc.  These should be freed before the program exits.
 */ 


/*****************************************************
 *  The exercises below all compute a single value
 *****************************************************/

/*
 *  Example A:
 *  Given an integer array and length, find the product of all the elements.
 *  Use a for loop.
 */
int product_for(int* array, int len) {
    int product = 1;
    for (int i = 0; i < len; i++) {
        product *= array[i];
    }
    return product;
}

/*
 *  Problem 1:
 *  Given an integer array and length, find the product of all the elements.
 *  Use a while loop.
 * 
 *  Add some tests in main.
 */
int product_while(int* array, int len) {
    return 0;
}

/*
 *  Example B:
 *  Given an integer array, its length, and an integer x,
 *  count the number of elements greater than or equal to x.
 *  Use a for loop.
 * 
 *  Add some tests in main.
 */
int count_greater(int* array, int len, int x) {
    int count = 0;
    for (int i = 0; i < len; i++) {
        if (array[i] >= x) {
            count++;
        }
    }
    return count;
}

/*
 *  Problem 2:
 *  Given a string, count the number of characters up to (but not including)
 *  the first null (0) character.  Use a for loop.    
 *  
 *  Add some tests in main.
 */
int string_len(char* s) {
    return 0;
}
	
/*
 *  Problem 3:
 *  Given an intger, return 1 if the input is prime, 0 if otherwise.
 *  Hint: A prime is a natural number greater than 1 
 *    that is only divisible by 1 and itself.
 *    One, zero and negative integers are not prime!     
 *   
 *  Add some tests in main.
 */
int isPrime(int input) {
    return 0;
}

/*****************************************************
 *  The exercises below all modify an array in place
 *****************************************************/

/*
 *  Example C:
 *  Given an integer array and its length, square each element    
 */
void squareElements(int* array, int len) {
    for (int i = 0; i < len; i++) {
        array[i] *= array[i];
    }
}

/*
 *  Problem 4:
 *  Given an integer array and its length, increase the value of each element by its index.    
 *   
 *  Add some tests in main.
 */
void incrementByIndex(int* array, int len) {
}

/*
 *  Problem 5:
 *  Given an integer array and its length, change the sign of all negative elements    
 *  
 *  Add some tests in main.
 */
void makePositive(int* array, int len) {
}

/*****************************************************
 *  The exercises below create a new array to return
 *****************************************************/

/*
 *  Example D:
 *  Given an integer array, its length, and an integer x,
 *  return a new array containing all elements greater than or equal to x
 */
int* filter_small(int* array, int len, int x) {
    int newLen = count_greater(array,len,x);
    int* newArray = (int*)malloc(newLen*sizeof(int));
    int count = 0;
    for (int i = 0; i < len; i++) {
        if (array[i] >= x) {
            newArray[count] = array[i];
            count++;
        }
    }
    return newArray;
}

/*
 *  Problem 6:
 *  Given a string, return a new string with every other index character of the original string.
 *  For example:
 *  "HelloWorld!" will become "Hlool!"    
 *  
 *  Add some tests in main.
 */
char* everyOther(char* inputString) {
    return "";
}


int main() {
    // some arrays to test with
    int array1[1] = {0};
    int array2[5] = {-3,7,9,-6,-5};
    int array3[3] = {3,3,3};
    int array4[6] = {-1,1,5,1,-3,1};

    // sum_for tests
    run_test("array1 product_for",
        product_for(array1,1) == 0
    );
    run_test("array2 product_for",
        product_for(array2,5) == -5670
    );
    run_test("array3 product_for",
        product_for(array3,3) == 27
    );
    run_test("array4 product_for partial",
        product_for(array4,4) == -5
    );

    // count_greater tests
    run_test("array1 count_greater",
        count_greater(array1,1,1) == 0
    );
    run_test("array2 count_greater",
        count_greater(array2,5,0) == 2
    );
    run_test("array3 count_greater",
        count_greater(array3,3,3) == 3
    );
    run_test("array4 count_greater none",
        count_greater(array4,0,-2) == 0
    );

    // squareElements tests
    squareElements(array3,3);
    run_test("array3 squareElements",
        (array3[0] == 9) && (array3[1] == 9) && (array3[2] == 9)
    );
    squareElements(array3,3);
    run_test("array3 squareElements again",
        (array3[0] == 81) && (array3[1] == 81) && (array3[2] == 81)
    );

    // filter_small tests
    int *fs1 = filter_small(array1,1,0);
    run_test("array1 filter_small",
        fs1[0] == 0
    );
    free(fs1);
    int *fs2 = filter_small(array2,5,0);
    run_test("array2 filter_small",
        (fs2[0] == 7) && (fs2[1] == 9)

    );
    free(fs2);
}
