1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <cmath>
double term( int i, double x, double a, double b, double c ) {
return std::pow( a*x - i*b , c ) ;
}
double SigmaFunction( int n, double x, double a, double b, double c ) {
double sum = 0.0 ;
for( int i = 1 ; i <= n ; ++i ) sum += term( i, a, x, b, c ) ;
return sum ;
}
bool do_another_calculation() {
char another ;
std::cout << "\nWould you like to do another calculation (Y/N)? ";
std::cin >> another ;
return another == 'Y' || another == 'y' ;
}
int main() {
char another;
do {
std::cout << "\nThis program will calculate the sum (ax - ib)^c, where i goes from 1 to n.\n"
<< "Enter the values for n, x, a, b, and c separated by spaces then press Enter.\n" ;
int n ;
double x, a, b, c;
std::cin >> n >> x >> a >> b >> c;
std::cout << "The sum of (" << a << "*" << x << " - i*" << b << ")^" << c
<< " where i goes from 1 to " << n << " is "
<< SigmaFunction(n, x, a, b, c) << '\n' ;
} while( do_another_calculation() );
}
|