summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-08-09 12:46:41 +0200
committerOskar <[email protected]>2024-08-09 12:46:41 +0200
commit97df48a5d420072c1ed487b7d5306d1665e692db (patch)
tree9fd129a1b108828d02cf4baa6cbeef54be371afe
parent9b931cfed1551961c8bbb93b21051104c0c4c716 (diff)
more exercises
-rw-r--r--3p21.cpp56
-rw-r--r--3p22.cpp42
-rw-r--r--3p23.cpp47
-rw-r--r--exercise-template.cpp2
-rw-r--r--iterator-test.cpp75
5 files changed, 220 insertions, 2 deletions
diff --git a/3p21.cpp b/3p21.cpp
new file mode 100644
index 0000000..f1bad84
--- /dev/null
+++ b/3p21.cpp
@@ -0,0 +1,56 @@
+#include <iostream>
+#include <vector>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.21
+ *
+ *
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+
+void cout_vector_int_iterator(vector<int> vx) {
+
+ for(auto itr = vx.cbegin() ; itr != vx.cend() ; itr++) {
+ cout << "'" << *itr << "'";
+ }
+
+ cout << endl;
+}
+
+void cout_vector_string_iterator(vector<string> vxs) {
+
+ for(auto itr = vxs.cbegin() ; itr != vxs.cend() ; itr++) {
+ cout << "'" << *itr << "'";
+ }
+
+ cout << endl;
+}
+
+int main () {
+
+ vector<int> v1; // 0 elements?
+ vector<int> v2(10); // 10 elements, default initialized
+ vector<int> v3(10, 42); // 10 elements, all initialized to 42
+ vector<int> v4{10}; // 1 element, initialized to 10.
+ vector<int> v5{10, 42}; // 2 elements, first one 10, second one 42
+ vector<string> v6{10}; // Not really valid but it creates 10 elements of default initialized strings. Empty strings.
+ vector<string> v7{10, "hi"}; // Not really valid but creates 10 elements that says "hi"
+ cout_vector_int_iterator(v1);
+ cout_vector_int_iterator(v2);
+ cout_vector_int_iterator(v3);
+ cout_vector_int_iterator(v4);
+ cout_vector_int_iterator(v5);
+ cout_vector_string_iterator(v6);
+ cout_vector_string_iterator(v7);
+ return 0;
+}
diff --git a/3p22.cpp b/3p22.cpp
new file mode 100644
index 0000000..488d85d
--- /dev/null
+++ b/3p22.cpp
@@ -0,0 +1,42 @@
+#include <iostream>
+#include <vector>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.22
+ *
+ * I'll be honest. I actually have no idea what i was supposed to do in this exercise.
+ * So i just made it so the program toupper's all the strings until a blank string is encountered
+ * Everything after the blank string is not processed or printed.
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+
+int main () {
+
+ vector<string> text;
+ string s1;
+ while(getline(cin, s1)) {
+ text.push_back(s1);
+ }
+
+ for(auto vi = text.begin() ; vi != text.end() && !vi->empty() ; vi++) {
+ for(auto &cc : *vi) {
+ cc = toupper(cc);
+ }
+ }
+
+ for (auto it = text.cbegin() ; it != text.cend() && !it->empty() ; ++it) {
+ cout << *it << endl;
+ }
+
+ return 0;
+}
diff --git a/3p23.cpp b/3p23.cpp
new file mode 100644
index 0000000..1456495
--- /dev/null
+++ b/3p23.cpp
@@ -0,0 +1,47 @@
+#include <iostream>
+#include <vector>
+#include <ctime>
+#include <cstdlib>
+#include <random>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.23
+ *
+ * I added a random number generator with some help from the internet.
+ * That way i could have some random numbers which i think makes the
+ * exercise a little more interesting :)
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+int main () {
+
+ int random_value = 0;
+ vector<int> ff;
+ decltype(ff.size()) amount = 10;
+ std::random_device rd;
+ std::mt19937 gen(rd());
+ std::uniform_int_distribution<> distr(1, 10000);
+ for(decltype(ff.size()) i = 0 ; i != amount; i++) {
+ random_value = distr(gen);
+ ff.push_back(random_value);
+ cout << ff[i] << "\n";
+ }
+
+ cout << endl;
+ for(auto ffit = ff.begin() ; ffit != ff.end() ; ffit++) {
+ *ffit *= 2;
+ cout << *ffit << "\n";
+ }
+
+ cout << endl;
+ return 0;
+}
diff --git a/exercise-template.cpp b/exercise-template.cpp
index 8986133..9c7d438 100644
--- a/exercise-template.cpp
+++ b/exercise-template.cpp
@@ -18,7 +18,5 @@ using std::clog;
using std::endl;
using std::vector;
int main () {
-
-
return 0;
}
diff --git a/iterator-test.cpp b/iterator-test.cpp
new file mode 100644
index 0000000..db86446
--- /dev/null
+++ b/iterator-test.cpp
@@ -0,0 +1,75 @@
+#include <iostream>
+#include <vector>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * Needed to test this program because i was
+ * confused about the condition in the for loop.
+ *
+ * Also some notes to get some things straight in my head
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+int main () {
+
+ string s = "hello";
+ for (auto it = s.begin(); it != s.end() && !isspace(*it); ++it) {
+ *it = toupper(*it); // capitalize the current character
+ }
+ // BOTH need to be true for the loop to continue
+ cout << s << endl;
+
+ // print each line in text up to the first blank line
+ vector<string> text;
+ string s1 = "hello!";
+ string s2 = "hello vro";
+ string s3 = "ffffff";
+ string s4;
+ string s5 = "yo";
+ text.push_back(s1);
+ text.push_back(s2);
+ text.push_back(s3);
+ text.push_back(s4);
+ text.push_back(s5);
+ for (auto it = text.cbegin() ; // Get the beginning element
+ it != text.cend() && !it->empty() ; ++it) { /* loop stops when we hit the end or if theres an empty string.
+ we do this by accessing the method empty() through the arrow operator
+ */
+ cout << *it << endl;
+ cout << it->at(0) << endl; // To access the individual characters in the strings in the vector we use at() method
+ cout << (*it)[0] << endl; // We can also do it this way.
+ }
+
+ /*
+ Example for myself:
+ we try to access method in dereferenced s1.empty()
+ but because we are dereferencing and accessing method at the same time we have a special operator
+ -> dereference while also accessing a member
+ */
+
+ /*
+ 'it' is a const iterator which is (kind of) a pointer? Kinda?
+ It points towards the first element (cbegin()). Which is a string.
+ But we want to use methods from the string class. So we have to use the
+ arrow operator to access that method.
+
+ I've used C before so i am a bit familiar with using the arrow operator
+ in structs. Though i will say that this does feel a bit different because
+ classes in this language dont just have data types in the structs but also
+ methods so yeah i guess it makes sense that we use the -> arrow operator
+ to access those methods. If we were to access a method but directly on a string
+ (not through a pointer / iterator) then yeah we would be using the . operator.
+
+ So i guess this all makes sense. I just had to write this text to get everything
+ clear in my head
+ */
+ return 0;
+}