// Arup Guha
// 1/19/2022
// Code for COP 3502 Quiz #1 Version C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct block {
    int number;
    char letter;
} block;

int* makeElevatedArray(int array[], int n, int add);
char* allFirstLetters(char** words, int n);

int main(void) {

    // Question #1 Test
    int n;
    scanf("%d", &n);

    block** arr = calloc(n, sizeof(block*));

    for (int i=0; i<n; i++) {
        arr[i] = malloc(sizeof(block));
        arr[i]->number = rand()%10 +1;
        arr[i]->letter = (char)(rand()%26+'a');
    }
    for (int i=0; i<n; i++) printf("%c%d, ", arr[i]->letter, arr[i]->number);
    printf("\n");

    // Not part of answer.
    for (int i=0; i<n; i++) free(arr[i]);
    free(arr);

    // Question #2 Test
    int nums[10] = {3,1,7,6,5,4,9,2,10,8};
    int* next = makeElevatedArray(nums, 10, 7);
    for (int i=0; i<10; i++) printf("%d ", next[i]);
    printf("\n");

    // Not part of answer.
    free(next);

    // 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 = allFirstLetters(words, n);
    printf("%s\n", res);

    // Not part of answer.
    for (int i=0; i<n; i++)
        free(words[i]);
    free(words);
    free(res);

    // Create an array of size n storing even integers.
    int* array = malloc(sizeof(int)*n);
    for (int i=0; i<n; i++) array[i] = 2*i;

    // Do the work for the realloc.
    int* tmp = calloc(2*n, sizeof(int));// 1 pt
    for (int i=0; i<n; i++)				// 1 pt
        tmp[i] = array[i];				// 1 pt
    free(array);						// 1 pt
    array = tmp;						// 1 pt

    for (int i=0; i<2*n; i++) printf("%d ", array[i]);
    printf("\n");

    // Not part of answer.
    free(array);

    return 0;
}

// Question #2 Solution.
int* makeElevatedArray(int array[], int n, int add) {

    // 4 pts, can use calloc or malloc.
    int* res = calloc(n, sizeof(int));

    // 1 pt for loop, 2 pts for assignment
    for (int i=0; i<n; i++)
        res[i] = array[i] + add;

    // 2 pts for returning correct pointer.
    return res;
}

// Question #3 Solution
char* allFirstLetters(char** words, int n) {

    // 4 pts, can use calloc or malloc.
    char* res = malloc((n+1)*sizeof(char));

    // 2 pts for loop, 2 pts for assignment
    for (int i=0; i<n; i++)
        res[i] = words[i][0];

    // 1 pt for assigning this.
    res[n] = '\0';

    // 1 pt for returning correct pointer.
    return res;
}
