summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-08-05 20:57:13 +0200
committerOskar <[email protected]>2024-08-05 20:57:13 +0200
commit4483699cc8cc937cb27040cfbd809b8a90029391 (patch)
tree6edbe3e65d3c922c535078161b35d0892e6d34ec
parent1d105b91b9678ffaa8eddea2218ed1789f5be27c (diff)
more exercises
-rw-r--r--3p5.cpp5
-rw-r--r--3p6-v2.cpp33
-rw-r--r--3p6.cpp28
-rw-r--r--3p7.cpp30
-rw-r--r--3p8-v1.cpp32
-rw-r--r--3p8-v2.cpp33
-rw-r--r--3p9.cpp25
7 files changed, 183 insertions, 3 deletions
diff --git a/3p5.cpp b/3p5.cpp
index 7f1be44..628557d 100644
--- a/3p5.cpp
+++ b/3p5.cpp
@@ -4,7 +4,7 @@
/*
*
- * Description
+ * 3.5
*
*
*/
@@ -19,7 +19,6 @@ int main () {
string full;
string full_sep;
- //string whitespace(" ");
string DD;
while(cin >> DD) {
full += DD;
@@ -27,6 +26,6 @@ int main () {
}
cout << full << endl;
cout << full_sep << endl;
-
+
return 0;
}
diff --git a/3p6-v2.cpp b/3p6-v2.cpp
new file mode 100644
index 0000000..31a8cef
--- /dev/null
+++ b/3p6-v2.cpp
@@ -0,0 +1,33 @@
+#include <iostream>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.6 v1
+ *
+ * Stupid pointer version of this exercise because i wanted to.
+ * Though i will say, i definitely say we should be using the range for. So much simpler.
+ * This version is also way too hard to read especially compared to the other characters.
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+int main () {
+
+ string Xer;
+ cout << "Enter a string" << endl;
+ cin >> Xer;
+ decltype(&Xer[0]) Character; // make pointer
+ for(decltype(Xer.size()) Index = 0 ; Index != Xer.size() ; Index++ ) { // When index is same as Xer.size() we stop the loop
+ Character = &Xer[Index]; // Point towards address of character in index
+ *Character = 'X'; // replace it with X
+ }
+
+ cout << Xer << endl;
+ return 0;
+}
diff --git a/3p6.cpp b/3p6.cpp
new file mode 100644
index 0000000..d33055a
--- /dev/null
+++ b/3p6.cpp
@@ -0,0 +1,28 @@
+#include <iostream>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.6 v1
+ *
+ *
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+int main () {
+
+ string Xer;
+ cout << "Enter a string" << endl;
+ cin >> Xer;
+ for(auto &c : Xer) { // Make reference for every character in Xer
+ c = 'X';
+ }
+ cout << Xer << endl;
+ return 0;
+}
diff --git a/3p7.cpp b/3p7.cpp
new file mode 100644
index 0000000..9acc8fb
--- /dev/null
+++ b/3p7.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.7
+ *
+ * Nothing different happens, pretty much same result. Though auto might just be better so we are sure what type it is.
+ * Unless the author meant that we were supposed to do char rather than (char&). If we were to just do 'char' type then
+ * we wouln't be able to modify the characters in the string, therefore the string remains unchanged.
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+int main () {
+
+ string Xer;
+ cout << "Enter a string" << endl;
+ cin >> Xer;
+ for(char &c : Xer) { // Make reference for every character in Xer
+ c = 'X';
+ }
+ cout << Xer << endl;
+ return 0;
+}
diff --git a/3p8-v1.cpp b/3p8-v1.cpp
new file mode 100644
index 0000000..a53a408
--- /dev/null
+++ b/3p8-v1.cpp
@@ -0,0 +1,32 @@
+#include <iostream>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.8 v1
+ *
+ *
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+int main () {
+
+ string Xer;
+ cout << "Enter a string" << endl;
+ cin >> Xer;
+ decltype(Xer.size()) Index = 0;
+ while(Index != Xer.size()) {
+ Xer[Index++] = 'X'; // We can use post increment operator
+ //Index++; // Or we can increment it here
+ //++Index; // We could also do this
+ }
+
+ cout << Xer << endl;
+ return 0;
+}
diff --git a/3p8-v2.cpp b/3p8-v2.cpp
new file mode 100644
index 0000000..158de77
--- /dev/null
+++ b/3p8-v2.cpp
@@ -0,0 +1,33 @@
+#include <iostream>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.8 v2
+ *
+ * I think both while and for are viable but i will say the range for is very minimal and nice.
+ * My C programmer brain feels more at home using something like a regular for / while loop with subscripts
+ * But i do think the range for is nice. It might take a bit of time getting used to using ranged for loops
+ * and it might take some time being able to recognise which situations it's best used but i think its a
+ * great tool to have either way.
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+int main () {
+
+ string Xer;
+ cout << "Enter a string" << endl;
+ cin >> Xer;
+ for(decltype(Xer.size()) Index = 0 ; Index != Xer.size() ; ++Index /*or Index++*/) { // Make reference for every character in Xer
+ Xer[Index] = 'X';
+ }
+
+ cout << Xer << endl;
+ return 0;
+}
diff --git a/3p9.cpp b/3p9.cpp
new file mode 100644
index 0000000..25077ce
--- /dev/null
+++ b/3p9.cpp
@@ -0,0 +1,25 @@
+#include <iostream>
+#include "sales_data.hpp"
+#include "sales_item.hpp"
+
+/*
+ *
+ * 3.9
+ *
+ * Program does compile but not sure if valid. It has a size of 0 so its probably undefined behavior.
+ */
+
+using std::string;
+using std::cout;
+using std::cin;
+using std::cerr;
+using std::clog;
+using std::endl;
+int main () {
+
+ string s;
+ auto Size = s.size();
+ cout << Size << endl;
+ cout << s[0] << endl;
+ return 0;
+}