Skip to content

Commit 5877988

Browse files
committed
Added materail for Section08 thus far: Coding Exercise 7 and 8, and Increment and Decrement Operators, amd Mixed Expressions and Conversions.
1 parent 7198409 commit 5877988

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3008
-1
lines changed

Section08-Statements_and_Operators/README.md

+92-1
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,95 @@
8686
- Multiplication (`*`)
8787
- Division (`/`)
8888
- Modulo or remainder (`%`)
89-
- only works with integers
89+
- only works with integers
90+
91+
## Increment and Decrement Operators
92+
93+
- Increment operator `++`
94+
- Decrement operator `--`
95+
- Increments or decrements its operand by 1
96+
- Can be used with integers, floating point types and pointers
97+
- Below behavior is not identical
98+
- Prefix `++num`
99+
- Postfix `num++`
100+
- see Example 2 - 5 under `section08_source_code/IncrementDecrementOperators/main.cpp`
101+
- Don't use the operator twice for the same variable in the same statement!!!
102+
- i.e. DON'T:
103+
- `cout << i++ << ++i`
104+
- `cout << i++ + ++i`
105+
106+
## Mixed Type Expressions
107+
108+
- C++ operations occur on same type operands
109+
- If operands are of different types, C++ will convert one
110+
- Important! Since it could affect calculation results
111+
- C++ will attempt to automatically convert types (coercion)
112+
- If it can't, a compiler error will occur
113+
- i.e. try to assign `string` to `int`
114+
115+
### Conversions
116+
117+
- Higer vs Lower types are based on the size of the values the type can hold
118+
- can typically convert from lower typ to higher type automatically since it will fit but the opposite may not be true
119+
- decending order: long double, double, float, unsigned long, long, unsigned int, int
120+
- short and char types are always converted to int
121+
- Type Coercion: conversion of one operand to another data
122+
- sometimes done automatically
123+
- Promotion: conversion to a higher type
124+
- Used in mathematical expressions
125+
- Demotion: conversion to a lower type
126+
- Used with assignment to lower type
127+
128+
#### Example
129+
- The `lower` is promoted to a `higher`
130+
- lower **op** higher: `2 * 5.2`
131+
- 2 is promoted to 2.0 automatically
132+
- The `higher` is demoted to a `lower`
133+
- lower = higher:
134+
```c++
135+
int num {0};
136+
num = 100.2;
137+
```
138+
139+
### Explicit Type Casting - static_cast<type>
140+
141+
```c++
142+
int total_amount {100};
143+
int total_number {8};
144+
double average {0.0};
145+
146+
average = total_amount / total_number;
147+
cout << average << endl;
148+
// displays 12 since int operations
149+
150+
/*
151+
- use static_cast<type>
152+
- () the variable name
153+
- total_number is automatically promoted
154+
- do double division instead of int division
155+
*/
156+
average = static_cast<double>(total_amount) / total_number;
157+
cout << average << endl;
158+
// displays 12.5
159+
```
160+
161+
## Testing for Equality `==` & `!=`
162+
163+
- Compares the value of 2 expressions
164+
- Evaluates to a Boolean (True or False, 1 or 0)
165+
- Commonly used in control flow statements
166+
- `expr1 == expr2`
167+
- `expr1 != expr2`
168+
- `100 == 200` -> False
169+
- `num1 != num2`
170+
```c++
171+
bool result {false};
172+
173+
result = (100 == 50 + 50);
174+
result = (num1 != num2);
175+
176+
cout << (num1 == num2) << endl; // 0 or 1
177+
cout << std::boolalpha;
178+
cout << (num1 == num2) << endl; // true or false
179+
cout << std::noboolalpha;
180+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Coding Exercise 7: Using the Assignment Operator
2+
3+
Write a program that uses the assignment operator `=` to change the value of an initialized variable as well as assign the calue of one variable to another.
4+
5+
- begin by declaring and initialize the integer variable `num1` to the value of `13`.
6+
- now declare and initialize the integer variable `num2` to the value `0`.
7+
- use the assignment operator to assign the value of `num1` to `5`.
8+
- now use the assignment operator to assign the value of `num1` to `num2`.
9+
10+
## Solution
11+
12+
```c++
13+
#include <iostream>
14+
#include <tuple>
15+
using namespace std;
16+
17+
void assignment_operator() {
18+
19+
//----WRITE YOUR CODE BELOW THIS LINE----
20+
// Declare num1 as an integer and initialize it to 13.
21+
int num1 {13};
22+
23+
// Declare num2 as an integer and initialize it to 0.
24+
int num2 {0};
25+
26+
// Assign the value 5 to num1
27+
num1 = 5;
28+
29+
// Assign the value of num1 to num2
30+
num2 = num1;
31+
32+
33+
//----WRITE YOUR CODE ABOVE THIS LINE----
34+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
35+
36+
cout << num1 << " " << num2;
37+
}
38+
```
39+
40+
# Coding Exercise 8: Using the Arithmetic Operators
41+
42+
Write a program that uses arithmetic operators to manipulate an integer number that is provided to you.
43+
You do **NOT** have to declare `number`, it is already declared and will be set to various values by the automated tester.
44+
In order to complete this exercise you will have to update the value contained within the variable `number` by using the currently contained value as an argument in the statement.
45+
This can be done through the use of the assignment operator `=` int he following way:
46+
- i.e. `number = number + 4`
47+
Lets assume that `number` is currently holding the value `3`. This means that the above statement is equivalent to `number = 3 + 4`. Thereby, through the assignment operator, the new value of `number` will be 7.
48+
49+
- use the arithmetic operators in the manner and order in which they are listed below. For those who feel inclined, try challenging yourself by completing all operations in one statement remembering the rules of **PEMDAS**.
50+
- use the multiplication operator `*` to `double` the value of the variable `number` and store the result back in `number`.
51+
- use the addition operator `+` to add `9` to the variable `number` and store the result back in `number`.
52+
- use the subtraction operator `-` to subtract `3` from the variable `number` and store the result back in `number`.new value.
53+
- use the division operator `/` to divide the variable `number` by `2` and store the result back in `number`.
54+
- use the subtraction operator `-` to subtract the variable named `original_number` from the variable `number` and store the result back in `number`.
55+
- use the modulo operator `%` to find the reamined of the new value when divided by `3` and store the result back in `number`.
56+
57+
## Solution
58+
59+
```c++
60+
#include <iostream>
61+
using namespace std;
62+
63+
int arithmetic_operators(int number) {
64+
int original_number {number};
65+
66+
67+
//----WRITE YOUR CODE BELOW THIS LINE----
68+
69+
//-- Multiply number by 2 and assign the result back to number
70+
number = number * 2;
71+
//-- Add 9 to number and assign the result back to number
72+
number = number + 9;
73+
//-- Subtract 3 from number and assign the result back to number
74+
number = number - 3;
75+
//-- Divide number by 2 and assign the result back to number
76+
number = number / 2;
77+
//-- Subtract original_number from number and assign the result back to number
78+
number = number - original_number;
79+
//-- Take the modulus 3 (%) of number and assign it back to number
80+
number = number % 3;
81+
82+
//----WRITE YOUR CODE ABOVE THIS LINE----
83+
84+
return number;
85+
}
86+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Session Name="C:\Users\frank\Desktop\CPPExamples\Section8\Section8.workspace">
3+
<int Value="0" Name="m_selectedTab"/>
4+
<wxString Value="C:\Users\frank\Desktop\CPPExamples\Section8\Section8.workspace" Name="m_workspaceName"/>
5+
<TabInfoArray Name="TabInfoArray"/>
6+
<SerializedObject Name="m_breakpoints">
7+
<long Value="0" Name="Count"/>
8+
</SerializedObject>
9+
</Session>
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
##
2+
## Auto Generated makefile by CodeLite IDE
3+
## any manual changes will be erased
4+
##
5+
## Debug
6+
ProjectName :=ArithmeticOperators
7+
ConfigurationName :=Debug
8+
WorkspacePath :=C:/Users/frank/Desktop/CPPExamples/Section8
9+
ProjectPath :=C:/Users/frank/Desktop/CPPExamples/Section8/ArithmeticOperators
10+
IntermediateDirectory :=./Debug
11+
OutDir := $(IntermediateDirectory)
12+
CurrentFileName :=
13+
CurrentFilePath :=
14+
CurrentFileFullPath :=
15+
User :=frank
16+
Date :=31/01/2018
17+
CodeLitePath :="C:/Program Files/CodeLite"
18+
LinkerName :=C:/MinGW/bin/g++.exe
19+
SharedObjectLinkerName :=C:/MinGW/bin/g++.exe -shared -fPIC
20+
ObjectSuffix :=.o
21+
DependSuffix :=.o.d
22+
PreprocessSuffix :=.i
23+
DebugSwitch :=-g
24+
IncludeSwitch :=-I
25+
LibrarySwitch :=-l
26+
OutputSwitch :=-o
27+
LibraryPathSwitch :=-L
28+
PreprocessorSwitch :=-D
29+
SourceSwitch :=-c
30+
OutputFile :=$(IntermediateDirectory)/$(ProjectName)
31+
Preprocessors :=
32+
ObjectSwitch :=-o
33+
ArchiveOutputSwitch :=
34+
PreprocessOnlySwitch :=-E
35+
ObjectsFileList :="ArithmeticOperators.txt"
36+
PCHCompileFlags :=
37+
MakeDirCommand :=makedir
38+
RcCmpOptions :=
39+
RcCompilerName :=C:/MinGW/bin/windres.exe
40+
LinkOptions :=
41+
IncludePath := $(IncludeSwitch). $(IncludeSwitch).
42+
IncludePCH :=
43+
RcIncludePath :=
44+
Libs :=
45+
ArLibs :=
46+
LibPath := $(LibraryPathSwitch).
47+
48+
##
49+
## Common variables
50+
## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables
51+
##
52+
AR := C:/MinGW/bin/ar.exe rcu
53+
CXX := C:/MinGW/bin/g++.exe
54+
CC := C:/MinGW/bin/gcc.exe
55+
CXXFLAGS := -std=c++14 -Wall -g -O0 -std=c++14 -Wall $(Preprocessors)
56+
CFLAGS := -g -O0 -Wall $(Preprocessors)
57+
ASFLAGS :=
58+
AS := C:/MinGW/bin/as.exe
59+
60+
61+
##
62+
## User defined environment variables
63+
##
64+
CodeLiteDir:=C:\Program Files\CodeLite
65+
Objects0=$(IntermediateDirectory)/main.cpp$(ObjectSuffix)
66+
67+
68+
69+
Objects=$(Objects0)
70+
71+
##
72+
## Main Build Targets
73+
##
74+
.PHONY: all clean PreBuild PrePreBuild PostBuild MakeIntermediateDirs
75+
all: $(OutputFile)
76+
77+
$(OutputFile): $(IntermediateDirectory)/.d $(Objects)
78+
@$(MakeDirCommand) $(@D)
79+
@echo "" > $(IntermediateDirectory)/.d
80+
@echo $(Objects0) > $(ObjectsFileList)
81+
$(LinkerName) $(OutputSwitch)$(OutputFile) @$(ObjectsFileList) $(LibPath) $(Libs) $(LinkOptions)
82+
83+
MakeIntermediateDirs:
84+
@$(MakeDirCommand) "./Debug"
85+
86+
87+
$(IntermediateDirectory)/.d:
88+
@$(MakeDirCommand) "./Debug"
89+
90+
PreBuild:
91+
92+
93+
##
94+
## Objects
95+
##
96+
$(IntermediateDirectory)/main.cpp$(ObjectSuffix): main.cpp $(IntermediateDirectory)/main.cpp$(DependSuffix)
97+
$(CXX) $(IncludePCH) $(SourceSwitch) "C:/Users/frank/Desktop/CPPExamples/Section8/ArithmeticOperators/main.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/main.cpp$(ObjectSuffix) $(IncludePath)
98+
$(IntermediateDirectory)/main.cpp$(DependSuffix): main.cpp
99+
@$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/main.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/main.cpp$(DependSuffix) -MM main.cpp
100+
101+
$(IntermediateDirectory)/main.cpp$(PreprocessSuffix): main.cpp
102+
$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/main.cpp$(PreprocessSuffix) main.cpp
103+
104+
105+
-include $(IntermediateDirectory)/*$(DependSuffix)
106+
##
107+
## Clean
108+
##
109+
clean:
110+
$(RM) -r ./Debug/
111+
112+

0 commit comments

Comments
 (0)