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