diff options
author | Oskar <[email protected]> | 2024-08-11 20:59:53 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-08-11 20:59:53 +0200 |
commit | 06615448f1fd8378817e8ef72b5347ae3f0c3577 (patch) | |
tree | fc89a7edaee13953211d006a9c1a428dd279247c | |
parent | b45c0d29ee690c8b436a33581d3b108e6064b5a7 (diff) |
more
-rw-r--r-- | 3p27.cpp | 37 | ||||
-rw-r--r-- | 3p28.cpp | 30 | ||||
-rw-r--r-- | 3p29.cpp | 31 | ||||
-rw-r--r-- | 3p30.cpp | 65 | ||||
-rw-r--r-- | 3p31.cpp | 40 | ||||
-rw-r--r-- | 3p32.cpp | 59 | ||||
-rw-r--r-- | 3p33.cpp | 43 | ||||
-rw-r--r-- | array-test.cpp | 32 |
8 files changed, 337 insertions, 0 deletions
diff --git a/3p27.cpp b/3p27.cpp new file mode 100644 index 0000000..de4f9d2 --- /dev/null +++ b/3p27.cpp @@ -0,0 +1,37 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.27 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; + +int text_size() { + + return 9900; +} + +int main () { + + //unsigned buf_size = 1024; + //int ia1[buf_size]; // illegal + int ia2[4 * 7 - 14]; // legal + //int ia3[text_size()]; // illegal, would be legal if text_size returned constexpr + //char st[11] = "fundamental"; // illegal + + ia2[0] = 1; + cout << ia2[0] << endl; + return 0; +} diff --git a/3p28.cpp b/3p28.cpp new file mode 100644 index 0000000..14f617a --- /dev/null +++ b/3p28.cpp @@ -0,0 +1,30 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.28 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; + +string sa[10]; // Default initialized strings +int ia[10]; // ints with 0 +int main() { + + string sa2[10]; // Default initialized strings + //int ia2[10]; // Unititialized, UB + + cout << sa[0] << "|" << ia[0] << "|" << sa2[0] << endl; + return 0; +} diff --git a/3p29.cpp b/3p29.cpp new file mode 100644 index 0000000..d219ef1 --- /dev/null +++ b/3p29.cpp @@ -0,0 +1,31 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.29 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; + +int main() { + + /* + - We need to know the size of an array when we create it + - We cannot initialize an array with another array and we cannot assign an array to another + - We cannot increase an array's size like we can with vectors. + - character arrays need space for the null character + + */ + return 0; +} diff --git a/3p30.cpp b/3p30.cpp new file mode 100644 index 0000000..68d4cdd --- /dev/null +++ b/3p30.cpp @@ -0,0 +1,65 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.30 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; + +int main() { + /* + constexpr size_t array_size = 10; + int ia[array_size]; + for (size_t ix = 1; ix <= array_size; ++ix) { + ia[ix] = ix; + } + + In the for loop: + ix should start at 0 rather than 1. Because when we use subscripts it starts at 0. + In this case, if we were to start with '1' then the first element in the array would remain uninitialized. + + ix should also not be compared to array sized using <= + array_size only tells us how many elements the array has, but when using subscripts we start at 0, so this means + we would need to compare to array_size-1 or use just the '<' operator, because then we also check at array_size-1 + + If we want to nitpick about the requirements of what the program is supposed to do we could change even more. + Maybe we want to start at 1 and get an array of 1 - 10? + */ + + // Fixed version here, fills array with 0 - 9 + constexpr size_t array_size = 10; + int ia[array_size]; + for (size_t ix = 0; ix < array_size; ++ix) { + ia[ix] = ix; + } + + // Same program but if we wanted the array to be from 1 - 10 + constexpr size_t array_size2 = 10; + int ia2[array_size2]; + for (size_t ix2 = 0; ix2 < array_size2; ++ix2) { + ia2[ix2] = ix2 + 1; + } + + for(auto i : ia) { + cout << i << endl; + } + + cout << endl; + for(auto i : ia2) { + cout << i << endl; + } + + return 0; +} diff --git a/3p31.cpp b/3p31.cpp new file mode 100644 index 0000000..0e7970b --- /dev/null +++ b/3p31.cpp @@ -0,0 +1,40 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.31 + * + * I mean this was really easy because i just remembered what + * i'd already seen in the last exercise... + * I made one more like the one in the book and one that's + * a little bit more simpler. Not sure how 'correct' the simpler + * version is. Who knows... + */ + +int main () { + + constexpr size_t ArraySize = 10; + int MyArray[ArraySize]; + for(size_t Index = 0 ; Index != ArraySize ; ++Index) { + MyArray[Index] = Index; + } + + int MyArray2[10]; + for(int Index = 0 ; Index != 10 ; ++Index) { + MyArray2[Index] = Index; + } + + for(auto i : MyArray) { + std::cout << i << std::endl; + } + + std::cout << std::endl; + for(auto i : MyArray2) { + std::cout << i << std::endl; + } + + return 0; +} diff --git a/3p32.cpp b/3p32.cpp new file mode 100644 index 0000000..2510e21 --- /dev/null +++ b/3p32.cpp @@ -0,0 +1,59 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.32 + * + * + */ + +int main () { + + constexpr size_t ArraySize = 10; + int MyArray[ArraySize]; + int MyArray2[ArraySize]; + for(size_t Index = 0 ; Index != ArraySize ; ++Index) { + MyArray[Index] = Index; + } + + for(size_t Index = 0 ; Index != 10 ; ++Index) { + MyArray2[Index] = MyArray[Index]; + } + + for(auto i : MyArray) { + std::cout << i << std::endl; + } + + std::cout << std::endl; + for(auto i : MyArray2) { + std::cout << i << std::endl; + } + + std::cout << std::endl; + + // Vector version + size_t VecSize = 10; + std::vector<int> MyVec; + std::vector<int> MyVec2; + for(size_t Index = 0 ; Index != VecSize ; ++Index) { + MyVec.push_back(Index); + } + + MyVec2 = MyVec; + // If we wanted to we could also initialize MyVec2 here instead of above + // std::vector<int> MyVec2 = MyVec; + + for(auto i : MyVec) { + std::cout << i << std::endl; + } + + std::cout << std::endl; + for(auto i : MyVec2) { + std::cout << i << std::endl; + } + + return 0; +} diff --git a/3p33.cpp b/3p33.cpp new file mode 100644 index 0000000..96d0105 --- /dev/null +++ b/3p33.cpp @@ -0,0 +1,43 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.33 + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; +int main () { + /* + Because we assume all score clusters to start at a value of 0 when we increment, + we need to initialize them with 0 because if we did not then each array element + could have any value because it is uninitialized. + + When we compile the program and run, we can see the uninitialized values get + printed. + */ + unsigned scores[11]; + unsigned grade; + while (cin >> grade) { + if (grade <= 100) { + ++scores[grade/10]; + } + } + + for (auto i : scores) { + cout << i << " "; + } + + cout << endl; + return 0; +} diff --git a/array-test.cpp b/array-test.cpp new file mode 100644 index 0000000..dd58435 --- /dev/null +++ b/array-test.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * + * + * + */ + +using std::string; +using std::cout; +using std::cin; +using std::cerr; +using std::clog; +using std::endl; +using std::vector; +int main () { + + constexpr int isz = 10; + int fff[isz]; + fff[0] = 304234; + cout << fff[0] << endl; + + int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; + int (*arr_p)[10] = &arr; + cout << &(*arr_p)[2] << " " << &arr[2] << endl; + // This syntax is crazy! + return 0; +} |