A
arnuld
this programme work fine. any suggestions from you:
/* C++ Primer - 4/e
*
* exercise 7.13
* STATEMENT:
* write a programme to calculate the sum of the elements of
vector<double>
*
*/
#include <iostream>
#include <vector>
double sum_vector( std::vector<double> &dvec ) {
double sum = 0;
for( std::vector<double>::const_iterator iter=dvec.begin();
iter != dvec.end();
++iter )
{
sum += *iter;
}
return sum;
}
int main()
{
std::vector<double> dvec;
std::cout << "Enter some floating-point numbers: "; double d;
while( std::cin >> d )
{
dvec.push_back(d);
}
std::cout << "sum of all numbers = "
<< sum_vector( dvec )
<< std::endl;
return 0;
}
/* C++ Primer - 4/e
*
* exercise 7.13
* STATEMENT:
* write a programme to calculate the sum of the elements of
vector<double>
*
*/
#include <iostream>
#include <vector>
double sum_vector( std::vector<double> &dvec ) {
double sum = 0;
for( std::vector<double>::const_iterator iter=dvec.begin();
iter != dvec.end();
++iter )
{
sum += *iter;
}
return sum;
}
int main()
{
std::vector<double> dvec;
std::cout << "Enter some floating-point numbers: "; double d;
while( std::cin >> d )
{
dvec.push_back(d);
}
std::cout << "sum of all numbers = "
<< sum_vector( dvec )
<< std::endl;
return 0;
}