diff --git a/CPlusPlus20ForProgrammers-master/examples/ch06/a.out b/CPlusPlus20ForProgrammers-master/examples/ch06/a.out new file mode 100755 index 0000000..f625659 Binary files /dev/null and b/CPlusPlus20ForProgrammers-master/examples/ch06/a.out differ diff --git a/CPlusPlus20ForProgrammers-master/examples/ch06/fig06_01.cpp b/CPlusPlus20ForProgrammers-master/examples/ch06/fig06_01.cpp index b43e941..86fadd8 100644 --- a/CPlusPlus20ForProgrammers-master/examples/ch06/fig06_01.cpp +++ b/CPlusPlus20ForProgrammers-master/examples/ch06/fig06_01.cpp @@ -1,62 +1,41 @@ -// fig06_01.cpp -// Initializing an array's elements to zeros and printing the array. -//#include <../libraries/fmt/include/format.h> // C++20: This will be #include -#include -#include -#include -#include - -template -void printArray(T& a, std::string tag){ - std::cout << "Element Value ---> "< +//global variable +//local variable +int k=0; +//function +//array +//struct +//class +//Macro +int main() +{ //block + cout< values{}; // values is an array of 5 int values - std::array floatarray; - std::vector floatvector(20, 0); - for (size_t i{0}; i < floatarray.size(); i++) - floatarray[i] = i*i/10.0; - - // initialize elements of array values to 0 - for (size_t i{0}; i < values.size(); ++i) { - values[i] = i*i; // set element at location i to 0 - } +//definition +short 2B +unsigned int 4B +signed long 8B - printArray(values,"values"); - printArray(floatarray,"floatarray"); - printArray(floatvector, "floatvector"); - std::cout << "Element Value\n" << "\n"; - - floatvector.resize(5); - printArray(floatvector,"floatvector resized"); - // access elements via the at member function - for (size_t i{0}; i < values.size(); ++i) { - std::cout << i< +#include +#include + +constexpr size_t rows{4}; +constexpr size_t columns{4}; +void MatVecMul(const std::array, rows>& A, const std::array& V, std::array& B); + +template +void MatVecMul_template(const std::array, N>& A, const std::array& V, std::array& B); + +template +void MatVecMul_templatefull(const T& A, const U& V, U& B); + +template +void printArrayTemplate(const T& a); + +int main() { + std::array A{std::array{1, 2, 3}, std::array{4, 5, 6},std::array{4, 5, 0}}; + std::array V{1, 2, 3,5}; + std::array B{}; + MatVecMul_template(A, B, V); + + return 0; +} + +void MatVecMul(const std::array, rows>& A, const std::array& V, std::array& B){ + for (int i{0}; i < A.size(); i++) + for (int j{0}; j < A[0].size(); j++) + B[i]+= A[i][j] * V[j]; +} + +template +void MatVecMul_template(const std::array, N>& A, const std::array& V, std::array& B){ + for (int i{0}; i < A.size(); i++) + for (int j{0}; j < A[0].size(); j++) + B[i]+= A[i][j] * V[j]; +} + +template +void MatVecMul_template(const std::vector>& A, const std::vector& V, std::vector& B){ + for (int i{0}; i < A.size(); i++) + for (int j{0}; j < A[0].size(); j++) + B[i]+= A[i][j] * V[j]; +} + +template +void MatVecMul_templatefull(const T& A, const U& V, U& B){ + if (A[0].size() == V.size() && A.size() == B.size() ){ + for (int i{0}; i < A.size(); i++) + for (int j{0}; j < A[0].size(); j++) + B[i]+= A[i][j] * V[j]; + } + else{ + std::cout << "sizes are not compatible!"; + } +} \ No newline at end of file