-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSeries.cpp
More file actions
59 lines (56 loc) · 1.66 KB
/
Series.cpp
File metadata and controls
59 lines (56 loc) · 1.66 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//MingkuanPang
//s. The program can read the first term (a) and the common ratio (r) for the
//series, then it will caculate the sum in two ways, if both sums agree with the formula to 7 significant
//digits. The sums will be printed.
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
bool if_equal(double, double);
double Sum_Series(double, double);
double Series_Sum(double, double);
int main()
{
double first_term, common_ration, sum_1, sum_2;
cout << "Please enter the first term: ";
cin >> first_term;
cout << "Please enter your common ration: ";
cin >> common_ration;
sum_1 = Sum_Series(first_term, common_ration);
sum_2 = Series_Sum(first_term, common_ration);
cout.setf(ios::fixed);
cout.precision(10);
cout << "The sum by caculated is: " << sum_1 << endl;
cout << "The sum by formula is: " << sum_2 << endl;
return 0;
}
double Sum_Series(double first_term, double common_ration)//This function can caculate a sum of a series of numbers.
{
double sum_1 = 0, sum_2;
if (-1<common_ration < 1)
{
sum_2 = Series_Sum(first_term, common_ration);
while (!if_equal(sum_1, sum_2))
{
sum_1 += first_term;
first_term *= common_ration;
}
}
return sum_1;
}
double Series_Sum(double first_term, double common_ration)//This function can caculate a sum of a series of numbers by formula.
{
double sum=0;
if (-1<common_ration < 1)
sum = first_term / (1 - common_ration);
else
cout << "The comman_ration must less then 1!" << endl;
return sum;
}
bool if_equal(double sum_1, double sum_2)// This function can judge if two numbers are equal.
{
bool equal = false;
if (fabs(sum_1 - sum_2) < fabs(sum_1*(1e-7)))
equal = true;
return equal;
}