// 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 5:	Using and writing conditional statements; logical expressions

//	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 Exercise6();
void Exercise7();
void Exercise8();
void Exercise9();
void Exercise10();

 
// Recitation 5: EXERCISES

// definitions

void Exercise1(){
// Regular price of a ticket for museum is $10.
// Seniors (age 65 and over) pay 1/2
// Write a program that keeps asking for the age of the next visitor
// and prints the price of the ticket.
// Use ReadingInt function to stop the loop when no more customers arrive.
// Count the number of visitors.

	cout << "Selling tickets for adults and seniors" << endl;
	int price = 10;				// price of a regular ticket
	int cnt = 0;				// number of customers
	int age;					// customer's age

	while (ReadingInt("Visitor's age:", age)){

		cnt++;					// next visitor

		if (age >= 65)
			cout << "Ticket price is: $" << price/2 << endl;
	
		else 
			cout << "Ticket price is: $" << price << endl;
		
	};

	cout << "Today we had " << cnt << " visitors." << endl;

};


void Exercise2(){
// Write a program that will simulate ten tosses of a coin.
// If a head is tossed, the program prints "I win!"
// If a tail is tossed, the program prints "You loose!"


};

void Exercise3(){
// Modify Exercise 1 so it also computes the total value of the tickets sold.
// At the end the program should print the total value of tickets sold.

	cout << "Selling tickets for adults and seniors" << endl;
	cout << "Also computing the total money taken in." << endl;

	int price = 10;				// price of a regular ticket
	int cnt = 0;				// number of customers
	int age;					// customer's age
	int total = 0;				// total value of tickets sold

	while (ReadingInt("Visitor's age:", age)){
		cnt++;					// next visitor
		if (age >= 65){			// seniors at half price
			cout << "Ticket price is: $" << price/2 << endl;
			total = total + price/2;
		}
		else {					// others at a regular price
			cout << "Ticket price is: $" << price << endl;
			total = total + price;
		};
	};
	cout << "Total value of tickets sold is $" << total << endl;

};


void Exercise4(){
// Write a program that will simulate 100 tosses of a coin
// and counts the number of heads.
// If the number of heads is greater than 55 or smaller than 45
// the program prints "Unfair coin!" otherwise it prints "Fair coin!"
// The program should also print the number of heads tossed.

	cout << "Testing a coin for fairness." << endl;

	int heads = 0;			// counter for number of heads
	int toss;				// one toss
	int n;					// counter of total number of tosses
	
	for (n = 1; n <= 100; n++){
		toss = RandomLong(0, 1);
		heads = heads + toss;
	};

	if ((heads > 55) || (heads < 45))
		cout << "Unfair coin!" << endl;
	else
		cout << "Fair coin!" << endl;

	cout << " The number of heads was: " << heads << endl;

};

void Exercise5(){
// Write a program that will simulate the roll of two die 100 times 
// and count the number of times both dice showed the same value

	cout << "Count the number of matched pairs in 100 rolls of two dice" << endl;

	int die1;
	int die2;
	int n;						// loop counter
	int cnt = 0;				// counter for the number of matched rolls

	for (n = 1; n <= 100; n++){
		die1 = RandomLong(1, 6);
		die2 = RandomLong(1, 6);

		if (die1 == die2)		// Check for a match
			cnt++;
	}

	cout << "Both dice showed the same value " << cnt << " times." << endl;
};

void Exercise6(){
// Write a program that will simulate the roll of a die 600 times
// and count the number of rolls when a five is rolled.
// The program prints "Unfair die!" if the number of times five was rolled is
// either less than 95 or greater than 105
// Otherwise it prints "Fair die!"
// The program should also print the number of times five was rolled.


};

bool FitsIn(int x, int y){
// returns true if the coordinates represent a point in the graphics window
	return ((x >= 0)    &&
			(x <= 300)  &&
			(y >= 0)    &&
			(y <= 300));
};

void Exercise7(){
// Write a program that will simulate Brownian motion of an atom
// The program will paint the lines that represent the movement of an atom
// The starting position will be in the center of the graphice window
// The atom will move in a random direction at a distance between
//	- 20 and 20 in both horizontal and vertical direction
// Count the number of moves before the atom reaches the boundary of the 
// graphics window
// Use FitsIn function that returns a bool value to check whether the 
// atom is still within bounds

	cout << "Brownian motion" << endl;

	int x = 150;						// location of the atom
	int y = 150;
	MoveTo(x, y);
	int cnt = 0;

	while (FitsIn(x, y)){
		LineTo(x, y);
		cnt++;
		x = x + RandomLong(-20, 20);
		y = y + RandomLong(-20, 20);
	};
	cout << "The atom made " << cnt << " moves." << endl;
};

void Swap(int&x, int&y){
	int temp = x;
	x = y;
	y = temp;
};

void Exercise8(){
// Write a program that will ask the user to type in three numbers
// and will print them in order - from the smallest to the largest
	
	cout << " Program that orders three numbers " << endl;

	int a = RequestInt(" Type in the first number:");
	int b = RequestInt(" Type in the second number:");
	int c = RequestInt(" Type in the third number:");

	if (a > b)
		Swap(a, b);
	if (a > c)
		Swap(a, c);				// now a is the smallest
	if (b > c) 
		Swap (b, c);			// now b and c are also in order

	cout << "The number in order are: " << a << " " << b << " " << c << endl;


};
void Exercise9(){
// Write a program that will assign a grade for the exam
// grade < 60 is F
// 60 <= grade < 80 is C
// 80 <= grade < 90 is B
// 90 <= grade is A
// User types in the points earned on the exam, the program prints the grade

	cout << "Grading program" << endl;

	int grade = RequestInt("Type in the points earned");

	if (grade < 60)
		cout << "F" << endl;
	else if (grade < 80)
		cout << "C" << endl;
	else if (grade < 90)
		cout << "B" << endl;
	else
		cout << "A" << endl;

};

void Exercise10(){
// Modify Exercise 3 so it also provides discount to children as follows:
// Children six and under get a free ticket
// Children of ages 7 to 11 pay $3


};




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;
}

