-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added c++ solutions for Chapter 17 for ques 1, 3 and 5
- Loading branch information
Showing
3 changed files
with
219 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,48 @@ | ||
#include <iostream> | ||
|
||
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 | ||
*/ |
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,77 @@ | ||
#include <iostream> | ||
|
||
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 | ||
*/ |
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,94 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
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; i<guess.size(); i++) { | ||
if (guess[i] == solution[i]) { | ||
res->hits++; // 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<guess.size(); i++) { | ||
int dig = code(guess[i]); | ||
if (dig >= 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 | ||
*/ | ||
|
||
|
||
|