// Arup Guha
// 1/19/2022
// Code for COP 3502 Quiz #1 Version B

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

typedef struct reindeer {
    int antlers;
    int legs;
} reindeer;

char* concatAll(char** words, int n);
int* makeReverseArray(int array[], int n);

int main(void) {

    // Question #1
    int n;
    scanf("%d", &n);
    reindeer* list = calloc(n, sizeof(reindeer));
    for (int i=0; i<n; i++)
        list[i].antlers = 2;

    // Not part of answer.
    for (int i=0; i<n; i++) printf("%d ", list[i].antlers);
    printf("\n");
    free(list);

    // Question #2 test case
    int test[10] = {2,7,6,1,9,10,3,5,8,4};
    int* rest = makeReverseArray(test, 10);
    for (int i=0; i<10; i++) printf("%d ", rest[i]);
    printf("\n");

    // Not part of answer.
    free(rest);

    // Question #3 Test
    printf("How many words?\n");
    scanf("%d", &n);
    char** words = malloc(sizeof(char*)*n);
    for (int i=0; i<n; i++) {
        char tmp[1000];
        printf("enter word %d.\n", i+1);
        scanf("%s", tmp);
        words[i] = malloc(sizeof(char)*(strlen(tmp)+1));
        strcpy(words[i], tmp);
    }
    char* res = concatAll(words, n);
    printf("%s\n", res);

    // Not part of answer.
    free(res);

    // Question #4
    for (int i=0; i<n; i++)
        free(words[i]);
    free(words);

    return 0;
}

// Question #2 Solution
int* makeReverseArray(int array[], int n) {

    // 4 pts, can use calloc or malloc.
    int* res = calloc(n, sizeof(int));

    // 2 pts for loop, 2 pts for assignment
    for (int i=0; i<n; i++)
        res[i] = array[n-1-i];

    // 2 pts for returning correct pointer.
    return res;
}

// Question #3 Solution
char* concatAll(char** words, int n) {

    // Add up all lengths. (4 pts)
    int len = 0;
    for (int i=0; i<n; i++)
        len += strlen(words[i]);

    // Nulls out all locations so we can strcat into it. (4 pts)
    char* res = calloc(len+1, sizeof(char));

    // Concat everything in - 4 pts.
    for (int i=0; i<n; i++)
        res = strcat(res, words[i]);
    return res;
}



