aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301
diff options
context:
space:
mode:
authorRossTheRoss <msattr@gmail.com>2019-02-27 10:18:27 -0600
committerRossTheRoss <msattr@gmail.com>2019-02-27 10:18:27 -0600
commit54997c885f0d8500ce0a15a18de911ad6ecad2ce (patch)
tree1d27126e5deb0b95a1ffc6316515359b6447f1d4 /ee1301
parentSeed time to NULL (diff)
downloadhomework-54997c885f0d8500ce0a15a18de911ad6ecad2ce.tar
homework-54997c885f0d8500ce0a15a18de911ad6ecad2ce.tar.gz
homework-54997c885f0d8500ce0a15a18de911ad6ecad2ce.tar.bz2
homework-54997c885f0d8500ce0a15a18de911ad6ecad2ce.tar.lz
homework-54997c885f0d8500ce0a15a18de911ad6ecad2ce.tar.xz
homework-54997c885f0d8500ce0a15a18de911ad6ecad2ce.tar.zst
homework-54997c885f0d8500ce0a15a18de911ad6ecad2ce.zip
Final fixes
Diffstat (limited to 'ee1301')
-rw-r--r--ee1301/wk3/hw3_directory/strap012_HW3A.cpp45
-rw-r--r--ee1301/wk3/hw3_directory/strap012_HW3B.cpp45
-rw-r--r--ee1301/wk3/hw3_directory/strap012_HW3C.cpp37
3 files changed, 68 insertions, 59 deletions
diff --git a/ee1301/wk3/hw3_directory/strap012_HW3A.cpp b/ee1301/wk3/hw3_directory/strap012_HW3A.cpp
index 07dc6b5..38c2a2b 100644
--- a/ee1301/wk3/hw3_directory/strap012_HW3A.cpp
+++ b/ee1301/wk3/hw3_directory/strap012_HW3A.cpp
@@ -14,6 +14,30 @@ One-armed Bandit Simulator
#include <iomanip>
using namespace std;
+int spin_the_wheels(int d, int w);
+
+int main () {
+ srand(time(NULL));
+ int w, d, winCount, winTest;
+ for (w=3; w<=6; w++) {
+ winCount=0;
+ for (d=9; d<=27; d++) {
+ winCount=0;
+ for (long n=1000000; n>0; n--) { //The simulation is always done 1,000,000 times
+ int winTest = spin_the_wheels(d, w);
+ winCount+=winTest;
+ }
+ if (w==6) {
+ cout << scientific;
+ }
+ cout << "w=" << w << ", d=" << d
+ << ": Simulated probability = m/n = " << (winCount / 1000000.0) * 100.0 << "%. "
+ << "Theoretical probability = " << (d / (pow(d, w))) * 100 << "%." << endl;
+ d+=2;
+ }
+ }
+}
+
// Function: spin_the_wheels
// ---------------------------
// Simulates the one-armed bandit from the previous HW
@@ -24,7 +48,7 @@ using namespace std;
int spin_the_wheels(int d, int w) {
int spinOG=0, spinNew=0, win=0;
spinOG = ( (rand() % d) + 1); //Original spin is always the same
- w-=1;
+ w-=1; //The first wheel was spun as spinOG
for (int i=0; i<w; i++) {
spinNew = ( (rand() % d) + 1);
if (spinOG==spinNew) {
@@ -36,23 +60,4 @@ int spin_the_wheels(int d, int w) {
} else {
return 0;
}
-}
-
-int main () {
- srand(time(NULL));
- int w, d, winCount, winTest;
- for (w=3; w<=6; w++) {
- winCount=0;
- for (d=9; d<=27; d++) {
- winCount=0;
- for (long n=1000000; n>0; n--) {
- int winTest = spin_the_wheels(d, w);
- winCount+=winTest;
- }
- cout << "w=" << w << ", d=" << d
- << ": Simulated probability = m/n = " << (winCount / 1000000.0) * 100.0 << "%. "
- << "Theoretical probability = " << (d / (pow(d, w))) * 100 << "%." << endl;
- d+=2;
- }
- }
} \ No newline at end of file
diff --git a/ee1301/wk3/hw3_directory/strap012_HW3B.cpp b/ee1301/wk3/hw3_directory/strap012_HW3B.cpp
index 128f5fa..dac276d 100644
--- a/ee1301/wk3/hw3_directory/strap012_HW3B.cpp
+++ b/ee1301/wk3/hw3_directory/strap012_HW3B.cpp
@@ -14,28 +14,7 @@ Character Detection
#include <iomanip>
using namespace std;
-// Function: charTest
-// ---------------------------
-// Tests to see what kind of character was inputted
-// character: Self-explanatory
-// returns: 1 if number, 2 if lower case, 3 if upper case, 0 if not any of the previous
-
-int charTest (char character) {
- if (character >= '0' && character <= '9')
- {
- return 1;
- } else {
- if (character>='a' && character<='z') {
- return 2;
- } else {
- if (character>= 'A' && character<='Z') {
- return 3;
- } else {
- return 0;
- }
- }
- }
-}
+int charTest (char character);
int main () {
int test;
@@ -62,3 +41,25 @@ int main () {
cout << endl;
}
}
+
+// Function: charTest
+// ---------------------------
+// Tests to see what kind of character was inputted
+// input: character from prompt in main
+// returns: 1 if number, 2 if lower case, 3 if upper case, 0 if not any of the previous
+
+int charTest (char character) {
+ if (character >= '0' && character <= '9') {
+ return 1;
+ } else {
+ if (character>='a' && character<='z') {
+ return 2;
+ } else {
+ if (character>= 'A' && character<='Z') {
+ return 3;
+ } else {
+ return 0;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/ee1301/wk3/hw3_directory/strap012_HW3C.cpp b/ee1301/wk3/hw3_directory/strap012_HW3C.cpp
index e3fe714..61af587 100644
--- a/ee1301/wk3/hw3_directory/strap012_HW3C.cpp
+++ b/ee1301/wk3/hw3_directory/strap012_HW3C.cpp
@@ -14,10 +14,25 @@ Character Detection
#include <iomanip>
using namespace std;
+void swapCase(char s);
+
+int main()
+{
+ char character;
+ do { //This loops until '@' is entered
+ cout << "Please enter a character that is an ASCII letter in the range [A-Za-z]: ";
+ cin >> character;
+ if (character!='@') {
+ swapCase(character);
+ }
+ } while (character!='@');
+}
+
// Function: swapCase
// ---------------------------
// Swaps the case of a latin ASCII character and prints an error if not one
// s: the character inputted
+//Outputs: Nothing, the function just outputs the response
void swapCase(char s) {
char New;
@@ -27,23 +42,11 @@ void swapCase(char s) {
cout << ", I respond with " << New;
} else {
if (s >= 'A' && s <= 'Z') {
- New = s + 32;
- cout << ", I respond with " << New;
+ New = s + 32;
+ cout << ", I respond with " << New;
} else {
- cout << ", which is not a valid character";
+ cout << ", which is not a valid character.";
}
}
- cout << "." << endl;
-}
-
-int main()
-{
- char character;
- do { //This loops until '@' is entered
- cout << "Please enter a character that is an ASCII letter in the range [A-Za-z]: ";
- cin >> character;
- if (character!='@') {
- swapCase(character);
- }
- } while (character!='@');
-}
+ cout << endl;
+} \ No newline at end of file