From f0bf055d9e84d069c28912ed56b06178f59df2cc Mon Sep 17 00:00:00 2001 From: Namesh Kher Date: Fri, 4 Dec 2015 19:02:41 -0500 Subject: [PATCH] Added c++ solutions for Chapter 17 for ques 1, 3 and 5 --- c++/Chapter 17/Question17_1.cpp | 48 +++++++++++++++++ c++/Chapter 17/Question17_3.cpp | 77 +++++++++++++++++++++++++++ c++/Chapter 17/Question17_5.cpp | 94 +++++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 c++/Chapter 17/Question17_1.cpp create mode 100644 c++/Chapter 17/Question17_3.cpp create mode 100644 c++/Chapter 17/Question17_5.cpp diff --git a/c++/Chapter 17/Question17_1.cpp b/c++/Chapter 17/Question17_1.cpp new file mode 100644 index 00000000..4d8054e2 --- /dev/null +++ b/c++/Chapter 17/Question17_1.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +void swap(int*, int*); +void swap_alternate(int&, int&); + +void swap(int* a, int* b) { + + (*a) = (*a) - (*b); + (*b) = (*a) + (*b); + (*a) = (*b) - (*a); +} + +void swap(int& a, int& b) { + a = a^b; + b = a^b; + a = a^b; +} + +int main() { + + int num1 = 10, num2 = 20; + cout << "\n------------- Using swap() method -------------\n" << endl; + cout << "Before Swap, num1 = " << num1 << " , num2 = " << num2 << endl; + swap(num1, num2); + cout << "After Swap, num1 = " << num1 << " , num2 = " << num2 << endl; + + num1 = 50, num2 = 60; + cout << "\n------------- Using swap_alternate() method -------------\n" << endl; + cout << "Before Swap, num1 = " << num1 << " , num2 = " << num2 << endl; + swap(num1, num2); + cout << "After Swap, num1 = " << num1 << " , num2 = " << num2 << endl; + + + + return 0; +} + +/* + +Commands to run +---------------- + +g++ -std=c++11 Question17_1.cpp -o Question17_1 +./Question17_1 + +*/ \ No newline at end of file diff --git a/c++/Chapter 17/Question17_3.cpp b/c++/Chapter 17/Question17_3.cpp new file mode 100644 index 00000000..155ebf7f --- /dev/null +++ b/c++/Chapter 17/Question17_3.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +// declarations +int factors_of_five(int); +int count_fact_zeros(int); + +int count_fact_zeros_alternate(int); + +/* Method 1 - Start */ + +int factors_of_five(int x) { + + int zero_count = 0; + while (x%5 == 0) { + zero_count++; + x /= 5; + } + return zero_count; +} + +int count_fact_zeros(int number) { + + if (number < 0) return -1; + int zero_count = 0, i; + for (i=2; i<=number; i++) { + zero_count += factors_of_five(i); + } + return zero_count; +} + +/* Method 1 - End */ + +/* Method 2 - Start */ + +int count_fact_zeros_alternate(int number) { + + if (number < 0) return -1; + int zero_count = 0, i; + + for (i=5; number/i > 0; i=i*5) { + zero_count += number/i; + } + + return zero_count; +} + +/* Method 1 - End */ + +int main() { + + int number1 = 20, number2 = 25; + + cout << "\n------------- Using count_fact_zeros() method -------------\n" << endl; + cout << "Number of trailing zeros in " << number1 << "'s factorial are : " << count_fact_zeros(number1) << endl; + cout << "Number of trailing zeros in " << number2 << "'s factorial are : " << count_fact_zeros(number2) << endl; + cout << endl; + + cout << "\n------------- Using count_fact_zeros_alternate() method -------------\n" << endl; + cout << "Number of trailing zeros in " << number1 << "'s factorial are : " << count_fact_zeros_alternate(number1) << endl; + cout << "Number of trailing zeros in " << number2 << "'s factorial are : " << count_fact_zeros_alternate(number2) << endl; + cout << endl; + + return 0; +} + + +/* + +Commands to run +---------------- + +g++ -std=c++11 Question17_3.cpp -o Question17_3 +./Question17_3 + +*/ diff --git a/c++/Chapter 17/Question17_5.cpp b/c++/Chapter 17/Question17_5.cpp new file mode 100644 index 00000000..263882bf --- /dev/null +++ b/c++/Chapter 17/Question17_5.cpp @@ -0,0 +1,94 @@ +#include +#include + +using namespace std; + +#define MAX_COL 4 + +// creating a structure for returning the results +struct Result { + int hits; + int pseudohits; +}; + +// declarations + +void print(Result*); +Result* estimate(string, string); +int code(char); + +// Function Def + +int code(char ch) { + switch(ch) { + case 'B' : return 0; + case 'G' : return 1; + case 'R' : return 2; + case 'Y' : return 3; + default : return -1; + } +} + +Result* estimate(string guess, string solution) { + + if (guess.size() != solution.size()) return nullptr; + + Result* res = new Result(); + int* freq = new int[MAX_COL]; + + int i; + for (i=0; ihits++; // calculating hits + } + else { + int dig = code(solution[i]); + if (dig >= 0) { + freq[dig]++; // incrementing frequency table if it is not a hit. this i used for pseudo hits + } + } + } + + // calculating psuedohits + for(i=0; i= 0 && freq[dig] > 0 && (guess[i] != solution[i]) ) { + res->pseudohits++; + freq[dig]--; + } + } + + return res; +} + +void print(Result* res) { + cout << "(h = " << res->hits << ", ph = " << res->pseudohits << ")" << endl; +} + +int main() { + + string guess = "GRBY", solution = "BBRY"; + cout << "Solution = " << solution << " Guess = " << guess << endl; + Result* res = estimate(guess, solution); + print(res); + + guess = "RGBY", solution = "GGRR"; + cout << "Solution = " << solution << " Guess = " << guess << endl; + res = estimate(guess, solution); + print(res); + + return 0; +} + +/* + +Commands to run +---------------- + +g++ -std=c++11 Question17_5.cpp -o Question17_5 +./Question17_5 + +*/ + + +