summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-08-29 13:27:45 +0200
committerOskar <[email protected]>2024-08-29 13:27:45 +0200
commit743c47c671347a0ac0f8f64b7117b48c65bab073 (patch)
tree5e33efe7a3e4d6ba5a9a6ee34762e978b51cb6b3
parent1208defdbc457a89c0d9aea3b8faf86b8492761f (diff)
more
-rw-r--r--4p36.cpp16
-rw-r--r--4p37.cpp25
-rw-r--r--4p38.cpp18
-rw-r--r--5p1.cpp18
-rw-r--r--5p2.cpp23
-rw-r--r--5p3.cpp12
-rw-r--r--5p4.cpp27
-rw-r--r--5p5.cpp37
-rw-r--r--5p6.cpp24
-rw-r--r--5p7.cpp43
-rw-r--r--5p8.cpp23
-rw-r--r--gede2.ini17
12 files changed, 266 insertions, 17 deletions
diff --git a/4p36.cpp b/4p36.cpp
new file mode 100644
index 0000000..5667dcb
--- /dev/null
+++ b/4p36.cpp
@@ -0,0 +1,16 @@
+#include <iostream>
+
+/*
+ *
+ * 4.36
+ *
+ *
+ */
+
+int main () {
+
+ int i = 10;
+ double d = 2;
+ std::cout << (i *= static_cast<int>(d)) << std::endl;
+ return 0;
+}
diff --git a/4p37.cpp b/4p37.cpp
new file mode 100644
index 0000000..2b8b16f
--- /dev/null
+++ b/4p37.cpp
@@ -0,0 +1,25 @@
+#include <iostream>
+
+/*
+ *
+ * 4.37
+ *
+ *
+ */
+
+int main () {
+
+ char s = 'a'; // just a variable we can use the address for to make the program not segfault
+ int i = 0;
+ double d = 0;
+ const std::string *ps = nullptr;
+ char *pc = &s;
+ void *pv = &s;
+ pv = const_cast<std::string*>(ps);
+ i = static_cast<int>(*pc);
+ pv = static_cast<void*>(&d);
+ pc = static_cast<char*>(pv);
+ if(i || d) {} // stop compiler from complaining
+
+ return 0;
+}
diff --git a/4p38.cpp b/4p38.cpp
new file mode 100644
index 0000000..63c814a
--- /dev/null
+++ b/4p38.cpp
@@ -0,0 +1,18 @@
+#include <iostream>
+
+/*
+ *
+ * 4.38
+ *
+ *
+ */
+
+int main () {
+
+ int i = 3;
+ int j = 10;
+ double slope = static_cast<double>(j / i); // divide i and j as integers and then cast returned value to double
+ // double slope = static_cast<double>(j) / i; I suspect that this version of the expression is what we are actually trying to do
+ std::cout << slope << std::endl;
+ return 0;
+}
diff --git a/5p1.cpp b/5p1.cpp
new file mode 100644
index 0000000..bc7c0ed
--- /dev/null
+++ b/5p1.cpp
@@ -0,0 +1,18 @@
+#include <iostream>
+
+/*
+ *
+ * 5.1
+ *
+ *
+ */
+
+int main () {
+
+ // A null statement is just a semicolon ;
+ // For example, we can use it for a while loop (while loops require a statement) if the condition can do all the work in the loop.
+ int i = 0;
+ while (i++ != 10)
+ ;
+ return 0;
+}
diff --git a/5p2.cpp b/5p2.cpp
new file mode 100644
index 0000000..ce158c9
--- /dev/null
+++ b/5p2.cpp
@@ -0,0 +1,23 @@
+#include <iostream>
+
+/*
+ *
+ * 5.2
+ *
+ *
+ */
+
+int main () {
+
+ // A block is when we need to do multiple statements for example within a loop (which only requires 1)
+ // A block is also useful to contain objects in a certain scope and such.
+ int i = 0;
+ int j = 0;
+ while(i != 100) { // Start of block
+ ++i;
+ j = i * 2;
+ } // end of block
+
+ std::cout << i << " " << j << std::endl;
+ return 0;
+}
diff --git a/5p3.cpp b/5p3.cpp
new file mode 100644
index 0000000..58d5fbf
--- /dev/null
+++ b/5p3.cpp
@@ -0,0 +1,12 @@
+#include <iostream>
+
+/*
+ *
+ * Description
+ *
+ *
+ */
+
+int main () {
+ return 0;
+}
diff --git a/5p4.cpp b/5p4.cpp
new file mode 100644
index 0000000..d7cc8f6
--- /dev/null
+++ b/5p4.cpp
@@ -0,0 +1,27 @@
+
+/*
+ *
+ * 5.4
+ *
+ *
+ */
+
+int main () {
+
+ /*
+ auto iter = s.begin();
+ while(iter != s.end()) {
+ // do stuff
+ }
+
+ bool status;
+ while((status = find(word))) {
+ // do stuff
+ }
+
+ if(!status) {
+ // do stuff
+ }
+ */
+ return 0;
+}
diff --git a/5p5.cpp b/5p5.cpp
new file mode 100644
index 0000000..b4aeff5
--- /dev/null
+++ b/5p5.cpp
@@ -0,0 +1,37 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 5.5
+ *
+ *
+ */
+
+int main () {
+
+ int TestScore = 0;
+ std::vector<std::string> GradeLetters = {"F", "D", "C", "B", "A", "A++"};
+ std::string FinalGrade;
+ if(std::cin >> TestScore) {} else { return -1; }
+
+ if(TestScore < 0 || TestScore > 100) { return -1; }
+
+ if(TestScore < 60) {
+ FinalGrade = GradeLetters[0];
+ } else {
+ if(TestScore != 100) {
+ FinalGrade = GradeLetters[(TestScore-50) / 10];
+ if((TestScore % 10) > 7) {
+ FinalGrade += "+";
+ } else if((TestScore % 10) < 3) {
+ FinalGrade += "-";
+ }
+ } else {
+ FinalGrade = GradeLetters[5];
+ }
+ }
+
+ std::cout << "Your grade is: " << FinalGrade << std::endl;
+ return 0;
+}
diff --git a/5p6.cpp b/5p6.cpp
new file mode 100644
index 0000000..3681085
--- /dev/null
+++ b/5p6.cpp
@@ -0,0 +1,24 @@
+#include <iostream>
+#include <vector>
+
+/*
+ *
+ * 5.6
+ *
+ * I had no idea we were only making the simple
+ * version of this program in the previous exercise
+ */
+
+int main () {
+
+ int TestScore = 0;
+ std::vector<std::string> GradeLetters = {"F", "D", "C", "B", "A", "A++"};
+ std::string FinalGrade;
+ if(std::cin >> TestScore) {} else { return -1; }
+
+ if(TestScore < 0 || TestScore > 100) { return -1; }
+
+ (TestScore < 60) ? FinalGrade = GradeLetters[0] : FinalGrade = GradeLetters[(TestScore - 50) / 10];
+ std::cout << "Your grade is: " << FinalGrade << std::endl;
+ return 0;
+}
diff --git a/5p7.cpp b/5p7.cpp
new file mode 100644
index 0000000..fcc850b
--- /dev/null
+++ b/5p7.cpp
@@ -0,0 +1,43 @@
+#include <iostream>
+
+/*
+ *
+ * 5.7
+ *
+ *
+ */
+
+int get_value() {
+
+ return 373;
+}
+
+int main () {
+
+ int ival1 = 50;
+ int ival2 = 100;
+ int minval = 10;
+ int occurs = 40;
+ int ival = 50;
+ if(ival1 != ival2)
+ ival1 = ival2;
+ else ival1 = ival2 = 0;
+
+ if(ival < minval) {
+ minval = ival;
+ occurs = 1;
+ }
+
+ int ival3;
+ if((ival3 = get_value()))
+ std::cout << "ival = " << ival << std::endl;
+ if(!ival3)
+ std::cout << "ival = 0\n";
+
+ if(ival == 0)
+ ival = get_value();
+
+ if(occurs) // stop compiler from complaining
+
+ return 0;
+}
diff --git a/5p8.cpp b/5p8.cpp
new file mode 100644
index 0000000..4e9b5b1
--- /dev/null
+++ b/5p8.cpp
@@ -0,0 +1,23 @@
+
+/*
+ *
+ * 5.8
+ *
+ *
+ */
+
+int main () {
+
+ // Dangling else is the ambiguity in if else statements
+ /*
+ if(condition)
+ statement
+ if(condition2)
+ statement
+ if(condition3)
+ statement
+ else
+ statement
+ */
+ return 0;
+}
diff --git a/gede2.ini b/gede2.ini
deleted file mode 100644
index 17e43c9..0000000
--- a/gede2.ini
+++ /dev/null
@@ -1,17 +0,0 @@
-Breakpoints="/home/oskar/code/primer/4p27.cpp:18;/home/oskar/code/primer/4p27.cpp:12"
-CoreDumpFile="./core"
-Download=1
-GdpPath="gdb"
-GoToRecentlyUsed=""
-InitCommands=""
-InitialBreakpoint="main"
-LastProgram="4p27"
-LastProgramArguments=""
-Mode=0
-ReuseBreakpoints=0
-RunningPid=0
-SerialBaudRate=1200
-SerialPort=""
-TcpHost="localhost"
-TcpPort=2000
-