In this tutorial we continue solving problems with C++. We’ll solve the Caesar Cipher, Solve Equations for X, Create Lists of Primes and Generate a List of Random Values in a Range. I’m making these videos to make you good at solving random problems rather then just covering language syntax. Don’t worry if you get them wrong. The goal is to make you think in new ways.

All of the code and a transcript of the video follows the video below.

If you like videos like this consider donating $1, or simply help by turning of Ad Blocking software. Either allows me to continue making free tutorials for everyone.

**Cheat Cheat / Transcript**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
// ----- 7. PROBLEM CAESAR CIPHER ----- std::string CaesarCipher(std::string theString, int key, bool encrypt); int main() { std::string theString = "Make me secret"; std::string encryptedStr = CaesarCipher(theString, 5, true); std::string decryptedString = CaesarCipher(encryptedStr, 5, false); std::cout << "Encrypted " << encryptedStr << "\n"; std::cout << "Decrypted " << decryptedString << "\n"; return 0; } std::string CaesarCipher(std::string theString, int key, bool encrypt){ std::string returnString = ""; int charCode = 0; char letter; // The key will shift and unshift character codes if(encrypt) key = key * -1; // Cycle through each character for(char& c : theString) { // Check if it's a letter and if not don't chage it if(isalpha(c)){ // Convert from char to int and shift the char code charCode = (int)c; charCode += key; // If uppercase check if the new character code > // char code for Z, or < char code for A // If so shift the character code so that its value // wraps back into the letter character codes if(isupper(c)){ if(charCode > (int)'Z'){ charCode -= 26; } else if(charCode < (int)'A'){ charCode += 26; } } else { // Do the same for lowercase letters if(charCode > (int)'z'){ charCode -= 26; } else if(charCode < (int)'a'){ charCode += 26; } } // Convert from int to char and add the returning string letter = charCode; returnString += letter; } else { letter = c; returnString += c; std::cout << c << "\n"; } } return returnString; } // ----- END PROBLEM CAESAR CIPHER ----- #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <numeric> #include <sstream> // ----- 1. PROBLEM : SOLVE FOR X ----- std::vector<std::string> StringToVector(std::string theString, char separator); void SolveForX(std::string equation); int main() { // Make a function that receives an algebraic // equation like x + 4 = 9 and solve for x // x will always be the 1st value received // and you only will deal with addition std::cout << "Enter an equation to solve "; std::string equation = ""; getline(std::cin, equation); SolveForX(equation); return 0; } std::vector<std::string> StringToVector(std::string theString, char separator){ std::vector<std::string> vecsWords; std::stringstream ss(theString); std::string sIndivStr; while(getline(ss, sIndivStr, separator)){ vecsWords.push_back(sIndivStr); } return vecsWords; } void SolveForX(std::string equation){ std::vector<std::string> vecEquation = StringToVector(equation, ' '); // Convert strings to int int num1 = std::stoi(vecEquation[2]); int num2 = std::stoi(vecEquation[4]); int xVal = num2 - num1; std::cout << "x = " << xVal << "\n"; } // ----- 1. END PROBLEM : SOLVE FOR X ----- // ----- 2. PROBLEM : RETURN A LIST OF PRIMES ----- std::vector<int> Range(int start, int max, int step); bool IsPrime(int num); std::vector<int> GetPrimes(int maxNum); int main() { int num = 0; std::cout << "Number to Check : "; std::cin >> num; // Shows bool values as true or false std::cout.setf(std::ios::boolalpha); std::cout << "Is " << num << " Prime " << IsPrime(num) << "\n"; std::cout << "Generate Primes up to "; int maxPrime; std::cin >> maxPrime; std::vector<int> primeList = GetPrimes(maxPrime); for(auto x: primeList) std::cout << x << "\n"; return 0; } std::vector<int> Range(int start, int max, int step){ int i = start; std::vector<int> range; while(i <= max){ range.push_back(i); i += step; } return range; } bool IsPrime(int num){ for(auto n: Range(2, num - 1, 1)){ if((num % n) == 0) return false; } return true; } std::vector<int> GetPrimes(int maxNum){ std::vector<int> vecPrimes; for(auto x: Range(2, maxNum, 1)){ if(IsPrime(x)) vecPrimes.push_back(x); } return vecPrimes; } // ----- 2. END PROBLEM : RETURN A LIST OF PRIMES ----- // ----- 3. PROBLEM : GENERATE A RANDOM VECTOR OF NUMBERS ----- #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <numeric> #include <sstream> // Needed for random number generation #include <ctime> std::vector<int> GenerateRandVec(int numOfNums, int min, int max); int main() { std::vector<int> vecVals = GenerateRandVec(10, 5, 50); for(auto x: vecVals) std::cout << x << "\n"; return 0; } std::vector<int> GenerateRandVec(int numOfNums, int min, int max){ std::vector<int> vecValues; // Seed the random number generator srand(time(NULL)); int i = 0, randVal = 0; while(i < numOfNums){ // The min added on increases result based on min // The remainder of modulus can't be greater // then the divisor. We have to adjust down // based on the addition from min randVal = min + std::rand() % ((max + 1) - min); vecValues.push_back(randVal); i++; } return vecValues; } |

## Leave a Reply