From 19903ae3536255dfa2778846275267605cee9f7e Mon Sep 17 00:00:00 2001 From: Oskar Date: Sun, 18 Aug 2024 21:02:35 +0200 Subject: more exercises, one unfinished --- 4p13.cpp | 23 +++++++++++++++++++++++ 4p14.cpp | 21 +++++++++++++++++++++ 4p15.cpp | 26 ++++++++++++++++++++++++++ 4p16.cpp | 29 +++++++++++++++++++++++++++++ 4p17.cpp | 14 ++++++++++++++ 4p18.cpp | 20 ++++++++++++++++++++ 4p19.cpp | 19 +++++++++++++++++++ increment-tests.cpp | 19 +++++++++++++++++++ 8 files changed, 171 insertions(+) create mode 100644 4p13.cpp create mode 100644 4p14.cpp create mode 100644 4p15.cpp create mode 100644 4p16.cpp create mode 100644 4p17.cpp create mode 100644 4p18.cpp create mode 100644 4p19.cpp create mode 100644 increment-tests.cpp 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 + +/* + * + * 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 + +/* + * + * 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 + +/* + * + * 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 + +/* + * + * 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 +#include + +/* + * + * 4.19 + * + * + */ + +int main () { + + int *ptr = nullptr; + int aa = 12374; + int ival = 0; + ptr = &aa; + vector 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 + +/* + * + * + * + * + */ + +int main () { + + int i = 0; + int j = 0; + int k = 0; + j = ++i; + k = i++; + std::cout << i << " " << j << " " << k << std::endl; + return 0; +} -- cgit v1.2.3