//CMSC 15200
//Homework #2

//Problem 1

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

void sort_selection(double[], int);
int find_largest(double[], int);
void swap(double&, double&);
void print_array(double[], int);

int main()
{
	int n;
	
	cout << "How many elements would you like in your array? " << endl;
	cin >> n;
	
	double a[n];
	int count = 1;
	double elt;
	while(count <= n){
		cout << "Element number " << count << " (of " << n << ") : " << endl;
		cin >> elt;
		a[(count-1)] = elt;
		count++;
	}
	
	cout << "Here is the given array: "<< endl;
	print_array(a, n );
	
	sort_selection(a, n);
	
	cout << "Here is the sorted array: "<< endl;
	
	print_array(a, n);
	
	return 0;
	
}

void sort_selection(double a[], int n)
{	
	if (n==1) {
		return;
	} else {
		swap( a[0], a[(find_largest(a, n))]);
		sort_selection(++a, --n);
	}
}

int find_largest(double a[], int n) {
	
	double largest = 0; //Note: this will only work 
			    //for positive numbers.
	int largest_ind = 0;
	
	for(int i=0; i<n; i++) {
		if (a[i] > largest) {
			largest = a[i];
			largest_ind = i;
		}
	}
	
	return largest_ind;
}

void swap(double &x, double &y)
{
	double temp = x; 
	x = y; 
	y = temp;
}

void print_array(double a[], int n)
{
	for(int i=0; i<n; i++){
		cout << a[i] << endl;
	}

	cout << endl;
}
