#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

double rand_float(double a, double b);

int main(void)
{
	//Initialize an array called 'x' - a double that has 20 elements
	double x[20];

	//Set the values of 'x' using rand_float -- with all values between 0.0 & 20.0
	int i;
	for(i=0;i<20;i++)
		x[i]=rand_float(0,20);
		
	for(i=0; i<20;i++)
	{
		printf("x[%i] = %f\n", i, x[i]);
	}



	//Write a loop to find the index of the smallest value in the array 'x' 
	int index=0;
	double low=x[0];
	for(i=1;i<20;i++)
	{
		if(x[i]<low)
		{
			low=x[i];
			index=i;
		}
	}


	//Set the first value of the array (i.e. x[0] = to the smallest value -- make sure and to not completely throw away the value 
	//in x[0] (put it in some special place (e.g. a double called temp)
	double temp = x[0];	
	x[index] = temp;
	x[0] = low;
	
	//Write a loop to go through the array comparing adjacent elements switching elements if the one with a smaller index is larger than
	//the adjacent element
	//Note the comparison could be if( x[i] >= x[i+1]) -> move x[i] to x[i+1] and move x[i+1] to x[i]
	for(i=1;i<20;i++)
	{
		if(x[i] < x[i-1])
		{
			temp = x[i-1];
			x[i-1] = x[i];
			x[i] = temp;
		}
	}

	

	//Take the loop written previously and make it repeat until no switches are made (meaning every value at x[i] is smaller than every
	//value at x[i+1]
	int flag = 0;

	while(flag == 0)
	{
		flag = 1;
		for(i=1;i<20;i++)
		{
			if(x[i] < x[i-1])
			{
				temp = x[i-1];
				x[i-1] = x[i];
				x[i] = temp;
				flag = 0;
			}
		}
	}





	//Now that the array is sorted, print out the values of x[] in the following way
	//x[i] = xxxxx	
	//Note print one value per line
	for(i=0; i<20;i++)
	{
		printf("x[%i] = %f\n", i, x[i]);
	}


return 0;
}


double rand_float(double a, double b)
{
	return ((double)rand()/RAND_MAX)*(b-a) + a;
}
	

