From a805c55e5b870ec3f65ce9dd5fdf6f5456a62ac3 Mon Sep 17 00:00:00 2001 From: Oskar Date: Wed, 7 Aug 2024 16:35:33 +0200 Subject: more exercises --- 3p12.cpp | 27 +++++++++++++++++++++++++++ 3p13.cpp | 37 +++++++++++++++++++++++++++++++++++++ 3p14.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 3p15.cpp | 33 +++++++++++++++++++++++++++++++++ exercise-template.cpp | 2 ++ 5 files changed, 138 insertions(+) create mode 100644 3p12.cpp create mode 100644 3p13.cpp create mode 100644 3p14.cpp create mode 100644 3p15.cpp diff --git a/3p12.cpp b/3p12.cpp new file mode 100644 index 0000000..ef46169 --- /dev/null +++ b/3p12.cpp @@ -0,0 +1,27 @@ +#include +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.12 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; +int main () { + + vector> ivec; // A vector of vectors of int + //vector svec = ivec; // Not valid, you can not initialize a vector of strings to with a vector of int. + vector svec(10, "null"); // A vector of 10 objects of type string all initialized to "null" + + return 0; +} diff --git a/3p13.cpp b/3p13.cpp new file mode 100644 index 0000000..ca60b58 --- /dev/null +++ b/3p13.cpp @@ -0,0 +1,37 @@ +#include +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.13 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; +int main () { + + vector v1; // 0 elements? + vector v2(10); // 10 elements, default initialized + vector v3(10, 42); // 10 elements, all initialized to 42 + vector v4{10}; // 1 element, initialized to 10. + vector v5{10, 42}; // 2 elements, first one 10, second one 42 + vector v6{10}; // Not really valid but it creates 10 elements of default initialized strings. Empty strings. + vector v7{10, "hi"}; // Not really valid but creates 10 elements that says "hi" + + // some extra testing below + vector v8(10); // same as v6? + vector v9(10, "hi"); // Same as v7? + + cout << v7[1] << endl; + cout << v9[1] << endl; + return 0; +} diff --git a/3p14.cpp b/3p14.cpp new file mode 100644 index 0000000..b11a9ff --- /dev/null +++ b/3p14.cpp @@ -0,0 +1,39 @@ +#include +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.14 + * + * Not exactly according to instructions but still learned from making this. + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; +int main () { + + int n; + vector vn; + if(cin >> n) { + vn.push_back(n); + while(cin >> n){ + vn.push_back(n); + } + + } else { + return -1; + } + + for(auto vni : vn) { + cout << vni << endl; + } + + return 0; +} diff --git a/3p15.cpp b/3p15.cpp new file mode 100644 index 0000000..27a648d --- /dev/null +++ b/3p15.cpp @@ -0,0 +1,33 @@ +#include +#include +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.15 + * + * Not completely according to instructions but thats okay. I still learned creating this. + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; +int main () { + + string phrase; + vector vp; + while(getline(cin, phrase)) { + vp.push_back(phrase); + } + + for(auto vpi : vp) { + cout << vpi << endl; + } + + return 0; +} diff --git a/exercise-template.cpp b/exercise-template.cpp index 38099b7..8986133 100644 --- a/exercise-template.cpp +++ b/exercise-template.cpp @@ -1,4 +1,5 @@ #include +#include #include "sales_data.hpp" #include "sales_item.hpp" @@ -15,6 +16,7 @@ using std::cin; using std::cerr; using std::clog; using std::endl; +using std::vector; int main () { -- cgit v1.2.3