#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;

//below are the answers to writing user-defined functions

//find the maximum of 3 integers and return the maximum value. 
//if you change all the ints to doubles, this works for doubles too
int maxOf3(int a, int b, int c) {
	if (a >= b && a >= c) {
		return a;
	}
	else if (b >= a && b >= c) {
		return b;
	}
	else {
		return c;
	}
}

//this is some interesting math function. Why does it return such interesting value(s) I wonder.
double funFun(double x) {
	return 0.5 * sin(x) * sin(x) + 0.5*cos(x)*cos(x);
}

//wanna know how far you will fall in 2 seconds ignoring some things like drag/etc? 
//Plug in 0, 32.2 and 2 to this function to get an answer in ft.
//plug in 0, 9.8 and 2 to get an answer in meters
double distanceTraveled(double v, double a, double t) {
	return v*t + 0.5*a*t*t;
}

//get input for a value that must be greater than 0 - a positive integer
//the string is for a prompt so the user is told what to enter
void getPosInteger(string prompt, int& val) {
	cout << prompt;
	cin >> val;
	while (val <= 0) {
		cerr << "Error in getPosInteger - Try Again ";
		cout << prompt;
		cin >> val;
	}
}


int main() {
	srand(time(NULL)); //for use later with rand

	//answers to the vector questions #1 and #4
	//how many standard deviations away is each element
	int num_grades = 5;
	//to make this arbitrary, allow the user to enter this value
	//cin >> num_grades; //if you want something other than 5 grades
	vector<int> grades;

	//this block pushes on user input into the vector
	for (int i = 0; i < num_grades; i++) {
		int v;
		cout << "enter grade " << i + 1 << ": ";
		cin >> v;
		grades.push_back(v);
	}

	//this block gets the sum of all the grades in the vector
	double sum = 0; 
	for (int i = 0; i < grades.size(); i++) {
		sum += grades.at(i); // sum = sum + grades.at(i);
	}

	//get the average of all the entered numbers
	double avg = sum / grades.size();

	cout << "The average of the grades is: " << avg << endl;

	//okay, compute the sum of the squared difference of each element with the average.
	sum = 0;
	for (int i = 0; i < grades.size(); i++) {
		sum += pow(grades.at(i) - avg, 2.0);
	}

	//find the standard deviation 
	double stddev = sqrt(sum / grades.size());

	cout << "The standard deviation of these grades is: " << stddev << endl;

	//How many standard deviations away from the average is each grade?
	for (int i = 0; i < grades.size(); i++) {
		cout << i << ": " << grades.at(i) << "       ";
		cout << fabs(grades.at(i) - avg) / stddev << endl;
	}


	//make a vector of 30 ints- it might useful to know another way to create this vector
	vector<int> mylist;
	for (int i = 0; i < 30; i++) {
		int v;
		cout << "Enter mylist " << i + 1 << " : ";
		cin >> v; //get some user input
		mylist.push_back(v); //put that user input in the back of the vector
	}

	//print a vector
	cout << "   Input: ";
	for (int i = 0; i < mylist.size(); i++) {
		cout << mylist[i] << " ";
	}
	cout << endl;

	//reverse the order of a vector (of 30 ints)
	vector<int> templist(mylist.size()); //how about via a temporary list?
	for (int i = 0; i < mylist.size(); i++) {
		templist.at(i) = mylist.at(mylist.size()-1 - i);
	}
	mylist = templist; //now just copy it over
	//can you think of a way you might do this "in place" without making a new vector?
	//perhaps you could write a function to swap two integers (using call by reference)

	//print a vector after the reverse
	cout << "Reversed: ";
	for (int i = 0; i < mylist.size(); i++) {
		cout << mylist[i] << " ";
	}
	cout << endl;

	//Next Problem: move all elements down in a vector (of 30 ints)
	int temp = mylist.at(0); //back this up for later
	for (int i = 0; i < mylist.size() - 1; i++) {
		mylist.at(i) = mylist.at(i + 1); //copy the one further down in the list to its new home
	}
	mylist.at(mylist.size() - 1) = temp; //set the last equal to the first 


	//print a vector after the moving down
	cout << "  Moved: ";
	for (int i = 0; i < mylist.size(); i++) {
		cout << mylist[i] << " ";
	}
	cout << endl;



										 //vector of strings for birds - fill it in as user wishes - choose two randomly and add them and output
	vector<string> birds;
	string more = "yes";
	cout << "Enter a bird name (or quit to quit): ";
	cin >> more;	
	while (more != "quit") {
		birds.push_back(more); //add this new bird name to the list
		cout << "Enter a bird name (or quit to quit): ";
		cin >> more;
	}

	//now have a vector of bird names - find two at random and output their sum
	if (birds.size() == 0) {
		cout << "No birds were given. :(" << endl;
	}
	else if (birds.size() == 1) {
		cout << "Only one bird given. Using its name twice: " << birds[0] + birds[0] << endl;
	}
	else {
		//get two random numbers from 0 to size -1
		int r1 = rand() % birds.size();
		int r2 = rand() % birds.size();
		cout << "The new bird name is: " << birds[r1] + birds[r2] << endl;
	}

}

