Functions
This page is under construction. Please come back later.
#include <clocale> #include <codecvt> #include <iostream> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; void printHello() noexcept { wcout << L"Hello, world!" << endl; } int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); printHello(); return 0; }
Output
$ g++ -std=c++17 Functions1.cpp -o Functions1 -lfmt
$ ./Functions1
Hello, world!
#include <clocale> #include <codecvt> #include <iostream> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; void printHello(wstring name) noexcept { wcout << L"Hello, " << name << L"!" << endl; } int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); printHello(L"Fred"); printHello(L"Wilma"); printHello(L"Barney"); printHello(L"Betty"); return 0; }
Output
$ g++ -std=c++17 Functions2.cpp -o Functions2 -lfmt
$ ./Functions2
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
#include <clocale> #include <codecvt> #include <fmt/format.h> #include <fmt/xchar.h> #include <iostream> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; int squared(int j) noexcept { return j * j; } int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); for (int i = 1; i <= 10; ++i) { wcout << fmt::format(L"{0:d}^2 = {1:d}", i, squared(i)) << endl; } return 0; }
Output
$ g++ -std=c++17 Functions3.cpp -o Functions3 -lfmt
$ ./Functions3
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
6^2 = 36
7^2 = 49
8^2 = 64
9^2 = 81
10^2 = 100
#include <clocale> #include <codecvt> #include <fmt/format.h> #include <fmt/xchar.h> #include <iostream> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; double exponentiation(double base, int powerArg) noexcept { int power = powerArg; double result = 1; while (power > 0) { result *= base; --power; } return result; } int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); for (double b = 1.1; b < 2.05; b += 0.1) { for (int p = 2; p <= 10; ++p) { wcout << fmt::format(L"{0:f} ^ {1:d} = {2:f}", b, p, exponentiation(b, p)) << endl; } } return 0; }
Output
$ g++ -std=c++17 Functions4.cpp -o Functions4 -lfmt
$ ./Functions4
1.100000 ^ 2 = 1.210000
1.100000 ^ 3 = 1.331000
1.100000 ^ 4 = 1.464100
1.100000 ^ 5 = 1.610510
1.100000 ^ 6 = 1.771561
1.100000 ^ 7 = 1.948717
1.100000 ^ 8 = 2.143589
1.100000 ^ 9 = 2.357948
1.100000 ^ 10 = 2.593742
1.200000 ^ 2 = 1.440000
1.200000 ^ 3 = 1.728000
1.200000 ^ 4 = 2.073600
1.200000 ^ 5 = 2.488320
1.200000 ^ 6 = 2.985984
1.200000 ^ 7 = 3.583181
1.200000 ^ 8 = 4.299817
1.200000 ^ 9 = 5.159780
1.200000 ^ 10 = 6.191736
1.300000 ^ 2 = 1.690000
1.300000 ^ 3 = 2.197000
1.300000 ^ 4 = 2.856100
1.300000 ^ 5 = 3.712930
1.300000 ^ 6 = 4.826809
1.300000 ^ 7 = 6.274852
1.300000 ^ 8 = 8.157307
1.300000 ^ 9 = 10.604499
1.300000 ^ 10 = 13.785849
1.400000 ^ 2 = 1.960000
1.400000 ^ 3 = 2.744000
1.400000 ^ 4 = 3.841600
1.400000 ^ 5 = 5.378240
1.400000 ^ 6 = 7.529536
1.400000 ^ 7 = 10.541350
1.400000 ^ 8 = 14.757891
1.400000 ^ 9 = 20.661047
1.400000 ^ 10 = 28.925465
1.500000 ^ 2 = 2.250000
1.500000 ^ 3 = 3.375000
1.500000 ^ 4 = 5.062500
1.500000 ^ 5 = 7.593750
1.500000 ^ 6 = 11.390625
1.500000 ^ 7 = 17.085938
1.500000 ^ 8 = 25.628906
1.500000 ^ 9 = 38.443359
1.500000 ^ 10 = 57.665039
1.600000 ^ 2 = 2.560000
1.600000 ^ 3 = 4.096000
1.600000 ^ 4 = 6.553600
1.600000 ^ 5 = 10.485760
1.600000 ^ 6 = 16.777216
1.600000 ^ 7 = 26.843546
1.600000 ^ 8 = 42.949673
1.600000 ^ 9 = 68.719477
1.600000 ^ 10 = 109.951163
1.700000 ^ 2 = 2.890000
1.700000 ^ 3 = 4.913000
1.700000 ^ 4 = 8.352100
1.700000 ^ 5 = 14.198570
1.700000 ^ 6 = 24.137569
1.700000 ^ 7 = 41.033867
1.700000 ^ 8 = 69.757574
1.700000 ^ 9 = 118.587876
1.700000 ^ 10 = 201.599390
1.800000 ^ 2 = 3.240000
1.800000 ^ 3 = 5.832000
1.800000 ^ 4 = 10.497600
1.800000 ^ 5 = 18.895680
1.800000 ^ 6 = 34.012224
1.800000 ^ 7 = 61.222003
1.800000 ^ 8 = 110.199606
1.800000 ^ 9 = 198.359290
1.800000 ^ 10 = 357.046723
1.900000 ^ 2 = 3.610000
1.900000 ^ 3 = 6.859000
1.900000 ^ 4 = 13.032100
1.900000 ^ 5 = 24.760990
1.900000 ^ 6 = 47.045881
1.900000 ^ 7 = 89.387174
1.900000 ^ 8 = 169.835630
1.900000 ^ 9 = 322.687698
1.900000 ^ 10 = 613.106626
2.000000 ^ 2 = 4.000000
2.000000 ^ 3 = 8.000000
2.000000 ^ 4 = 16.000000
2.000000 ^ 5 = 32.000000
2.000000 ^ 6 = 64.000000
2.000000 ^ 7 = 128.000000
2.000000 ^ 8 = 256.000000
2.000000 ^ 9 = 512.000000
2.000000 ^ 10 = 1024.000000
#include <clocale> #include <codecvt> #include <fmt/format.h> #include <fmt/xchar.h> #include <iostream> #include <string> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; long factorial(int x) noexcept { if (x <= 1) { return 1; } return x * factorial(x - 1); } int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); for (int x = 1; x < 10; ++x) { wcout << fmt::format(L"{0:d}! = {1:d}", x, factorial(x)) << endl; } return 0; }
Output
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/cpp/examples/output/Functions5.out
#include "Utils.hpp" #include <iostream> #include <string> #include <vector> using namespace std; vector<string> reverseList(const vector<string> &x) noexcept { vector<string> y = {}; for (int i = x.size() - 1; i >= 0; --i) { Utils::push(y, x[i]); } return y; } int main(int argc, char **argv) { vector<string> names = {"Fred", "Wilma", "Barney", "Betty"}; for (auto name : names) { cout << "Hello, " << name << "!" << endl; } names = reverseList(names); for (auto name : names) { cout << "Hello, " << name << "!" << endl; } cout << Utils::to_string(names) << endl; return 0; }
Output
$ g++ -std=c++17 Lists6.cpp -o Lists6 -lfmt
$ ./Lists6
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
Hello, Betty!
Hello, Barney!
Hello, Wilma!
Hello, Fred!
["Betty", "Barney", "Wilma", "Fred"]
#include "Utils.hpp" #include <iostream> #include <string> #include <unordered_map> using namespace std; unordered_map<string, int> convertKMtoMiles(const unordered_map<string, int> &x) noexcept { unordered_map<string, int> y = {}; for (auto planet : Utils::keys(x)) { y[planet] = int(x.at(planet) * 0.621371 + 0.5); } return y; } int main(int argc, char **argv) { unordered_map<string, int> planetDiametersInKM = { {"Mercury", 4879}, {"Venus", 12103}, {"Earth", 12756}, {"Mars", 6794}, {"Jupiter", 142985}, {"Saturn", 120534}, {"Uranus", 51115}, {"Neptune", 49534}, {"Pluto", 2374}, {"Ceres", 946}, {"Eris", 2326}, {"Makemake", 1430} }; unordered_map<string, int> planetDiametersInMiles = convertKMtoMiles(planetDiametersInKM); for (auto planet : Utils::keys(planetDiametersInMiles)) { cout << planet << " has a diameter of " << planetDiametersInMiles.at(planet) << " miles" << endl; } return 0; }
Output
$ g++ -std=c++17 Maps3.cpp -o Maps3 -lfmt
$ ./Maps3
Ceres has a diameter of 588 miles
Earth has a diameter of 7926 miles
Eris has a diameter of 1445 miles
Jupiter has a diameter of 88847 miles
Makemake has a diameter of 889 miles
Mars has a diameter of 4222 miles
Mercury has a diameter of 3032 miles
Neptune has a diameter of 30779 miles
Pluto has a diameter of 1475 miles
Saturn has a diameter of 74896 miles
Uranus has a diameter of 31761 miles
Venus has a diameter of 7520 miles
#include "Utils.hpp" #include <iostream> #include <string> #include <tuple> using namespace std; tuple<int, string> swap(const tuple<string, int> &x) noexcept { tuple<int, string> y; y = make_tuple(std::get<1>(x), std::get<0>(x)); return y; } int main(int argc, char **argv) { tuple<string, int> pair1 = make_tuple("Hello", 5); tuple<int, string> pair2; pair2 = swap(pair1); cout << Utils::to_string(pair1) << " becomes " << Utils::to_string(pair2) << endl; return 0; }
Output
$ g++ -std=c++17 Tuples3.cpp -o Tuples3 -lfmt
$ ./Tuples3
<"Hello", 5> becomes <5, "Hello">
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Alouette Lyrics
- Bingo Lyrics
- 99 Bottles of Beer Lyrics
- Bubble Sort
- Calculate Euler's Number
- Compute π Function
- Compute π (Monte Carlo with Central Limit Theorem)
- Credit Card Validator
- The Farmer in the Dell (Revisited)
- Fibonacci Numbers
- Football Passer Rating (Revisited)
- GCD
- Insertion Sort
- LCG Pseudo-Random Number Generator
- License Plate Generator (Revisited)
- Merge Sort
- Min/Max Functions
- Old MacDonald Lyrics
- Quicksort
- Renard Numbers with Rounding Function
- Roman Numerals
- Sign Function
- Spreadsheet Columns
- Tower of Hanoi
- Twelve Days of Christmas Lyrics
References
-
[[C++ Programming Language]], 4th Edition, Bjarne Stroustrup, Addison-Wesley, 2013, ISBN 978-0321563842.
- [[C++ Language Reference]]
- [[cplusplus.com]]
- [[Cprogramming.com]]