Skip to content
Snippets Groups Projects
mean_procedure_auto.cpp 834 B
Newer Older
// 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()
// Definition of variables
  auto ncount{0},rsum{0},indata{0};
  auto 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
    auto stemp=static_cast<double>(rsum);
    auto ntemp=static_cast<double>(ncount);
    // Do the calculation
    amean=(ntemp!=0)?stemp/ntemp:0.;
  }
// Print the result
  std::cout <<"Arithmetic mean: " << amean<< std::endl;