#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:  we allocate at x space for a single float
    // Then store 1.54 into it
    x = (float*)malloc(sizeof(float));
    *x = 1.54;
    
    // Example:  we 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:  we 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:  we 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:  we 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.)

    
    // Exercise 2:  allocate at p space for a seven int
    // Then store the squares of 1 through 7 using a loop


    // 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


    // Exercise 4:  Make z point to the third element of y
    // Then print out the last two elements of y, using z


    // Exercise 5:  Allocate enough space at q to make a copy of m
    // Then copy the values from m to q


    // 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
    // (as long as it has not been freed already)
}

// 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: we 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:  we 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: we 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: we 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){
    // FILL IN HERE
}

// Exercise 8:  write a function that takes a pointer to a 2D array,
// plus the number of rows (subarrays) m, and frees all the memory
void free2d(int** p, int m) {
    // FILL IN HERE
}

int main() {
    part_one();
    part_two();
}
