Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <array>
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- #include <iomanip>
- using namespace std;
- void craps(int);
- // bubblesort template
- template < typename T, size_t size >
- void bubblesort(array<T, size> &toSort) {
- for (size_t i = 0; i < toSort.size() - 1; i++) {
- for (size_t j = 0; j < (toSort.size() - 1); j++){
- if (toSort[j] > toSort[j + 1]) swapElems(toSort[j], toSort[j + 1]);
- }
- }
- }
- // selectionsort template
- template < typename T, size_t size >
- void selectionsort(array<T, size> &toSort) {
- bool change = false;
- T smallest = toSort[0];
- size_t place = 0;
- for (size_t i = 0; i < toSort.size(); i++) {
- change = false;
- smallest = toSort[i];
- for (size_t j = i; j < toSort.size(); j++) {
- if (toSort[j] < smallest) {
- smallest = toSort[j];
- place = j;
- change = true;
- }
- }
- if (change) swapElems(toSort[i], toSort[place]);
- }
- }
- // lessEqualSorted template
- template < typename T, size_t size >
- bool lessEqualSorted(const array< T, size >& anArray) {
- for (size_t i = 0; i < anArray.size() - 1; i++) {
- if (anArray[i] > anArray[i + 1]) return false;
- }
- return true;
- }
- // printArray template
- template < typename T, size_t size >
- void printArray(const array< T, size >& anArray) {
- cout << "Array = { ";
- for (T elems : anArray) cout << elems << ", ";
- cout << "\b\b }" << endl;
- }
- // swapElems template
- template < typename T >
- void swapElems(T& item1, T& item2) {
- T tmp;
- tmp = item1;
- item1 = item2;
- item2 = tmp;
- }
- int main() {
- int windowOpen = 0;
- // Exercise 1
- craps(1000000);
- // Exercise 2
- srand(time(nullptr));
- array<int, 100> arrayOne = { 0 };
- array<string, 100> stringArray = { " " };
- for (size_t i = 0; i < arrayOne.size(); i++) arrayOne[i] = rand() % 100;
- for (size_t i = 0; i < stringArray.size(); i++) {
- string aString;
- for (size_t j = 0; j < 4; j++) aString += 'a' + rand() % ('z' - 'a');
- stringArray[i] = aString;
- }
- printArray(arrayOne);
- cout << "lessEqualSorted: " << lessEqualSorted(arrayOne) << endl;
- selectionsort(arrayOne);
- printArray(arrayOne);
- cout << "lessEqualSorted: " << lessEqualSorted(arrayOne) << endl;
- printArray(stringArray);
- cout << "lessEqualSorted: " << lessEqualSorted(stringArray) << endl;
- bubblesort(stringArray);
- printArray(stringArray);
- cout << "lessEqualSorted: " << lessEqualSorted(stringArray) << endl;
- // keep window open
- cin >> windowOpen;
- return 0;
- }
- void craps(int numberOfGames) {
- const int arraySize = 22;
- array<size_t, arraySize> winsOfRolls = { 0 }; // create an array of size 22 and initialize it with 22 "zeros"
- array<size_t, arraySize> losesOfRolls = { 0 };
- srand(time(nullptr));
- int numberOfWins = 0;
- int numberOfLoses = 0;
- int diceValue = 0;
- int numberOfCurrentRolls = 0;
- int numberOfAllRolls = 0;
- int point = 0;
- for (int i = 1; i <= numberOfGames; i++) {
- numberOfCurrentRolls = 1;
- diceValue = rand() % 6 + 1 + rand() % 6 + 1;
- if ((diceValue == 7) || (diceValue == 11)) winsOfRolls[1]++;
- else if ((diceValue == 2) || (diceValue == 3) || (diceValue == 12)) losesOfRolls[1]++;
- else {
- point = diceValue;
- while (diceValue != 7) {
- numberOfCurrentRolls++;
- diceValue = rand() % 6 + 1 + rand() % 6 + 1;
- if ((diceValue == point) && (numberOfCurrentRolls < 21)) {
- winsOfRolls[numberOfCurrentRolls]++;
- break;
- }
- else if ((diceValue == point) && (numberOfCurrentRolls >= 21)) {
- winsOfRolls[21]++;
- break;
- }
- }
- if ((diceValue == 7) && (numberOfCurrentRolls < 21)) losesOfRolls[numberOfCurrentRolls]++;
- else if ((diceValue == 7) && (numberOfCurrentRolls >= 21)) losesOfRolls[21]++;
- }
- numberOfAllRolls += numberOfCurrentRolls;
- }
- for (int n : winsOfRolls) numberOfWins += n;
- for (int n : losesOfRolls) numberOfLoses += n;
- for (int j = 1; j < 22; j++) cout << setw(3) << j << ". Roll:" << " Wins - " << setw(8) << winsOfRolls[j] << setw(10)
- << "Loses - " << setw(8) << losesOfRolls[j] << endl;
- cout << "Chance of Win: " << (static_cast<float>(numberOfWins) / numberOfGames)*100 << " %" << endl;
- cout << "Avg. Rolls per Game: " << static_cast<float>(numberOfAllRolls) / numberOfGames << endl;
- cout << "Change of Win in late game >20. Roll: " << (static_cast<float>(winsOfRolls[21]) / (winsOfRolls[21] + losesOfRolls[21]))*100 << " %" << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement