Skip to content

Commit

Permalink
Excersices 7.13, 7.16
Browse files Browse the repository at this point in the history
  • Loading branch information
PureJoyMind committed Apr 11, 2022
1 parent 0c69469 commit a645722
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ch.07/DiceRolling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Deitel ch.06 exercise 7.16
// Simulating the rolling of two dice 36.000.00 times
#include <iostream>
#include <array>
#include <iomanip>
#include <random>

using namespace std;

default_random_engine eng{static_cast<unsigned int>(time(0))};
uniform_int_distribution<unsigned int> randInt{1, 6};

int main(){
array<unsigned int, 13> dice{};

unsigned int d1{0}, d2{0};

unsigned int sum{0};

for (int i{1}; i <= 36'000'000; i++){
d1 = randInt(eng);
d2 = randInt(eng);
sum = d1 + d2;

++dice[sum];
}

cout << "The results are:" << endl;
for(int i{2}; i < 13; i++){
if(i > 9)
cout << i << ':' << setw(9) << dice[i] <<endl;
else
cout << i << ':' << setw(10) << dice[i] <<endl;
}
}
58 changes: 58 additions & 0 deletions ch.07/dupEliminationVec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Deitel ch.06 exercise 7.13
// Duplicate elimination with array

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool searchVec(vector<int> &, int);

int main()
{
vector<int> num;
int tmp{0}; // To get user input and if validated add to array

cout << "Enter 20 numbers between 10 & 100:\n";

bool srch{false}; // Variable to hold search answer

for (int i{0}; i < 20; i++) // Looping the array
{
cin >> tmp;
while (tmp > 100 || tmp < 10) // Bounds checking
{
cout << "Out of bounds: \n";
cin >> tmp;
}

srch = searchVec(num, tmp); // Searching user input in array, true if found
if (srch)
{
cout << "duplicate\n";
continue; // If duplicate was found jump to next index
}

num.push_back(tmp); // If no duplicate existed add to array
}

sort(num.begin(), num.end());

cout << "The entered numbers are:\n";
for (int i{0}; i < num.size(); i++)
{
cout << num[i] << ' ';
}
cout << endl;
}

bool searchVec(vector<int> &n, int a)
{
for (int i : n)
{
if (i == a)
return true;
}
return false;
}

0 comments on commit a645722

Please sign in to comment.