// Arup Guha
// 11/30/2011
// Written in COP 3223 class to illustrate an array of pointers to structs.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define SIZE 10

struct pt {
    int x;
    int y;
};

void swap(struct pt* array[], int i, int j);
void print(struct pt* array[], int length);
void fill_points(struct pt* array[], int length);

int main() {

    srand(time(0));

    // Declare the array.
    struct pt* my_points[SIZE];

    // Fill it.
    fill_points(my_points, SIZE);
    print(my_points, SIZE);

    // Swap the first two elements via pointers and print.
    swap(my_points, 0, 1);
    print(my_points, SIZE);

    // Free the memory.
    int i;
    for (i=0; i<SIZE; i++)
        free(my_points[i]);

    system("PAUSE");
    return 0;
}

// Regular swap.
void swap(struct pt* array[], int i, int j) {
    struct pt* temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}

// Prints out each point.
void print(struct pt* array[], int length) {
    int i;
    for (i=0; i<length; i++)
        printf("(%d, %d), ", array[i]->x, array[i]->y);
    printf("\n");
}

// Fills the points with random points with x,y coordinates
// in between 0 and 99, inclusive.
void fill_points(struct pt* array[], int length) {

    int i;
    for (i=0; i<length; i++) {

        array[i] = (struct pt*)(malloc(sizeof(struct pt)));
        array[i]->x = rand()%100;
        array[i]->y = rand()%100;
    }
}

