some changes

This commit is contained in:
davoudn 2024-05-07 12:02:03 +03:30
parent 5a612f8354
commit db792e6fc1
4 changed files with 30 additions and 7 deletions

View File

@ -0,0 +1,16 @@
#include <iostream>
#include <vector>
using namespace std;
int main (){
double matris[5][6];
int number_of_start = 10;
for(int row_counter=1; row_counter <= number_of_start ; row_counter++){
for(int star_counter=0; star_counter < row_counter ; star_counter++)
cout << '*';
cout << endl;
}
return 0;
}

View File

@ -10,6 +10,10 @@ T multiply(T x, T y) {
} }
auto multiply_auto(auto x, auto y) {
return x * y;
}
int multiply_int(int x, int y) { int multiply_int(int x, int y) {
return x * y; return x * y;
} }
@ -20,7 +24,7 @@ double f(double& x){
} }
template <typename T, typename F, long unsigned int N> template <typename T, typename F, long unsigned int N>
T integrate (std::array<T, N> x, F f){ T integrate (std::array<T, N>& x, F f){
T tmp{T(0)}; T tmp{T(0)};
for (auto s: x){ for (auto s: x){
tmp+= f(s); tmp+= f(s);
@ -31,10 +35,11 @@ T integrate (std::array<T, N> x, F f){
auto integrate_auto (auto x, auto f){ auto integrate_auto (auto x, auto f){
auto tmp{x[0]}; auto tmp{x[0]};
tmp = 0.0;
for (auto s: x){ for (auto s: x){
tmp+= f(s); tmp+= f(s);
} }
tmp-=x[0];
return tmp; return tmp;
} }
@ -43,7 +48,7 @@ typedef double (*FUNC)(double&);
template <typename T, long unsigned int N> template <typename T, long unsigned int N>
T integrate2 (std::array<T, N> x, FUNC f){ T integrate_legacy (std::array<T, N> x, FUNC f){
T tmp{T(0)}; T tmp{T(0)};
for (auto s: x){ for (auto s: x){
tmp+= f(s); tmp+= f(s);
@ -69,6 +74,8 @@ int main() {
std::cout << "Product of integers: " << std::accumulate( std::begin(integers), std::end(integers), 1, multiply<int>) << "\n"; std::cout << "Product of integers: " << std::accumulate( std::begin(integers), std::end(integers), 1, multiply<int>) << "\n";
//std::cout << "Product of integers: " << std::accumulate( std::begin(integers), std::end(integers), 1, multiply_auto) << "\n";
std::cout << "Product of integers: " << std::accumulate( integers.cbegin(), integers.cend(), 1, multiply<int>) << "\n"; std::cout << "Product of integers: " << std::accumulate( integers.cbegin(), integers.cend(), 1, multiply<int>) << "\n";
std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(integers), std::end(integers), 1, multiply_lambda) << "\n"; std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(integers), std::end(integers), 1, multiply_lambda) << "\n";
@ -77,9 +84,9 @@ int main() {
std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(floats), std::end(floats), 1, multiply_lambda) << "\n"; std::cout << "Product of integers with a lambda: " << std::accumulate(std::begin(floats), std::end(floats), 1, multiply_lambda) << "\n";
std::cout << "Integration2 template " << integrate2(floats, f) << "\n"; std::cout << "Integration2 template " << integrate_legacy(floats, f_lambda) << "\n";
std::cout << "Integration full template " << integrate(floats, f_lambda) << "\n"; std::cout << "Integration full template " << integrate(floats, f) << "\n";
std::cout << "Integration auto " << integrate_auto(floats, f_lambda) << "\n"; std::cout << "Integration auto " << integrate_auto(floats, f_lambda) << "\n";

View File

@ -25,12 +25,12 @@ int main() {
double lower_bound = 0; double lower_bound = 0;
std::uniform_real_distribution<double> randomReal{lower_bound, upper_bound}; std::uniform_real_distribution<double> randomReal{lower_bound, upper_bound};
constexpr size_t N{10}; constexpr size_t N{4};
double del {(upper_bound-lower_bound)/N}; double del {(upper_bound-lower_bound)/N};
std::array<double, N> histogram{}; std::array<double, N> histogram{};
// roll die 60,000 times; use die value as frequency index // roll die 60,000 times; use die value as frequency index
for (int roll{1}; roll <= 60'000; ++roll) { for (int roll{1}; roll <= 6000; ++roll) {
double val{ randomReal(engine)}; double val{ randomReal(engine)};
accumulate<N>(histogram, val, lower_bound, upper_bound); accumulate<N>(histogram, val, lower_bound, upper_bound);
} }