-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c69469
commit a645722
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |