#include <iostream> // basic i/o
#include <iomanip> // output formatting
#include <string> // string handling
using namespace std;

int maxValue(int nums[], int size){
  int max; //0 is an arbitrary value to compile
  // our code here
  max = nums[0];
  for(int i=0; i<size; i++){
    // cout << "Current max value: " << max << endl;
    if(max < nums[i]){
      max = nums[i];
      // print when it changes
      // cout << "New max value: " << max << endl;
    }
  }
  return max;
}

int minValue(int nums[], int size){
  int min; //0 is an arbitrary value to compile
  // our code here
  min = nums[0];
  for(int i=0; i<size; i++){
    // cout << "Current min value: " << min << endl;
    if(min > nums[i]){
      min = nums[i];
      // print when it changes
      // cout << "New min value: " << min << endl;
    }
  }
  return min;
}

bool isSorted(int nums[], int size){
  bool sorted = true;
  for(int i=0; i<size; i++){
    // if the next item exists
    if(i+1 < size){
      if(nums[i] > nums[i+1]){
        return false;
      }
    }
  }
  return sorted;
}

void printArray(int nums[], int size){
	cout << "Current array state:\n";
	for(int count=0; count < size; count++){
		cout << "[" << count << "]: " << nums[count] << endl; 
	}
  return;
}


int main(){

	int nums[] = {4,3,2,5,8,6,-2,1};
	int temp;
	int size = sizeof(nums)/sizeof(nums[0]);
	bool atEnd = false;
  int min, max;
	cout << "initial array:\n";
	for(int count=0; count < size; count++){
		cout << "[" << count << "]: " << nums[count] << endl; 
	}

  min = minValue(nums,size);
  cout << "\n***************************\n";
  cout << "The minimum value is: " << min << ".\n";
  cout << "***************************\n";

  max = maxValue(nums,size);
  cout << "\n***************************\n";
  cout << "The maximum value is: " << max << ".\n"; 
  cout << "***************************\n";

  while(!isSorted(nums,size)){
    for(int count=0; count < size; count++){
      if(count+1 == size){
        cout << "I'm at the end.\n";
        atEnd = true;
      }

      // swapping
      if(!atEnd && nums[count] > nums[count+1]){
        cout << "Swapping: " << nums[count] << " for " << nums[count+1] << endl;

        temp = nums[count];
        nums[count] = nums[count+1];
        nums[count+1] = temp;
      }
    }
    printArray(nums,size);
    atEnd = false;
  }

	return 0;
}
