diff --git a/.gitignore b/.gitignore index a311db0..c95de48 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,14 @@ cpp/ANSI/W1/Day3/3.7/printchars2 cpp/ANSI/W1/Day3/3.8/enumeratedconstants cpp/ANSI/W1/Day3/3.9/enumeratedconstants2 cpp/ANSI/W1/Day3/Ex.SpecialChars/specialchars +cpp/ANSI/W1/Day4/4.1/complexexpressions +cpp/ANSI/W1/Day4/4.2/integerOverflow +cpp/ANSI/W1/Day4/4.3/prefixPostfixOperators +cpp/ANSI/W1/Day4/4.4/nestedif +cpp/ANSI/W1/Day4/4.5/elseif +cpp/ANSI/W1/Day4/4.6/complexnestedif +cpp/ANSI/W1/Day4/4.7/brokenbracesif +cpp/ANSI/W1/Day4/4.8/fixedbracesif +cpp/ANSI/W1/Day4/Quiz/quiz +cpp/ANSI/W1/Day4/Ex.LogicalOperators/logicaloperators +cpp/ANSI/W1/Day4/4.9/conditionaloperator diff --git a/cpp/ANSI/W1/Day4/4.1/Makefile b/cpp/ANSI/W1/Day4/4.1/Makefile new file mode 100644 index 0000000..0a70dcd --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.1/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o complexexpressions main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: complexexpressions + ./complexexpressions + +clean: + rm -f complexexpressions diff --git a/cpp/ANSI/W1/Day4/4.1/main.cpp b/cpp/ANSI/W1/Day4/4.1/main.cpp new file mode 100644 index 0000000..fd533dc --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.1/main.cpp @@ -0,0 +1,17 @@ +// Demonstrating how to use Complex Expressions +#include + +int main() +{ + using namespace std; + + int a=0, b=0, x=0, y=35; + cout << "a: " << a << " b: " << b; + cout << " x: " << x << " y: " << y << endl; + a = 9; + b = 7; + y = x = a+b; + cout << "a: " << a << " b: " << b; + cout << " x: " << x << " y: " << y << endl; + return 0; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/4.2/Makefile b/cpp/ANSI/W1/Day4/4.2/Makefile new file mode 100644 index 0000000..8b8b172 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.2/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o integerOverflow main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: integerOverflow + ./integerOverflow + +clean: + rm -f integerOverflow diff --git a/cpp/ANSI/W1/Day4/4.2/main.cpp b/cpp/ANSI/W1/Day4/4.2/main.cpp new file mode 100644 index 0000000..59b1c48 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.2/main.cpp @@ -0,0 +1,19 @@ +// Demonstration of subtraction and Interger Overflow +#include + +using namespace std; + +int main() +{ + typedef unsigned int UINT; + UINT difference; + UINT bigNumber = 100; + UINT smallNumber = 50; + + difference = bigNumber - smallNumber; + cout << "Difference is: " << difference; + + difference = smallNumber - bigNumber; + cout << "\nNow the difference is: " << difference << endl; + return 0; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/4.3/Makefile b/cpp/ANSI/W1/Day4/4.3/Makefile new file mode 100644 index 0000000..cf97c86 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.3/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o prefixPostfixOperators main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: prefixPostfixOperators + ./prefixPostfixOperators + +clean: + rm -f prefixPostfixOperators diff --git a/cpp/ANSI/W1/Day4/4.3/main.cpp b/cpp/ANSI/W1/Day4/4.3/main.cpp new file mode 100644 index 0000000..494ccf7 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.3/main.cpp @@ -0,0 +1,34 @@ +/* Demonstrate use of prefix and postix increment +and decrement operators */ +#include + +int main() +{ + using namespace std; + + int myAge = 39; // Initialize two Integers + int yourAge = 75; + + cout << "I am: " << myAge << " years old.\n"; + cout << "You are: " << yourAge << " years old\n"; + myAge++; // Postfix increment + ++yourAge; // prefix increment + + cout << "One year passes...\n"; + + cout << "I am: " << myAge << " years old.\n"; + cout << "You are: " << yourAge << " years old\n"; + myAge++; // Postfix increment + ++yourAge; // prefix increment + + cout << "Another year passes...\n"; + + cout << "I am: " << myAge << " years old.\n"; + cout << "You are: " << yourAge << " years old\n"; + + cout << "Let's print it again.\n"; + cout << "I am: " << myAge << " years old.\n"; + cout << "You are: " << yourAge << " years old\n"; + return 0; + +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/4.4/Makefile b/cpp/ANSI/W1/Day4/4.4/Makefile new file mode 100644 index 0000000..27de7aa --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.4/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o nestedif main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: nestedif + ./nestedif + +clean: + rm -f nestedif diff --git a/cpp/ANSI/W1/Day4/4.4/main.cpp b/cpp/ANSI/W1/Day4/4.4/main.cpp new file mode 100644 index 0000000..0920022 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.4/main.cpp @@ -0,0 +1,41 @@ +/* Demonstrate use of if statement +used with relational operators */ + +#include + +int main() +{ + using namespace std; + + int metsScore, yankeesScore; + cout << "Enter the score for the Mets: "; + cin >> metsScore; + + cout<< "\nEnter the score of the Yankees: "; + cin >> yankeesScore; + + cout << endl; + + if (metsScore > yankeesScore) + cout << "Let's Go Mets!\n"; + + if (metsScore < yankeesScore) + { + cout << "Go Yankees!\n"; + } + if (metsScore == yankeesScore) + { + cout << "A tie? Naah, Can't be.\n"; + cout << "Give me the real score for the Yanks: "; + cin >> yankeesScore; + + if (metsScore > yankeesScore) + cout << "Knew it! Let's go Mets!\n"; + if (metsScore > yankeesScore) + cout << "Wow, go Yanks!\n"; + if (metsScore == yankeesScore) + cout << "Wow, it really was a tie!\n"; + } + cout << "\n Thanks for telling me.\n"; + return 0; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/4.5/Makefile b/cpp/ANSI/W1/Day4/4.5/Makefile new file mode 100644 index 0000000..7e62dd2 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.5/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o elseif main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: elseif + ./elseif + +clean: + rm -f elseif diff --git a/cpp/ANSI/W1/Day4/4.5/main.cpp b/cpp/ANSI/W1/Day4/4.5/main.cpp new file mode 100644 index 0000000..2f00ae4 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.5/main.cpp @@ -0,0 +1,21 @@ +/* Demonstrates the use of the else clause +while using the if condition */ + +#include + +int main() +{ + using namespace std; + + int firstNumber, secondNumber; + cout << "Please enter a big number: "; + cin >> firstNumber; + cout << "\nPlease enter a smaller number: "; + cin >> secondNumber; + if (firstNumber > secondNumber) + cout << "\nThanks!\n"; + else + cout << "\nWow, you should go back to first grade.\n"; + + return 0; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/4.6/Makefile b/cpp/ANSI/W1/Day4/4.6/Makefile new file mode 100644 index 0000000..28ad5ee --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.6/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o complexnestedif main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: complexnestedif + ./complexnestedif + +clean: + rm -f complexnestedif diff --git a/cpp/ANSI/W1/Day4/4.6/main.cpp b/cpp/ANSI/W1/Day4/4.6/main.cpp new file mode 100644 index 0000000..358ffab --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.6/main.cpp @@ -0,0 +1,32 @@ +// Demonstrates advanced If statements +#include + +int main() +{ + /*Ask for two numbers + Assign the numbers to bigNumber and littleNumber + if bigNumber is bigger than littleNumber, + see if they are evenly divisle + If they are, see if they are the same number */ + + using namespace std; + + int firstNumber, secondNumber; + + cout << "Enter two number.\nFirst: "; + cin >> firstNumber; + cout << "\nSecond: "; + cin >> secondNumber; + + if ( firstNumber >= secondNumber) + { + if ( (firstNumber % secondNumber) == 0) //evenly divisble - no remainder + { + if (firstNumber == secondNumber) + cout << "They are the same!\n"; + } + else + cout << "They are not evenly divisble!\n"; + return 0; + } +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/4.7/Makefile b/cpp/ANSI/W1/Day4/4.7/Makefile new file mode 100644 index 0000000..7ec3108 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.7/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o brokenbracesif main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: brokenbracesif + ./brokenbracesif + +clean: + rm -f brokenbracesif diff --git a/cpp/ANSI/W1/Day4/4.7/main.cpp b/cpp/ANSI/W1/Day4/4.7/main.cpp new file mode 100644 index 0000000..9f0a36d --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.7/main.cpp @@ -0,0 +1,22 @@ +/* Demonstrates why braces are important inside +nested if statements. This program will exit early. +Not at the intended else clause, but in the nested if.*/ +#include + +int main() +{ + using namespace std; + + int x; + cout << "Enter a number less than 10 or greater than 100: "; + cin >> x; + cout << "\n"; + + if (x >= 10) + if (x > 100) + cout << "More than 100, Thanks!\n"; + else + cout << "Less than 10, Thanks\n"; + + return 0; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/4.8/Makefile b/cpp/ANSI/W1/Day4/4.8/Makefile new file mode 100644 index 0000000..b6b1dbf --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.8/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o fixedbracesif main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: fixedbracesif + ./fixedbracesif + +clean: + rm -f fixedbracesif diff --git a/cpp/ANSI/W1/Day4/4.8/main.cpp b/cpp/ANSI/W1/Day4/4.8/main.cpp new file mode 100644 index 0000000..433a840 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.8/main.cpp @@ -0,0 +1,23 @@ +/* Demonstrates the proper way to use braces +to seperate if statements in advanced nested +if statements*/ +#include + +int main () +{ + using namespace std; + + int x; + cout << "Enter a number less than 10 or greater than 100: "; + cin >> x; + cout << endl; + + if (x >= 100) + { + if (x > 100) + cout << "More than 100, Thanks!\n"; + } + else + cout << "Less than 10, Thanks!\n"; + return 0; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/4.9/Makefile b/cpp/ANSI/W1/Day4/4.9/Makefile new file mode 100644 index 0000000..3acbc56 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.9/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o conditionaloperator main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: conditionaloperator + ./conditionaloperator + +clean: + rm -f conditionaloperator diff --git a/cpp/ANSI/W1/Day4/4.9/main.cpp b/cpp/ANSI/W1/Day4/4.9/main.cpp new file mode 100644 index 0000000..cc90b67 --- /dev/null +++ b/cpp/ANSI/W1/Day4/4.9/main.cpp @@ -0,0 +1,29 @@ +// Demonstrates the conditional operator "?" +#include + +int main() +{ + using namespace std; + + int x, y, z; + cout << "Enter two numbers.\n"; + cout << "First number: "; + cin >> x; + cout << "\nSecond number: "; + cin >> y; + cout << endl; + + if (x > y) + z = x; + else + z = y; + + cout << "After it test, z: " << z << endl; + + /* This line is read as “If expression1 (x>y) is true, return the value of expression2; otherwise, +return the value of expression3.” Assigned to variable "z" */ + z = (x > y) ? x : y; + + cout << "After conditional test, z: " << z << endl; + return 0; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/Ex.LogicalOperators/Makefile b/cpp/ANSI/W1/Day4/Ex.LogicalOperators/Makefile new file mode 100644 index 0000000..c25a753 --- /dev/null +++ b/cpp/ANSI/W1/Day4/Ex.LogicalOperators/Makefile @@ -0,0 +1,13 @@ +CFLAGS = -std=c++03 -O2 +LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi + +1.1: main.cpp + c++ $(CFLAGS) -o logicaloperators main.cpp $(LDFLAGS) + +.PHONY: test clean + +test: logicaloperators + ./logicaloperators + +clean: + rm -f logicaloperators diff --git a/cpp/ANSI/W1/Day4/Ex.LogicalOperators/main.cpp b/cpp/ANSI/W1/Day4/Ex.LogicalOperators/main.cpp new file mode 100644 index 0000000..ee662e2 --- /dev/null +++ b/cpp/ANSI/W1/Day4/Ex.LogicalOperators/main.cpp @@ -0,0 +1,15 @@ +/* A list of the logical operators in C++ */ +#include + +int main() +{ + using namespace std; + + cout << "The && operator means AND . ie if ( (x == 5) && (y == 5) )\n\n"; + cout << "The || operator means OR . ie if ( (x == 5) || (y == 5) )\n\n"; + cout << "The ! operator mean NOT equal to. ie if ( var1 != 420)\n\n"; + + // Appendix C has order of operations or relational precedence on conditional statements + + return 0; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/Quiz/main.cpp b/cpp/ANSI/W1/Day4/Quiz/main.cpp new file mode 100644 index 0000000..dbb13b6 --- /dev/null +++ b/cpp/ANSI/W1/Day4/Quiz/main.cpp @@ -0,0 +1,12 @@ +#include + +int main() +{ + using namespace std; + + int x = 8+2*3; + + cout << x << endl; + + return 0; +} \ No newline at end of file diff --git a/cpp/ANSI/W1/Day4/Quiz/quiz.txt b/cpp/ANSI/W1/Day4/Quiz/quiz.txt new file mode 100644 index 0000000..8512279 --- /dev/null +++ b/cpp/ANSI/W1/Day4/Quiz/quiz.txt @@ -0,0 +1,58 @@ +Quiz +1. What is an expression? + +2. Is x = 5 + 7 an expression? What is its value? + +3. What is the value of 201 / 4? +Depends on the datatype. If its an INT it would be 50. float would have the decimal. etc... +4. What is the value of 201 % 4? +0 because there would be a remainder so its not perfectly divisble +5. If myAge, a, and b are all int variables, what are their values after +myAge = 39; +a = myAge++; +b = ++myAge; + +6. What is the value of 8+2*3? +14 - follows pemdas? +7. What is the difference between if(x = 3) and if(x == 3)? + +Around page 80 + + +8. Do the following values evaluate true or false? +a. 0 +b. 1 +c. –1 +d. x = 0 +e. x == 0 // assume that x has the value of 0 + +------------------------------------------------ +Exercises +1. Write a single if statement that examines two integer variables and changes the +larger to the smaller, using only one else clause. + +2. Examine the following program. Imagine entering three numbers, and write what +output you expect. + +#include +using namespace std; +int main() +{ + +int a, b, c; + +cout << “Please enter three numbers\n”; +cout << “a: “; +cin >> a; +cout << “\nb: “; +cin >> b; +cout << “\nc: “; +cin >> c; + +if (c = (a-b)) + cout << “a: “ << a << “ minus b: “ << b << ” equals c: “ << c; +else + cout << “a-b does not equal c: “; + +return 0; +} \ No newline at end of file