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

void sort(int x[],int nItems);
int search(int x[], int nItems, int lookFor);

int main(void){	
	srand(time(NULL));
	int toSort[5];
	//for every item in the array
	int i;
	for (i =0; i < 5; i++){
		//put a random number between 0 and 99 in that spot
		//toSort[i] = rand() % 100;
		//printf("%d\n",toSort[i]);
		scanf("%d",&toSort[i]);
	}

	int look;
	printf("What number would you like to look for? ");
	scanf("%d",&look);

	int spotFound = search(toSort,5,look);
	if (spotFound == -1){
		printf("%d not found\n",look);
	}
	else{
		printf("%d found at %d\n",look,spotFound);	
	}

	//sorts the array
	//because it was an array I didn't have to pass it in as a pointer
	sort(toSort,5);

	//print out the sorted array
	printf("SORTED: \n");
	for (i = 0; i < 5; i++){
		printf("\t%d\n",toSort[i]);
	}

	return 0;
}	

void sort(int x[],int nItems){
	//for every spot in the array
	int currentPlace;
	for (currentPlace = 0; currentPlace < nItems; currentPlace++){
		//look for the integer that goes in that spot
		//I only need to consider integers that are 
		//past the spot
		int consider;
		//the smallest person I've seen so far is at the starting place
		//for this iteration
		int smallest = currentPlace;
		for (consider = currentPlace; consider < nItems; consider++){
			//if the number I'm looking at right now is smaller
			//than the smallest I've seen so far
			if(x[smallest] > x[consider]){
				//it's my new smallest
				smallest = consider;
			}
		}
		//by this time we've looked through the entire array for this
		//iteration
		//we swap the smallest to its rightful place
		int swap = x[smallest];

		//in the place of the smallest element, I put the element
		//that's at the spot that smallest belongs
		x[smallest] = x[currentPlace];

		//put the smallest element in the spot where they belong
		x[currentPlace] = swap;

	}
}

int search(int x[], int nItems, int lookFor){
	int currentSpot;
	for (currentSpot = 0; currentSpot < nItems; currentSpot++){
		if (x[currentSpot] == lookFor){
			return currentSpot;
		}
	}
	//by the time we've got here we've looked at every element in the array
	//and none of them matched
	return -1;
}
