Advertisement
algore87

PG1Lab4

Jun 2nd, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <array>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10. void craps(int);
  11.  
  12. // bubblesort template
  13. template < typename T, size_t size >
  14. void bubblesort(array<T, size> &toSort) {
  15.    for (size_t i = 0; i < toSort.size() - 1; i++) {
  16.       for (size_t j = 0; j < (toSort.size() - 1); j++){
  17.          if (toSort[j] > toSort[j + 1]) swapElems(toSort[j], toSort[j + 1]);
  18.       }
  19.    }
  20. }
  21.  
  22. // selectionsort template
  23. template < typename T, size_t size >
  24. void selectionsort(array<T, size> &toSort) {
  25.    bool change = false;
  26.    T smallest = toSort[0];
  27.    size_t place = 0;
  28.    for (size_t i = 0; i < toSort.size(); i++) {
  29.       change = false;
  30.       smallest = toSort[i];
  31.       for (size_t j = i; j < toSort.size(); j++) {
  32.          if (toSort[j] < smallest) {
  33.             smallest = toSort[j];
  34.             place = j;
  35.             change = true;
  36.          }
  37.       }
  38.       if (change) swapElems(toSort[i], toSort[place]);
  39.    }
  40. }
  41.  
  42. // lessEqualSorted template
  43. template < typename T, size_t size >
  44. bool lessEqualSorted(const array< T, size >& anArray) {
  45.    for (size_t i = 0; i < anArray.size() - 1; i++) {
  46.       if (anArray[i] > anArray[i + 1]) return false;
  47.    }
  48.    return true;
  49. }
  50.  
  51. // printArray template
  52. template < typename T, size_t size >
  53. void printArray(const array< T, size >& anArray) {
  54.    cout << "Array = { ";
  55.    for (T elems : anArray) cout << elems << ", ";
  56.    cout << "\b\b }" << endl;
  57. }
  58.  
  59. // swapElems template
  60. template < typename T >
  61. void swapElems(T& item1, T& item2) {
  62.    T tmp;
  63.    tmp = item1;
  64.    item1 = item2;
  65.    item2 = tmp;
  66. }
  67.  
  68.  
  69.  
  70. int main() {
  71.    int windowOpen = 0;
  72.  
  73.    // Exercise 1
  74.    craps(1000000);
  75.  
  76.    // Exercise 2
  77.    srand(time(nullptr));
  78.    array<int, 100> arrayOne = { 0 };
  79.    array<string, 100> stringArray = { " " };
  80.    for (size_t i = 0; i < arrayOne.size(); i++) arrayOne[i] = rand() % 100;
  81.    for (size_t i = 0; i < stringArray.size(); i++) {
  82.       string aString;
  83.       for (size_t j = 0; j < 4; j++) aString += 'a' + rand() % ('z' - 'a');
  84.       stringArray[i] = aString;
  85.    }
  86.    printArray(arrayOne);
  87.    cout << "lessEqualSorted: " << lessEqualSorted(arrayOne) << endl;
  88.    selectionsort(arrayOne);
  89.    printArray(arrayOne);
  90.    cout << "lessEqualSorted: " << lessEqualSorted(arrayOne) << endl;
  91.    printArray(stringArray);
  92.    cout << "lessEqualSorted: " << lessEqualSorted(stringArray) << endl;
  93.    bubblesort(stringArray);
  94.    printArray(stringArray);
  95.    cout << "lessEqualSorted: " << lessEqualSorted(stringArray) << endl;
  96.  
  97.    // keep window open
  98.    cin >> windowOpen;
  99.  
  100.  
  101.  
  102.  
  103.  
  104.    return 0;
  105. }
  106.  
  107.  
  108.  
  109. void craps(int numberOfGames) {
  110.    const int arraySize = 22;
  111.    array<size_t, arraySize> winsOfRolls = { 0 };  // create an array of size 22 and initialize it with 22 "zeros"
  112.    array<size_t, arraySize> losesOfRolls = { 0 };
  113.    srand(time(nullptr));
  114.    int numberOfWins = 0;
  115.    int numberOfLoses = 0;
  116.    int diceValue = 0;
  117.    int numberOfCurrentRolls = 0;
  118.    int numberOfAllRolls = 0;
  119.    int point = 0;
  120.    for (int i = 1; i <= numberOfGames; i++) {
  121.       numberOfCurrentRolls = 1;
  122.       diceValue = rand() % 6 + 1 + rand() % 6 + 1;
  123.       if ((diceValue == 7) || (diceValue == 11)) winsOfRolls[1]++;
  124.       else if ((diceValue == 2) || (diceValue == 3) || (diceValue == 12)) losesOfRolls[1]++;
  125.       else {
  126.          point = diceValue;
  127.          while (diceValue != 7) {
  128.             numberOfCurrentRolls++;
  129.             diceValue = rand() % 6 + 1 + rand() % 6 + 1;
  130.             if ((diceValue == point) && (numberOfCurrentRolls < 21)) {
  131.                winsOfRolls[numberOfCurrentRolls]++;
  132.                break;
  133.             }
  134.             else if ((diceValue == point) && (numberOfCurrentRolls >= 21)) {
  135.                winsOfRolls[21]++;
  136.                break;
  137.             }
  138.          }
  139.          if ((diceValue == 7) && (numberOfCurrentRolls < 21)) losesOfRolls[numberOfCurrentRolls]++;
  140.          else if ((diceValue == 7) && (numberOfCurrentRolls >= 21)) losesOfRolls[21]++;
  141.       }
  142.       numberOfAllRolls += numberOfCurrentRolls;
  143.    }
  144.    for (int n : winsOfRolls) numberOfWins += n;
  145.    for (int n : losesOfRolls) numberOfLoses += n;
  146.    for (int j = 1; j < 22; j++) cout << setw(3) << j << ". Roll:" << " Wins - " << setw(8) << winsOfRolls[j] << setw(10)
  147.       << "Loses - " << setw(8) << losesOfRolls[j] << endl;
  148.    cout << "Chance of Win: " << (static_cast<float>(numberOfWins) / numberOfGames)*100 << " %" << endl;
  149.    cout << "Avg. Rolls per Game: " << static_cast<float>(numberOfAllRolls) / numberOfGames << endl;
  150.    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