summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-08-07 22:39:51 +0200
committerOskar <[email protected]>2024-08-07 22:39:51 +0200
commit9b931cfed1551961c8bbb93b21051104c0c4c716 (patch)
tree9232cc1246d35db36b0800e3d4af318f4e3905c9
parent601e9571981ef44984e740ba71ce1648dbc223d3 (diff)
more exercises
-rw-r--r--3p16.cpp65
-rw-r--r--3p17.cpp48
-rw-r--r--3p18.cpp29
-rw-r--r--3p19.cpp38
-rw-r--r--3p20.cpp48
-rw-r--r--vector-index-example.cpp32
6 files changed, 260 insertions, 0 deletions
diff --git a/3p16.cpp b/3p16.cpp
new file mode 100644
index 0000000..0f1ff0b
--- /dev/null
+++ b/3p16.cpp
@@ -0,0 +1,65 @@
+#include <iostream>
+#include <vector>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.16
+ *
+ *
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+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 << v1.size() << " <v1 size" << endl;
+ for(auto v1i : v1) { cout << "'" << v1i << "'"; }
+ cout << " <v1 contents" << endl;
+
+ cout << v2.size() << " <v2 size" << endl;
+ for(auto v2i : v2) { cout << "'" << v2i << "'"; }
+ cout << " <v2 contents" << endl;
+
+ cout << v3.size() << " <v3 size" << endl;
+ for(auto v3i : v3) { cout << "'" << v3i << "'"; }
+ cout << " <v3 contents" << endl;
+
+ cout << v4.size() << " <v4 size" << endl;
+ for(auto v4i : v4) { cout << "'" << v4i << "'"; }
+ cout << " <v4 contents" << endl;
+
+ cout << v5.size() << " <v5 size" << endl;
+ for(auto v5i : v5) { cout << "'" << v5i << "'"; }
+ cout << " <v5 contents" << endl;
+
+ cout << v6.size() << " <v6 size" << endl;
+ for(auto v6i : v6) { cout << "'" << v6i << "'"; }
+ cout << " <v6 contents" << endl;
+
+ cout << v7.size() << " <v7 size" << endl;
+ for(auto v7i : v7) { cout << "'" << v7i << "'"; }
+ cout << " <v7 contents" << endl;
+
+ // some extra testing below
+ //vector<string> v8(10); // same as v6?
+ //vector<string> v9(10, "hi"); // Same as v7?
+ //cout << v7[1] << endl;
+ //cout << v9[1] << endl;
+
+ return 0;
+}
diff --git a/3p17.cpp b/3p17.cpp
new file mode 100644
index 0000000..a2195c7
--- /dev/null
+++ b/3p17.cpp
@@ -0,0 +1,48 @@
+#include <iostream>
+#include <vector>
+#include <cctype>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.17
+ *
+ *
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+int main () {
+
+ string word;
+ vector<string> v_words;
+ while(cin >> word) {
+ v_words.push_back(word);
+ }
+
+ for(auto &i : v_words) {
+ for(auto &c : i) {
+ c = toupper(c);
+ }
+ }
+
+ decltype(v_words.size()) cnt = 0;
+ for(auto vww : v_words) {
+ if(cnt >= 8) {
+ cout << endl;
+ cnt = 1;
+ } else {
+ ++cnt;
+ }
+ cout << vww << " ";
+ }
+
+ cout << endl;
+ return 0;
+}
diff --git a/3p18.cpp b/3p18.cpp
new file mode 100644
index 0000000..a35ff43
--- /dev/null
+++ b/3p18.cpp
@@ -0,0 +1,29 @@
+#include <iostream>
+#include <vector>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.18
+ *
+ *
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+int main () {
+
+ vector<int> ivec1;
+ vector<int> ivec2(10); // We could initialize it with some elements
+ vector<int> ivec3{42}; // We could initialize it with the first element being 42
+ ivec1.push_back(42); // One way we might fix it
+ ivec2[0] = 42;
+ cout << ivec1[0] << " " << ivec2[0] << " " << ivec3[0] << endl;
+ return 0;
+}
diff --git a/3p19.cpp b/3p19.cpp
new file mode 100644
index 0000000..1d659c9
--- /dev/null
+++ b/3p19.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include <vector>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.19
+ *
+ *
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+int main () {
+
+ vector<int> ivec1(10, 42);
+ vector<int> ivec2{42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
+ vector<int> ivec3;
+ for(decltype(ivec3.size()) i = 0 ; i < 10 ; i++) {
+ ivec3.push_back(42);
+ }
+
+ vector<vector<int>> vecivec = {ivec1, ivec2, ivec3};
+ for(auto &vv : vecivec) {
+ for(auto &iv : vv) {
+ cout << iv << " ";
+ }
+ cout << endl;
+ }
+
+ return 0;
+}
diff --git a/3p20.cpp b/3p20.cpp
new file mode 100644
index 0000000..0a84561
--- /dev/null
+++ b/3p20.cpp
@@ -0,0 +1,48 @@
+#include <iostream>
+#include <vector>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.20
+ *
+ *
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+int main () {
+
+ vector<int> VecNumbers;
+ int Numbers = 0;
+ while(cin >> Numbers) {
+ VecNumbers.push_back(Numbers);
+ }
+
+ for(decltype(VecNumbers.size()) Index = 0 ; Index < VecNumbers.size()-1 ; Index += 2) { // VecNumbers.size() -1 in case the input
+ cout << VecNumbers[Index] + VecNumbers[Index + 1] << " "; // numbers are odd. It doesnt calculate anything
+ } // if it's odd
+
+ cout << endl;
+ auto MaxSize = VecNumbers.size()-1;
+ decltype(VecNumbers.size()) Index = 0;
+ while(Index <= MaxSize) {
+ if (Index == MaxSize) {
+ cout << VecNumbers[Index] + VecNumbers[MaxSize] << " ";
+ break;
+ }
+
+ cout << VecNumbers[Index] + VecNumbers[MaxSize] << " ";
+ --MaxSize;
+ ++Index;
+ }
+
+ cout << endl;
+ return 0;
+}
diff --git a/vector-index-example.cpp b/vector-index-example.cpp
new file mode 100644
index 0000000..f6a0e43
--- /dev/null
+++ b/vector-index-example.cpp
@@ -0,0 +1,32 @@
+#include <iostream>
+#include <vector>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * Description
+ *
+ *
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+using std::vector;
+int main () {
+
+ vector<unsigned> scores(11, 0); // 11 buckets, all initially 0
+ unsigned grade;
+ while (cin >> grade) {
+ if (grade <= 100) {
+ ++scores[grade/10];
+ // scores[grade/10] += 1; // basically equivalent
+ }
+ }
+
+ return 0;
+}