summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-08-18 21:02:35 +0200
committerOskar <[email protected]>2024-08-18 21:02:35 +0200
commit19903ae3536255dfa2778846275267605cee9f7e (patch)
tree94f9ecb62c1d02b5c00f4fc9c760ec4ed4a10105
parent3d0a78eaaafff660cd04adf23a6bec1721187abf (diff)
more exercises, one unfinished
-rw-r--r--4p13.cpp23
-rw-r--r--4p14.cpp21
-rw-r--r--4p15.cpp26
-rw-r--r--4p16.cpp29
-rw-r--r--4p17.cpp14
-rw-r--r--4p18.cpp20
-rw-r--r--4p19.cpp19
-rw-r--r--increment-tests.cpp19
8 files changed, 171 insertions, 0 deletions
diff --git a/4p13.cpp b/4p13.cpp
new file mode 100644
index 0000000..b5cfdf1
--- /dev/null
+++ b/4p13.cpp
@@ -0,0 +1,23 @@
+#include <iostream>
+
+/*
+ *
+ * 4.13
+ *
+ *
+ */
+
+int main () {
+
+
+ int i1;
+ int i2;
+ double d1;
+ double d2;
+ double threepointfive = 3.5; // made a variable for 3.5 to avoid compiler errors
+ d1 = i1 = threepointfive; // i == 3, d == 3
+ i2 = d2 = threepointfive; // d == 3.5, i == 3
+ std::cout << d1 << " <-d1(3) (3)i1-> " << i1 << std::endl;
+ std::cout << d2 << " <-d2(3.5) (3)i2-> " << i2 << std::endl;
+ return 0;
+}
diff --git a/4p14.cpp b/4p14.cpp
new file mode 100644
index 0000000..84fe963
--- /dev/null
+++ b/4p14.cpp
@@ -0,0 +1,21 @@
+#include <iostream>
+
+/*
+ *
+ * 4.14
+ *
+ *
+ */
+
+int main () {
+
+ int i = 0;
+
+ //if(42 = i) {} Not valid, error
+ int ifs2 = i = 42;
+ if(ifs2) { // evaluates as true because nonZero == true
+ std::cout << "..." << std::endl;
+ }
+
+ return 0;
+}
diff --git a/4p15.cpp b/4p15.cpp
new file mode 100644
index 0000000..7ac66d5
--- /dev/null
+++ b/4p15.cpp
@@ -0,0 +1,26 @@
+#include <iostream>
+
+/*
+ *
+ * 4.15
+ *
+ *
+ */
+
+int main () {
+
+ double dval;
+ int ival;
+ int *pi;
+ /*
+ ival = 0;
+ dval = 0;
+ pi = 0;
+
+ I don't really see a reason not to do it this way but both of these examples work
+ */
+ ival = dval = 0;
+ pi = 0;
+ std::cout << dval << ival << pi << std::endl;
+ return 0;
+}
diff --git a/4p16.cpp b/4p16.cpp
new file mode 100644
index 0000000..5205446
--- /dev/null
+++ b/4p16.cpp
@@ -0,0 +1,29 @@
+#include <iostream>
+
+/*
+ *
+ * 4.16
+ *
+ * Added a mockup getPtr function to demonstrate
+ */
+
+int *getPtr(int *f) {
+
+ return f;
+}
+
+int main () {
+
+ int f;
+ int *p = nullptr;
+ if ((p = getPtr(&f)) != 0) {
+ std::cout << "not nullptr" << std::endl;
+ }
+
+ int i = 1024;
+ if (i == 1024) {
+ std::cout << "i: 1024" << std::endl;
+ }
+
+ return 0;
+}
diff --git a/4p17.cpp b/4p17.cpp
new file mode 100644
index 0000000..63459d8
--- /dev/null
+++ b/4p17.cpp
@@ -0,0 +1,14 @@
+
+/*
+ *
+ * 4.17
+ *
+ *
+ */
+
+int main () {
+
+ // Prefix increments will increment the object by 1 and return the incremented object as result.
+ // Postfix increment will increment the object by 1 but return a copy of the object before being incremented.
+ return 0;
+}
diff --git a/4p18.cpp b/4p18.cpp
new file mode 100644
index 0000000..0788e51
--- /dev/null
+++ b/4p18.cpp
@@ -0,0 +1,20 @@
+
+/*
+ *
+ * 4.18
+ *
+ *
+ */
+
+int main () {
+ /*
+ auto pbeg = v.begin();
+ while (pbeg != v.end() && *beg >= 0)
+ cout << *pbeg++ << endl;
+ */
+
+ // It would increment pbeg by 1 and then dereference and print.
+ // This would mean that the first element in the vector is not printed
+ // This would also mean dereferencing whatever is at v.end() which is not a good idea.
+ return 0;
+}
diff --git a/4p19.cpp b/4p19.cpp
new file mode 100644
index 0000000..ab94f28
--- /dev/null
+++ b/4p19.cpp
@@ -0,0 +1,19 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 4.19
+ *
+ *
+ */
+
+int main () {
+
+ int *ptr = nullptr;
+ int aa = 12374;
+ int ival = 0;
+ ptr = &aa;
+ vector<int> vec;
+ return 0;
+}
diff --git a/increment-tests.cpp b/increment-tests.cpp
new file mode 100644
index 0000000..d5b5665
--- /dev/null
+++ b/increment-tests.cpp
@@ -0,0 +1,19 @@
+#include <iostream>
+
+/*
+ *
+ *
+ *
+ *
+ */
+
+int main () {
+
+ int i = 0;
+ int j = 0;
+ int k = 0;
+ j = ++i;
+ k = i++;
+ std::cout << i << " " << j << " " << k << std::endl;
+ return 0;
+}