P.2 โ Building skills (chapters 5โ8)
Loops, functions and lists. These take a bit longer โ that's the point!
Problems#
Print a countdown from 10 to 1, then Liftoff! ๐.
A for loop can count down: start at 10, keep going while i >= 1, and use --i.
#include <iostream> int main(){ for (int i { 10 }; i >= 1; --i) { std::cout << i << "...\n"; } std::cout << "Liftoff! ๐\n"; return 0;}Write a function bool isPrime(int n). A prime number is bigger than 1 and can only be divided by 1 and itself. Print all primes from 1 to 50.
Try dividing n by every number from 2 up. If any divides evenly (n % d == 0), it's not prime. You only need to check up to d * d <= n.
#include <iostream> bool isPrime(int n){ if (n < 2) return false; for (int d { 2 }; d * d <= n; ++d) { if (n % d == 0) return false; } return true;} int main(){ for (int n { 1 }; n <= 50; ++n) { if (isPrime(n)) std::cout << n << ' '; } std::cout << '\n'; return 0;}Write bool isPalindrome(const std::string& word). A palindrome reads the same backwards, like racecar or level.
Compare the first character with the last, the second with the second-last, and so on. Stop in the middle.
#include <iostream>#include <string> bool isPalindrome(const std::string& word){ if (word.empty()) return true; std::size_t left { 0 }; std::size_t right { word.length() - 1 }; while (left < right) { if (word[left] != word[right]) return false; ++left; --right; } return true;} int main(){ std::cout << std::boolalpha; std::cout << isPalindrome("racecar") << '\n'; // true std::cout << isPalindrome("pizza") << '\n'; // false return 0;}Put these scores in a std::vector<int>: 72, 95, 88, 61, 79, 93. Print the lowest, highest and average score.
Loop through once. Keep a running total, plus the smallest and biggest seen so far (start both at the first score).
#include <iostream>#include <vector> int main(){ std::vector<int> scores { 72, 95, 88, 61, 79, 93 }; int lowest { scores[0] }; int highest { scores[0] }; int total { 0 }; for (int s : scores) { if (s < lowest) lowest = s; if (s > highest) highest = s; total += s; } std::cout << "Lowest: " << lowest << '\n'; std::cout << "Highest: " << highest << '\n'; std::cout << "Average: " << static_cast<double>(total) / scores.size() << '\n'; return 0;}Count how many times each letter appears in a sentence, ignoring upper/lower case and anything that isn't a letter. Print the counts in alphabetical order.
Use a std::map<char, int>. For each character, skip it if std::isalpha is false, otherwise do ++counts[std::tolower(c)]. A map keeps its keys sorted for you.
#include <cctype>#include <iostream>#include <map>#include <string> int main(){ std::string text { "Hello, World! C++ is fun." }; std::map<char, int> counts {}; for (char c : text) { unsigned char uc { static_cast<unsigned char>(c) }; if (std::isalpha(uc)) ++counts[static_cast<char>(std::tolower(uc))]; } for (const auto& [letter, count] : counts) std::cout << letter << ": " << count << '\n'; return 0;}Tip: press โ / โ to move between lessons, / to search.