From 743c47c671347a0ac0f8f64b7117b48c65bab073 Mon Sep 17 00:00:00 2001 From: Oskar Date: Thu, 29 Aug 2024 13:27:45 +0200 Subject: more --- 4p36.cpp | 16 ++++++++++++++++ 4p37.cpp | 25 +++++++++++++++++++++++++ 4p38.cpp | 18 ++++++++++++++++++ 5p1.cpp | 18 ++++++++++++++++++ 5p2.cpp | 23 +++++++++++++++++++++++ 5p3.cpp | 12 ++++++++++++ 5p4.cpp | 27 +++++++++++++++++++++++++++ 5p5.cpp | 37 +++++++++++++++++++++++++++++++++++++ 5p6.cpp | 24 ++++++++++++++++++++++++ 5p7.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 5p8.cpp | 23 +++++++++++++++++++++++ gede2.ini | 17 ----------------- 12 files changed, 266 insertions(+), 17 deletions(-) create mode 100644 4p36.cpp create mode 100644 4p37.cpp create mode 100644 4p38.cpp create mode 100644 5p1.cpp create mode 100644 5p2.cpp create mode 100644 5p3.cpp create mode 100644 5p4.cpp create mode 100644 5p5.cpp create mode 100644 5p6.cpp create mode 100644 5p7.cpp create mode 100644 5p8.cpp delete mode 100644 gede2.ini 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 + +/* + * + * 4.36 + * + * + */ + +int main () { + + int i = 10; + double d = 2; + std::cout << (i *= static_cast(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 + +/* + * + * 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(ps); + i = static_cast(*pc); + pv = static_cast(&d); + pc = static_cast(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 + +/* + * + * 4.38 + * + * + */ + +int main () { + + int i = 3; + int j = 10; + double slope = static_cast(j / i); // divide i and j as integers and then cast returned value to double + // double slope = static_cast(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 + +/* + * + * 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 + +/* + * + * 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 + +/* + * + * 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 +#include + +/* + * + * 5.5 + * + * + */ + +int main () { + + int TestScore = 0; + std::vector 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 +#include + +/* + * + * 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 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 + +/* + * + * 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 - -- cgit v1.2.3