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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4
5 //equations
6 double f1(double x){
7 return ((2*pow(x,5))+(pow(x,3))-(10*x)+(2));
8 }
9
10 double f2(double x){
11 return ((6*pow(x,2))-(x)+(10));
12 }
13
14 double f3(double x){
15 return ((5*x)+(3));
16 }
17
18 double f4(double x){
19 return ((2*pow(x,3))+(120));
20 }
21
22 double f5(double x){
23 return (2*pow(x,2));
24 }
25
26
27
28
29 //this function calculates the area of rectangles and adds them up
30 double rect_area(double a, double b, double n, int func_choice){
31 double area =0;
32 double x=0;
33 double width;
34 width =((b-a)/n);
35
36 for(int i=1; i<=n; i++){
37 x+=width;
38 if(func_choice==1){
39 area += (f1(x) * width);
40 }
41 if(func_choice==2){
42 area+= (f2(x) * width);
43 }
44 if(func_choice==3){
45 area+= (f3(x) * width);
46 }
47 if(func_choice==4){
48 area+= (f4(x) * width);
49 }
50 if(func_choice==5){
51 area+= (f5(x) * width);
52 }
53 }
54 return area;
55 }
56
57 //this function calculates the area of trapezoids and adds them up
58 double trap_area(double a, double b, double n, int func_choice){
59 double area = 0;
60 double x = 0;
61 double width;
62 width = ((b-a)/n);
63
64 for(int i; i<=n; i++){
65 x+=width;
66 if(func_choice==1){
67 area += (f1(x) * width);
68 }
69 if(func_choice==2){
70 area+= (f2(x) * width);
71 }
72 if(func_choice==3){
73 area+= (f3(x) * width);
74 }
75 if(func_choice==4){
76 area+= (f4(x) * width);
77 }
78 if(func_choice==5){
79 area+= (f5(x) * width);
80 }
81 }
82 return area;
83 }
84
85
86 int main(){
87 int reset;
88 int func_choice;
89 double a, b;
90 int method;
91 double rect_num;
92 double trap_num;
93 double width;
94 double area = 0;
95
96 //do while loop to repeat if user wants
97 do{
98 //user picks a function
99 cout << "Choose a function(1,2,3,4,5,other(quit)): " << endl;
100 cout << "f1(x)=2x^5 + x^3 - 10x +2" << endl;
101 cout << "f2(x)=6x^2 -x + 10" << endl;
102 cout << "f3(x)=5x + 3 " << endl;
103 cout << "f4(x)=2x^3 +120 " << endl;
104 cout << "f5(x)=2x^2 " << endl;
105 cin >> func_choice;
106
107 //if user chooses something other than a given number, program quits
108 if(func_choice < 1 || func_choice > 5){
109 return 0;
110 }
111
112 //method of integration
113 cout <<"Which method would you like to use?(rectangle(1), trapezoid(2), both(3)): " << endl;
114 cin >> method;
115
116 //starting values
117 cout <<"Input a start value: " << endl;
118 cin >> a;
119 cout <<"Input an end value: " << endl;
120 cin >> b;
121
122 //calculates area based on users choice
123 switch(method){
124 case 1:
125 cout <<"How many rectangles would you like?" << endl;
126 cin >> rect_num;
127 area = rect_area(a,b,rect_num,func_choice);
128 cout << "The area of " << func_choice << " is " << area << " from " << a << " to " << b << " with " << rect_num << " rectangles " << endl;
129 break;
130
131 case 2:
132 cout <<"How many trapezoids would you like?" << endl;
133 cin >> trap_num;
134 area = trap_area(a,b,trap_num,func_choice);
135 cout << "The area of " << func_choice << " is " << area << " from " << a << " to " << b << " with " << trap_num << " trapezoids " << endl;
136 break;
137
138 case 3:
139 cout <<"How many rectangles would you like?" << endl;
140 cin >> rect_num;
141 cout <<"How many trapezoids would you like?" << endl;
142 cin >> trap_num;
143
144 area = rect_area(a,b,rect_num,func_choice);
145 cout << "The area of " << func_choice << " is " << area << " from " << a << " to " << b << " with " << rect_num << " rectangles " << endl;
146 area = trap_area(a,b,trap_num,func_choice);
147 cout << "The area of " << func_choice << " is " << area << " from " << a << " to " << b << " with " << trap_num << " trapezoids " << endl;
148 break;
149
150 default:
151 while(method<1, method>3){
152 cout << "not a valid input" << endl;
153 cout << "please choose a given method" << endl;
154 method =0;
155 cin >> method;
156 }
157 }
158
159 //question to recieve input for the condition of the do while loop
160 cout << "would you like to compute another area?(1 for yes, 0 for no)" << endl;
161 cin >> reset;
162
163 }while(reset==1);
164 return 0;
165 }
|