diff options
| -rw-r--r-- | InClassExercise 9/InClassExercise 9.vcxproj | 3 | ||||
| -rw-r--r-- | InClassExercise 9/InClassExercise 9.vcxproj.filters | 9 | ||||
| -rw-r--r-- | InClassExercise 9/Node.h | 14 | ||||
| -rw-r--r-- | InClassExercise 9/PointerExercises.cpp | 21 | ||||
| -rw-r--r-- | InClassExercise 9/PointerExercises.h | 13 | ||||
| -rw-r--r-- | InClassExercise 9/program.cpp | 82 |
6 files changed, 133 insertions, 9 deletions
diff --git a/InClassExercise 9/InClassExercise 9.vcxproj b/InClassExercise 9/InClassExercise 9.vcxproj index 32bc00a..caf0542 100644 --- a/InClassExercise 9/InClassExercise 9.vcxproj +++ b/InClassExercise 9/InClassExercise 9.vcxproj @@ -127,10 +127,13 @@ </Link> </ItemDefinitionGroup> <ItemGroup> + <ClCompile Include="PointerExercises.cpp" /> <ClCompile Include="program.cpp" /> <ClCompile Include="ReferenceExamples.cpp" /> </ItemGroup> <ItemGroup> + <ClInclude Include="Node.h" /> + <ClInclude Include="PointerExercises.h" /> <ClInclude Include="ReferenceExamples.h" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> diff --git a/InClassExercise 9/InClassExercise 9.vcxproj.filters b/InClassExercise 9/InClassExercise 9.vcxproj.filters index 79d55c6..ae02f34 100644 --- a/InClassExercise 9/InClassExercise 9.vcxproj.filters +++ b/InClassExercise 9/InClassExercise 9.vcxproj.filters @@ -21,10 +21,19 @@ <ClCompile Include="program.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="PointerExercises.cpp"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="ReferenceExamples.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="Node.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="PointerExercises.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> </Project>
\ No newline at end of file diff --git a/InClassExercise 9/Node.h b/InClassExercise 9/Node.h new file mode 100644 index 0000000..b0ba7cd --- /dev/null +++ b/InClassExercise 9/Node.h @@ -0,0 +1,14 @@ +#ifndef NODE_H +#define NODE_H + +struct Node { + int data; + Node* _next; + +}; + + + + +#endif // !NODE_H + diff --git a/InClassExercise 9/PointerExercises.cpp b/InClassExercise 9/PointerExercises.cpp new file mode 100644 index 0000000..003734d --- /dev/null +++ b/InClassExercise 9/PointerExercises.cpp @@ -0,0 +1,21 @@ +#include "PointerExercises.h" +#include <iostream> +using std::cout; +using std::endl; + +void Swap(Node* first, Node* second) +{ + int temp = first->_num; + first->_num = second->_num; + second->_num = temp; +} + +void Standardize_101(Node* node) +{ + node->_num %= 101; +} + +void Square(Node* node) +{ + node->_num *= node->_num; +}
\ No newline at end of file diff --git a/InClassExercise 9/PointerExercises.h b/InClassExercise 9/PointerExercises.h new file mode 100644 index 0000000..2894655 --- /dev/null +++ b/InClassExercise 9/PointerExercises.h @@ -0,0 +1,13 @@ +#ifndef POINTER_EXERCISES_H +#define POINTER_EXERCISES_H + +#include "Node.h" + +void Swap(Node* first, Node* second); + +void Standardize_101(Node* node); + +void Square(Node* node); + + +#endif // !POINTER_EXERCISES_H diff --git a/InClassExercise 9/program.cpp b/InClassExercise 9/program.cpp index 0339535..ab09232 100644 --- a/InClassExercise 9/program.cpp +++ b/InClassExercise 9/program.cpp @@ -4,27 +4,91 @@ // Assignment: InClassExercise9 #include <iostream> + +#include "Node.h" + +#include "PointerExercises.h" + +void BasicReferencesAndVariables() { + int variable = 15; + + int& ref = variable; + + int* address = &variable; + + ref++; + + std::cout << variable << std::endl; + + std::cout << address; + +} + + +void DoublesNodeData(Node node) +{ + node.data *= 2; +} +void DoublesNodeDataRef(Node& node) { + node.data *= 2; +} +void DoublesNodeData(Node* node){ + node->data *= 2; +} + +//Node* node = &newNode + + #include "ReferenceExamples.h" using std:: cout; using std:: endl; int main() { - int x = 5, y = 72; - Swap(x, y); + //BasicReferencesAndVariables(); + Node newNode{}; + + newNode.data = 32; + + //std::cout << newNode.data << std::endl; + + Node nextNode{}; + + nextNode.data = 438; + + newNode._next = &nextNode; + + //cout << newNode._next->data << endl; + + Node* address = &newNode; + + cout << address << endl; + + address++; + + cout << address << endl; + + + //int x = 5, y = 72; + + //Swap(x, y); + + //std::cout << "x = " << x << ", y = " << y << endl; - std::cout << "x = " << x << ", y = " << y << endl; + //int n = 4392; - int n = 4392; + //Standardize_101(n); - Standardize_101(n); + //cout << "Modded n = " << n << endl; - cout << "Modded n = " << n << endl; + //Square(n); - Square(n); + //cout << "Squared n = " << n << endl; - cout << "Squared n = " << n << endl; + //cout << " Size of an int" << sizeof(int); - cout << " Size of an int" << sizeof(int); + Swap(&newNode, &nextNode); + Standardize_101(&newNode); + Square(&nextNode); }
\ No newline at end of file |