diff options
author | Oskar <[email protected]> | 2024-08-12 17:13:23 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-08-12 17:13:23 +0200 |
commit | 0863591e1aa4a172e376dcdc9c3e0d2c39a63019 (patch) | |
tree | 38289669b386ba7a31610ad46cbf921652336195 | |
parent | 4555acdaeae73b8c2d49696c9766366ead8eb847 (diff) |
more
-rw-r--r-- | 3p34.cpp | 28 | ||||
-rw-r--r-- | 3p35.cpp | 43 | ||||
-rw-r--r-- | 3p36.cpp | 69 | ||||
-rw-r--r-- | array-test.cpp | 54 |
4 files changed, 185 insertions, 9 deletions
diff --git a/3p34.cpp b/3p34.cpp new file mode 100644 index 0000000..84d148b --- /dev/null +++ b/3p34.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.34 + * + * + */ + +int main () { + + int ia[] = {1,2,3,4,5,6,7,8,9,10}; + int *p1 = ia + 12; + int *p2 = ia + 0; + // Well as long as p2 is pointing to a valid element in the array it seems like + // changing values in p1 cant make it illegal. + // Though if i change p2 to point to -1 or lower then i get a buffer underflow + + // -12 + // p1 = -12 + // p1[-12] = ia[0] + p1 += p2 - p1; + std::cout << *p1 << " " << *p2 << std::endl; + return 0; +} diff --git a/3p35.cpp b/3p35.cpp new file mode 100644 index 0000000..b24d099 --- /dev/null +++ b/3p35.cpp @@ -0,0 +1,43 @@ +#include <iostream> +#include <vector> +#include <iterator> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.35 + * + * + */ + +int main () { + + int ia[50]; // We make it uninitialized and print it so we can easily see before and after + for(auto r : ia) { + std::cout << r << " "; + } + + std::cout << std::endl; + /* + // Different loops we can use, commented out here is using a while loop + // And uncommented further below is using a for loop + + auto p_begin = std::begin(ia); + auto p_end = std::end(ia); + while(p_begin != p_end) { + *p_begin = 0; + ++p_begin; + } + */ + for(auto p_beg = std::begin(ia) ; p_beg != std::end(ia) ; ++p_beg) { + *p_beg = 0; + } + + for(auto r : ia) { + std::cout << r << " "; + } + + std::cout << std::endl; + return 0; +} diff --git a/3p36.cpp b/3p36.cpp new file mode 100644 index 0000000..3928de5 --- /dev/null +++ b/3p36.cpp @@ -0,0 +1,69 @@ +#include <iostream> +#include <vector> +#include <iterator> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.36 + * + * I found a little bit of strangeness when comparing the vectors with '==' + * If i input for example 5 1's to the first vector + * and then i enter maybe 2 1's to the 2nd vector + * and then afterwards hit CTRL+D, the vector evaluates + * as equal... Not sure if it's supposed to be like this, they clearly aren't the same... + */ + +int main () { + + constexpr size_t ArrSize = 5; + int arr1[ArrSize]; + int arr2[ArrSize]; + std::cout << "Input " << ArrSize << " numbers: " << std::endl; + for(auto &a : arr1) { + if(std::cin >> a) {} else { return -1; } + } + + std::cout << "Input " << ArrSize << " numbers: " << std::endl; + for(auto &a : arr2) { + if(std::cin >> a) {} else { return -1; } + } + + bool f = true; + for(size_t ii = 0 ; ii != ArrSize ; ++ii) { + if(arr1[ii] != arr2[ii]) { + std::cout << "The arrays are not equal." << std::endl; + f = false; + break; + } + } + + if(f == true) { + std::cout << "The arrays are equal." << std::endl; + } + + int a = 0; + std::vector<int> vec1; + std::vector<int> vec2; + std::cout << "Input " << ArrSize << " numbers: " << std::endl; + for(decltype(vec1.size()) i = 0 ; i != ArrSize ; ++i) { + if(std::cin >> a) {} else { break; } + vec1.push_back(a); + } + + std::cout << "Input " << ArrSize << " numbers: " << std::endl; + for(decltype(vec2.size()) i = 0 ; i != ArrSize ; ++i) { + if(std::cin >> a) {} else { break; } + vec2.push_back(a); + } + + if(vec1 != vec2) { + std::cout << "The vectors are not equal." << std::endl; + return 0; + } else if (vec1 == vec2) { + std::cout << "The vectors are equal." << std::endl; + } + + return 0; +} diff --git a/array-test.cpp b/array-test.cpp index dd58435..10d4708 100644 --- a/array-test.cpp +++ b/array-test.cpp @@ -1,5 +1,6 @@ #include <iostream> #include <vector> +#include <iterator> #include "sales_data.hpp" #include "sales_item.hpp" @@ -10,23 +11,58 @@ * */ -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; + std::cout << fff[0] << std::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; + std::cout << &(*arr_p)[2] << " " << &arr[2] << std::endl; // This syntax is crazy! + + int ia[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + int *pia = ia; + int *pia2 = &ia[0]; + auto apia = ia; + apia[9] = 100; + std::cout << pia << " " + << pia2 << " " + << apia << " " + << apia[9] << " " + << std::endl; + + int ar[] = {0,1,2,3,4,5,6,7,8,9}; + int *pbegin = ar; + int *pend = ar + 10; + for( ; pbegin != pend ; ++pbegin) { // Wow look! I've basically made my own iterators! + std::cout << *pbegin << "."; + } + + std::cout << std::endl; + for(auto a : ar) { + std::cout << a << ","; + } + + std::cout << std::endl; + signed long long int arre[] = {4,547,2365,35,65,2456,244,78,5,33354,0,111,122432,253,32323,-13432,-233333333}; + auto p_beg = std::begin(arre); + auto p_end = std::end(arre); + for( ; p_beg != p_end ; ++p_beg) { + *p_beg *= 2; + } + + for(auto p : arre) { + std::cout << p << " "; + } + + std::cout << std::endl; + + int hhh[] = {1,344,224,465333,553,24,12,234,243,2}; + int hhhh = *(hhh + 4); + int h4 = *hhh + 10; // 11 + std::cout << hhhh << " " << h4 << std::endl; return 0; } |