// ************************************************************************ //
//  Jeff Balsley  11-15-2001                                                //
//  assignment #6 problem 2                                                 //
//                                                                          //
// ************************************************************************ //

#include<iostream>
#include<cmath>
using namespace std;
const int SIZE = 5;

// function declarations
void square_array(int arr[][2]);
void print_array(int arr[][2]);
void sort_array(int arr[][2]);

int main(void)
{
	int myArray[SIZE][2];
	int i;

	// request first collumn from the user

	for(i=0; i<SIZE; ++i){
		cout << "Enter number " << i+1 << " > ";
		cin >> myArray[i][0];
	}

	// square the given array
	square_array(myArray);

	// sort and print the array
	sort_array(myArray);
	print_array(myArray);

	return 0;
}

// ************************************************************************* //
//  This function will take a 2-D array as input and populate arr[x][1]      //
//  with pow(arr[x][0],2)                                                    //
// ************************************************************************* //
void square_array(int arr[][2])
{
	int i;

	for(i=0; i<SIZE; ++i){
		arr[i][1] = pow(arr[i][0],2);
	}
}

// ************************************************************************* //
//  This function will simply print the array                                //
// ************************************************************************* //
void print_array(int arr[][2])
{
	int i;
	cout << "\n";
	for(i=0; i<SIZE; ++i){
		cout << arr[i][0] << " ";
		cout << arr[i][1] << "\n";
	}
}

// ************************************************************************* //
//  This function will sort the array using selection sort                   //
// ************************************************************************* //
void sort_array(int arr[][2])
{
	cout << "Enter sort_array\n";
	int i,j;
	int largest;
	int temp[1][2];


	for(j=0; j<SIZE; ++j){
		// set the first element of the array as the initial largest
		largest = j;  

		// check the rest of the array to see which elements are larger
		for(i=j; i<SIZE; i++){
			if(arr[i][0] > arr[j][0]){
				largest = i;
			}
		}

		//  swap the array elements
		if (largest != j){
			cout << "Switching...\n";

			temp[0][0] = arr[largest][0];
			temp[0][1] = arr[largest][1];

			arr[largest][0] = arr[j][0];
			arr[largest][1] = arr[j][1];

			arr[j][0] = temp[0][0];
			arr[j][1] = temp[0][1];
		}
	}
	cout << "Exit sort_array\n";
}
