diff --git a/basic/mean_modular.cpp b/basic/mean_modular.cpp index 05faaef334f0872d1d19e5d18fd7966cec10db72..e91daa6b42a9960ba5baec3018c289a62db3de67 100644 --- a/basic/mean_modular.cpp +++ b/basic/mean_modular.cpp @@ -1,34 +1,46 @@ +// An example of a modular procedure #include <iostream> +// The namespace, a module that stores any kind of entity namespace mtools{ +// An user defined type having just public data and no user defined operation struct avars{ int ncount; int rsum; double amean; }; +// A module implementing a procedure, that is a function +// This function operates on an object of the above user +// defined type and calculates the average void calcave(avars & a){ double stemp{static_cast<double>(a.rsum)}; double ntemp{static_cast<double>(a.ncount)}; a.amean=stemp/ntemp; } +// A function updating the state of an object of the above +// user defined type, incrementing the running sum and the number of counts void updateave(avars & a, int d){ a.rsum+=d; a.ncount++; } - +// A function that just print the value of the mean, stored into an object +// of the above defined type void printave(const avars & a){ std::cout <<"Arithmetic mean: " << a.amean<< std::endl; } } +// Every C++ program must start executing a function called main. int main(){ - +// Create object of type defined in the module namespace mtools mtools::avars a{}; int indata{0}; std::cout <<"Enter values [Ctrl-D to finish]: " <<std::endl; +// The procedure is now less bound to the chosen algorithm +// but it scale, thanks to the modularization. while(std::cin>>indata){ mtools::updateave(a,indata); mtools::calcave(a); diff --git a/basic/mean_procedure.cpp b/basic/mean_procedure.cpp index 78c9f1f2dad006eda6e2fe8a4b3c2715a3bc5104..71b0fb8022bf546e1daf456c71cb2468d50bde32 100644 --- a/basic/mean_procedure.cpp +++ b/basic/mean_procedure.cpp @@ -1,19 +1,28 @@ -#include <iostream> +// An example of procedural programming +#include <iostream> // For std::cout and std::cin declarations + +// All C++ program must start with a function called main() int main(){ +// Definition of variables int ncount{0},rsum{0},indata{0}; double amean{0.}; +// A printout std::cout <<"Enter values [Ctrl-D to finish]: " <<std::endl; +// The procedure +// First ask for data and read it while(std::cin>>indata){ rsum+=indata; // rsum=rsum+indata; ncount++; // ncount=ncount+1; + // Convert values into the right types double stemp=static_cast<double>(rsum); double ntemp=static_cast<double>(ncount); + // Do the calculation amean=(ntemp!=0)?stemp/ntemp:0.; } - +// Print the result std::cout <<"Arithmetic mean: " << amean<< std::endl; return 0; } diff --git a/basic/mean_procedure_auto.cpp b/basic/mean_procedure_auto.cpp index 3e58b16596ed369b9362c075fe88670bf7f461ae..d40e8e3dba3257b7cb20f763956152963caf43b3 100644 --- a/basic/mean_procedure_auto.cpp +++ b/basic/mean_procedure_auto.cpp @@ -1,22 +1,29 @@ -#include <iostream> +// An example of procedural programming using the type auto deduction +#include <iostream> // For std::cout and std::cin declarations + +// All C++ program must start with a function called main() int main(){ - auto ncount=0,rsum=0,indata=0; - auto amean=0.; - decltype(rsum/ncount) truncmean; +// Definition of variables + auto ncount{0},rsum{0},indata{0}; + auto amean{0.}; - std::cout <<"Enter values [Ctrl-D to finish]: \n" ; +// A printout + std::cout <<"Enter values [Ctrl-D to finish]: " <<std::endl; +// The procedure +// First ask for data and read it while(std::cin>>indata){ rsum+=indata; // rsum=rsum+indata; ncount++; // ncount=ncount+1; + // Convert values into the right types auto stemp=static_cast<double>(rsum); auto ntemp=static_cast<double>(ncount); + // Do the calculation amean=(ntemp!=0)?stemp/ntemp:0.; - truncmean=(ntemp!=0)?rsum/ncount:0; } - - std::cout <<"Arithmetic mean: " << amean<< '\n'; - std::cout <<"Rounded to int mean: " << truncmean<< std::endl; +// Print the result + std::cout <<"Arithmetic mean: " << amean<< std::endl; return 0; } + diff --git a/basic/mean_userdefined.cpp b/basic/mean_userdefined.cpp index b3a1c0c152abc11af4685f9fd7713963a73ee9f9..39158ca6fdc4bd6e3dde173a1bf6a45f45f1358f 100644 --- a/basic/mean_userdefined.cpp +++ b/basic/mean_userdefined.cpp @@ -1,9 +1,17 @@ +// An example of programming using user defined types #include <iostream> +// Here is the type definition, that is a declaration +// containing the type data layout and the operations +// needed to use the type in the correct way. +// The type is created using a class, that is a type that +// implement a separation between a public part, what every +// user can use, and a private part, what can be used only +// by object of the type itself. class ave{ public: - ave(): ncount_(0), rsum_(0), amean_(0) {}; - +// Here the modules, functions, needed to create an object in memory +// And operate on the data layout ave & operator+=(int d){ rsum_+=d; ncount_++; @@ -15,25 +23,31 @@ public: double get_ave() const { return amean_;} - private: - int ncount_; - int rsum_; - double amean_; - +// Here the list of data needed to describe +// the type. + int ncount_{0}; + int rsum_{0}; + double amean_{0.}; }; - +// Every C++ program must start executing a function called main. int main(){ - +// User's types have the same support of the base types +// Create an object with a state, i.e. data layout, and +// a behavior, i.e. the code needed to manage the data layout, +// connected somehow in memory. ave a; int indata{0}; std::cout <<"Enter values [Ctrl-D to finish]: " <<std::endl; +// The object oriented programming semantic is implemented in C++ using +// operators, that is symbols that represent an operation. Here std::cin and a +// are the "object"s to operate on ... while(std::cin>>indata){ a+=indata; } - +// ... here the std::cout is the "object" to "use" std::cout << "Average: " << a.get_ave() << std::endl; return 0; }