#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "test.h"

/*
 *  This lab contains a number of exercises relating to 
 *  memory management:  allocation, deallocation, and other
 *  topics.  The format is a little different from some of 
 *  the previous labs in this course.  Since C provides fewer
 *  diagnostic tools than more modern languages, there is no
 *  good way to automatically check your results.  Instead
 *  you should check your work against the provided solution
 *  file after finishing each section.
 */

/*
 *  PART ONE
 * 
 *  For this part you are asked to write some memory allocations
 *  and deallocations using malloc() and free().  Follow the 
 *  examples in writing your own lines to fulfill the requested tasks.  
 * 
 *  Make sure that your program compiles and runs cleanly after completing this part.
 *  Then check your work against the answer key when you are finished.
 */
void part_one() {
    printf("\n\nPART ONE\n");

    // First some variable declarations for use below.
    int *m, *n, *p, *q;
    float *x, *y, *z;
    char *s, *t;

    // Example:  allocate at x space for a single float
    // Then store 1.54 into it
    x = (float*)malloc(sizeof(float));
    *x = 1.54;
    
    // Example:  allocate at m space for a three int
    // Then store 4, 5, 6
    m = (int*)malloc(3*sizeof(int));
    m[0] = 4;
    m[1] = 5;
    m[2] = 6;

    // Example:  allocate at s space for a five character string
    // making sure to include enough space for the terminal null
    // Then use sprintf to put ABCDE into it
    s = (char*)malloc(6*sizeof(char));
    sprintf(s,"ABCDE");

    // Example:  make n an alias for the space allocated at m
    // Then print the three values using n
    n = m;
    printf("%d %d %d\n",n[0],n[1],n[2]);

    // Example:  free space allocated at x
    free(x);

    // Now it's your turn...
  
    // Exercise 1:  allocate at y space for an array of four float
    // Then store the square roots of the first four prime numbers
    // (you can use sqrt(2) to get the square root of 2, etc.)
    y = (float*)malloc(4*sizeof(float));
    y[0] = sqrt(2);
    y[1] = sqrt(3);
    y[2] = sqrt(5);
    y[3] = sqrt(7);
    
    // Exercise 2:  allocate at p space for a seven int
    // Then store the squares of 1 through 7 using a loop
    p = (int*)malloc(7*sizeof(int));
    for (int i = 0; i < 7; i++) {
        p[i] = (i+1)*(i+1);
    }

    // Exercse 3:  allocate at t space for an eight character string
    // making sure to include enough space for the terminal null
    // Then use sprintf to put y[0] into it, to 6 decimals
    t = (char*)malloc(9*sizeof(char));  
    sprintf(t,"%8.6f",y[0]);

    // Exercise 4:  Make z point to the third element of y
    // Then print out the last two elements of y, using z
    z = y+2;
    printf("%f %f\n",z[0],z[1]);

    // Exercise 5:  Allocate enough space at q to make a copy of m
    // Then copy the values from m to q
    q = (int*)malloc(3*sizeof(int));
    q[0] = m[0];
    q[1] = m[1];
    q[2] = m[2];

    // Some tests
    //
    // Output should be:
    // 4 5 6
    // 2.236068 2.645751
    // 1.414214 1.732051 2.236068 2.645751
    // 1 4 9 16 25 36 49
    // 4 5 6
    // 1.414214

    printf("%f %f %f %f\n",y[0],y[1],y[2],y[3]);
    printf("%d %d %d %d %d %d %d\n",p[0],p[1],p[2],p[3],p[4],p[5],p[6]);
    m[0] = m[1] = m[2] = -1;
    printf("%d %d %d\n",q[0],q[1],q[2]);
    printf("%s\n",t);

    // Exercise 6:  free all the memory allocated in this section
    free(y);
    free(m);
    free(p);
    free(q);
    free(s);
    free(t);
    // Note:  x is already freed, and z is an alias for y 
}

// forward declarations of some functions that will be needed below
int** allocate2d(int m, int n);
void free2d(int** p, int m);

/*
 *  PART TWO
 * 
 *  For this part you are asked to write some functions that will work
 *  with two-dimensional arrays -- that is, arrays with two indices.
 *  In practice these are implemented as an array of pointers, each 
 *  element of which points to an array of values.  Each array of values
 *  represents a row in the 2D grid, and the array of pointers represents
 *  all the rows stacked together.
 * 
 *  When you access the members of a 2D array, you will give two indices:
 *  one for the first array, and one for the second.
 * 
 *  Make sure that your program compiles and runs cleanly after completing this part.
 *  Then check your work against the answer key when you are finished.
 */
void part_two() {
    printf("\n\nPART TWO\n");

    // Example: allocate a 3x3 array
    int** magic = (int**)malloc(3*sizeof(int*));
    magic[0] = (int*)malloc(3*sizeof(int));
    magic[1] = (int*)malloc(3*sizeof(int));
    magic[2] = (int*)malloc(3*sizeof(int));

    // Example:  now store values that form a magic square
    //  8     1     6
    //  3     5     7
    //  4     9     2
    magic[0][0] = 8;
    magic[0][1] = 1;
    magic[0][2] = 6;
    magic[1][0] = 3;
    magic[1][1] = 5;
    magic[1][2] = 7;    
    magic[2][0] = 4;
    magic[2][1] = 9;
    magic[2][2] = 2;

    // Example: print some sums
    printf("First diagonal sum: %d\n",magic[0][0]+magic[1][1]+magic[2][2]);
    printf("Second diagonal sum: %d\n",magic[2][0]+magic[1][1]+magic[0][2]);

    // Example: free the 2D array
    free(magic[0]);
    free(magic[1]);
    free(magic[2]);
    free(magic);  // do this last

    // Exercises 7 & 8:  fill in the function definitions that follow
    // Then uncomment the section below, which will use your work
    int** t = allocate2d(2,3);
    t[0][0] = 11;
    t[0][1] = 12;
    t[0][2] = 13;
    t[1][0] = 21;
    t[1][1] = 22;
    t[1][2] = 23;
    printf("%d %d %d\n%d %d %d\n",t[0][0],t[0][1],t[0][2],t[1][0],t[1][1],t[1][2]);   
    free2d(t,2);
}

// Exercise 7:  write a function that takes two arguments m and n,
// and allocates a 2D array that has m rows of n elements each
int** allocate2d(int m, int n){
    int **result = (int**)malloc(m*sizeof(int*));
    for (int i = 0; i < m; i++) {
        result[i] = (int*)malloc(n*sizeof(int));
    }
    return result;
}

// Exercise 8:  write a function that takes two arguments m and n,
// plus a pointer to a 2D array, and frees all the memory
void free2d(int** p, int m) {
    for (int i = 0; i < m; i++) {
        free(p[i]);
    }
    free(p);
}

int main() {
    part_one();
    part_two();
}