ch06 was changed

This commit is contained in:
davoudn 2024-04-23 11:55:49 +03:30
parent 2e54dfadfe
commit 1fc5123d88
3 changed files with 94 additions and 58 deletions

Binary file not shown.

View File

@ -1,62 +1,41 @@
// fig06_01.cpp //variable scope
// Initializing an array's elements to zeros and printing the array. #include<iostream>
//#include <../libraries/fmt/include/format.h> // C++20: This will be #include <format> //global variable
#include <iostream> //local variable
#include <array> int k=0;
#include <vector> //function
#include <string> //array
//struct
template <typename T> //class
void printArray(T& a, std::string tag){ //Macro
std::cout << "Element Value ---> "<<tag <<"\n" ; int main()
{ //block
// output each array element's value cout<<k;
for (size_t i{0}; i < a.size(); ++i) { int k = 2;
std::cout << i << a[i]<<"\n"; int j=0;
{
int i=0;
} }
cout<<k;
cout<<::k;
} }
int main() { //definition
std::array<int, 5> values{}; // values is an array of 5 int values short 2B
std::array<double,10> floatarray; unsigned int 4B
std::vector<double> floatvector(20, 0); signed long 8B
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
}
printArray(values,"values"); 1B = 0xFF
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<<values.at(i) <<"\n";
}
/* for (auto i : values){
std::cout << i << values.at(i)<<"\n";
}
*/
// accessing an element outside the array's bounds with at
// values.at(10); // throws an exception
}
/************************************************************************** float
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and * double
* Pearson Education, Inc. All Rights Reserved. *
* * unsigned char
* DISCLAIMER: The authors and publisher of this book have used their * string
* best efforts in preparing the book. These efforts include the * bool
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make * int i=10;
* no warranty of any kind, expressed or implied, with regard to these * //initialization
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or * long b;
* consequential damages in connection with, or arising out of, the * b=1200;
* furnishing, performance, or use of these programs. * //assignement
**************************************************************************/

View File

@ -0,0 +1,57 @@
#include <iostream>
#include <array>
#include <vector>
constexpr size_t rows{4};
constexpr size_t columns{4};
void MatVecMul(const std::array<std::array<int, columns>, rows>& A, const std::array<int,columns>& V, std::array<int,rows>& B);
template <typename T, long unsigned int N>
void MatVecMul_template(const std::array<std::array<T, N>, N>& A, const std::array<T,N>& V, std::array<T,N>& B);
template <typename T, typename U>
void MatVecMul_templatefull(const T& A, const U& V, U& B);
template <typename T>
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 <int,3> B{};
MatVecMul_template(A, B, V);
return 0;
}
void MatVecMul(const std::array<std::array<int, columns>, rows>& A, const std::array<int,columns>& V, std::array<int,rows>& 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 <typename T, long unsigned int N>
void MatVecMul_template(const std::array<std::array<T, N>, N>& A, const std::array<T,N>& V, std::array<T,N>& 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 <typename T>
void MatVecMul_template(const std::vector<std::vector<T>>& A, const std::vector<T>& V, std::vector<T>& 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 <typename T, typename U>
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!";
}
}