// Copyright 1999
// College of Computer Science
// Northeastern University Boston MA 02115

// This software may be used for educational purposes as long as this copyright
// notice is retained at the top of all files

// Should this software be modified, the words "Modified from Original" must be
// included as a comment below this notice

// All publication rights are retained.  This software or its documentation may
// not be published in any media either in whole or in part.

///////////////////////////////////////////////////////////////////////////////

//	Recitation 4:	Using and writing conditional loops

//	STUDENT NAME:	¥?¥?¥
//	STUDENT ID:		¥?¥?¥
//	DATE:			¥?¥?¥

///////////////////////////////////////////////////////////////////////////////

// The standard include files that include traditional C and C++ headers and
// many other Core Tools headers ... see CHeaders.h for additional details

#include "IOTools.h"
#include "Graphics.h"
#include "Random.h"

// Enter project specific include files here as well as classes and functions
// that you choose to define in the main shell rather than in separate files

// prototypes

void Exercise1();
void Exercise2();
void Exercise3();
void Exercise4();
void Exercise5();

void CirclesInaRow(int r, int v);
void Exercise6();

void CirclesInaRowFromRight(int r, int v);
void Exercise7();

void ColumnOfCircles(int r, int h);
void Exercise8();
void Exercise9();
void Exercise10();

 
// Recitation 4: EXERCISES

// definitions

void Exercise1(){
// Write a program that will print the values of n and n! 
// as long as n! < 10000

	cout << "Print n and n! while n! < 10000" << endl;
	
	int nfac = 1;									// base of the compound product
	int n = 1;										// the  counter and multiplier
	
	while (nfac < 10000){							// repeat as long as n! < 10000
	
		cout << setw(2) << n <<  setw(8) << nfac << endl;
		
		n = n + 1;
		nfac = nfac * n;							// compute the next factorial
	};


};

int SavingsPlan(int goal, double payments, double intRate){
// Write a function that will compute how many months will it take to reach savings goal
// User types in the monthly payments, and the yearly interest rate

	cout << "Time needed to reach savings goal" << endl;

	int savings = payments;							// amount saved after 1st month
	int count = 1;									// counts the number of months
	
	while (savings < goal){
		count++;									// next month
		savings = savings * (1 + intRate/12) + payments;	// total after the next month
	} 
	
	return (count);									// number of months you were saving
};


void Exercise2(){
// Set up a driver that will ask the user to type in monthly payments to the savings plan
// the interest rate (yearly) and the desired savings goal
// Call function SavingsPlan and print the number of months needed to reach the goal
// Allow the user to repeat the exercise with new set of input data - until no data is given

	
	cout << "Compute the time needed to reach savings goal" << endl;
	
	int n;
	double rate;
	double monthly;
	int goal;
	
	while (Confirm("Another scenario?", true)) {

		// collect data from the user
		monthly = RequestDouble("What will your monthly payment be?", 100);
		rate = RequestDouble("Yearly interest rate?", 0.05);
		goal = RequestInt("How much would you like to save?", 5000);
		
		n = SavingsPlan(goal, monthly, rate);
		
		cout << "You will save $" << goal << " in " << n << " months." << endl;
	
	};
	

};


void Exercise3(){
// Write a program that will simulate the toss of a coin
// and count the number of tosses before two heads are tossed consecutively
// Print all tosses to make sure the program runs correctly

	cout << "Tossing a coin till two consecutive heads." << endl;

	bool lastToss = false;				// set last toss to tails
	bool toss = false;					// set current toss to tails
	int n = 0;							// set up a counter for tosses

	while (!(lastToss && toss)){		// one of the last two tosses was not head
		
		lastToss = toss;				// remember the last toss
		toss = RandomLong(0, 1);		// toss again
		n++;							// and count the toss

		cout << n << " : " << toss << endl;

	}

	cout << "The first consecutive heads were tossed at the tosses ";
	cout << n-1 << " and " << n << endl;


};

void Exercise4(){
// Write a program that will simulate the roll of a die
// and count the number of rolls before a five is rolled
// Print all the rolls to see that your program runs correctly

	cout << "Rolling a die till five is rolled." << endl;

	int roll = 0;						// set the roll to 0 to start the loop
	int n = 0;							// set up a counter for the rolls

	while (roll != 5) {
		
		roll = RandomLong(1, 6);		// roll a die
		n++;							// update the count

		cout << n << " : " << roll << endl;

	};

	cout << "It took " << n << " rolls to get a five." << endl;

};

void Exercise5(){


};

void CirclesInaRow(int r, int v){
// Paint as many circles in a row, with center at the vertical v
// and of the given radius r as will fit into the graphics window
// Print how many circles were painted.

	int n = 0;							// set up a counter for the circles
	int xcenter = r;
	
	while (xcenter + r <= 300){
		
		PaintCircle(xcenter, v, r);		// paint the circle
		xcenter += 2 * r;				// move the center
		n++;							// increment the count

	};

	cout << "We painted " << n << " circles." << endl;

};

void Exercise6(){
// Write a program that will ask the user for a radius of a circle
// and a vertical coordinate. The program will then call a function
// CirclesInaRow that will paint as many circles as will fit into
// the graphics window and print how many circles were painted.
// Allow the user to repeat the requests without erasing the graphics

	int y;								// vertical coordinate of the centers
	int radius;							// radius of circles

	cout << "Painting circles in a row." << endl;

	while (Confirm("Another row of circles?", true)){
		ClearDrawing();
		
		// collect data from the user
		radius = RequestLong("Select the radius of circles:", 12);
		y = RequestLong("Select the vertical location: ", 120);

		// set some nice color for the circles
		SetForeColor(255, radius, y);

		// paint the row of circles
		CirclesInaRow(radius, y);

	}

};

void CirclesInaRowFromRight(int r, int v){
// Paint as many circles in a row, with center at the vertical v
// and of the given radius r as will fit into the graphics window
// Print how many circles were painted.

	int n = 0;							// set up a counter for the circles
	int xcenter = 300 - r;
	
	while (xcenter - r >= 0){
		
		PaintCircle(xcenter, v, r);		// paint the circle
		xcenter -= 2 * r;				// move the center
		n++;							// increment the count

	};

	cout << "We painted " << n << " circles." << endl;

};

void Exercise7(){
// Write a program that will ask the user for a radius of a circle
// and a vertical coordinate. The program will then call a function
// CirclesInaRowFromRight that will paint as many circles as will fit into
// the graphics window (starting at the right edge of the window)
// and print how many circles were painted.
// Allow the user to repeat the requests without erasing the graphics

	int y;								// vertical coordinate of the centers
	int radius;							// radius of circles

	cout << "Painting circles in a row starting on the right." << endl;

	while (Confirm("Another row of circles?", true)){
		ClearDrawing();
		
		// collect data from the user
		radius = RequestLong("Select the radius of circles:", 12);
		y = RequestLong("Select the vertical location: ", 120);

		// set some nice color for the circles
		SetForeColor(255, radius, y);

		// paint the row of circles
		CirclesInaRowFromRight(radius, y);

	}

};

void ColumnOfCircles(int r, int h){
// Paint as many circles in a row, with center at the vertical v
// and of the given radius r as will fit into the graphics window
// Print how many circles were painted.

	int n = 0;							// set up a counter for the circles
	int ycenter = r;
	
	while (ycenter + r <= 300){
		
		PaintCircle(h, ycenter, r);		// paint the circle
		ycenter += 2 * r;				// move the center
		n++;							// increment the count

	};

	cout << "We painted " << n << " circles." << endl;

};

void Exercise8(){
// Write a program that will ask the user for a radius of a circle
// and a horizontal coordinate. The program will then call a function
// ColumnOfCircles that will paint as many circles as will fit into
// the graphics window (starting at the top edge of the window)
// and print how many circles were painted.
// Allow the user to repeat the requests without erasing the graphics

	int y;								// horizontal coordinate of the centers
	int radius;							// radius of circles

	cout << "Painting circles in a column starting on the top.." << endl;

	while (Confirm("Another column of circles?", true)){
		ClearDrawing();
		
		// collect data from the user
		radius = RequestLong("Select the radius of circles:", 12);
		y = RequestLong("Select the horizontal location: ", 120);

		// set some nice color for the circles
		SetForeColor(255, radius, y);

		// paint the row of circles
		ColumnOfCircles(radius, y);

	}


};



void Exercise9(){

};

void Exercise10(){


};



int main(int argc, char* argv[]) {

	// Use the following line if you choose NOT to open any graphics windows
	// InitializeConsole();

	// Build graphics window 0
	GraphicsWindow GW0(300, 300);

	// Move the console below graphics window 0
	ConsolePlaceBelow(0);

	// Give the console the focus for user interaction
	MakeConsoleForeground();

//////////

	// Enter the main program here
	SetRandomSeed();

	int i = 1;
	
	while (Confirm("Another exercise?", true)){		// loop to run another exercise
		i = RequestInt("Exercise number: ", i);		// determine which exercise to run
													// the default moves you to the next one
		cout << endl << "Exercise " << i << endl;	// notify user of the accepted selection
		switch (i){									// un the appropriate exercise
			case 1: {
					Exercise1();
					break;
					}
			case 2: {
					Exercise2();
					break;
					}
			case 3: {
					Exercise3();
					break;
					}
			case 4: {
					Exercise4();
					break;
					}
			case 5: {
					Exercise5();
					break;
					}
			case 6: {
					Exercise6();
					break;
					}
			case 7: {
					Exercise7();
					break;
					}
			case 8: {
					Exercise8();
					break;
					}
			case 9: {
					Exercise9();
					break;
					}
			case 10: {
					Exercise10();
					break;
					}
			default:
					Exercise1();
		
		};
		i = (i+1);									// next time, continue with the next exercise
		if (i > 10)									// we only have 10 exercises - cycle through again
			i = 1;
		cout << endl;

	}
	
//////////

	// The lines below make sure that the graphics windows remain open just
	// before the program terminates

	PressReturn("\nThe main program is about to terminate\n");

	return 0;
}

