From 06615448f1fd8378817e8ef72b5347ae3f0c3577 Mon Sep 17 00:00:00 2001 From: Oskar Date: Sun, 11 Aug 2024 20:59:53 +0200 Subject: more --- 3p30.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 3p30.cpp (limited to '3p30.cpp') 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 +#include +#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; +} -- cgit v1.2.3