Area under a curve for multiple functions

Alright guys, I have been all over trying to find something to help me, and while there are lots of "area under a curve" topics, there aren't any that discuss how to do it for multiple functions.

My assignment is to find the area under a curve for five different functions. The user gets to choose the function, the starting and ending points, and whether they want to calculate it with rectangles, trapezoids, or both.

I am struggling with the very end. everything is running like i need it to be, except for my end output. I need help with how to get a certain output based on the function that the user inputs!

This is due VERY soon, so immediate help would be awesome, and I most likely won't be needing any more help after tomorrow. THANK YOU SO MUCH!

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

  1 #include <iostream>
  2 #include <cmath>
  3 #include <string>
  4
  5 using namespace std;
  6 double func_1 (double x);
  7 double func_2 (double x);
  8 double func_3 (double x);
  9 double func_4 (double x);
 10 double func_5 (double x);
 11 double f(double x);
 12
 13 int main () {
 14 int function, area_method, n, answer;
 15 double a, b, area, w, h, x;
 16
 17
 18    cout << "Would you like to find the area under a function?(1 for yes and 2 for no)" << endl;
 19    cin >> answer;
 20    while(answer==2){
 21       return 0;
 22    }
 23
 24    while(answer==1){
 25
 26       cout << "Choose a function (1, 2, 3, 4, 5): " << endl;
 27       cin >> function;
 28
 29       cout << "Would you like to calculate the area using the rectangle, trapezoid, or both (1, 2, 3):" << endl;
 30       cin >> area_method;
 31
 32       if(area_method == 1){
 33         cout << "What is the left x value? " << endl;
 34         cin >> a;
 35         cout << "What is the right x value? " << endl;
 36         cin >> b;
 37         cout << "How many pieces are there? " << endl;
 38         cin >> n;
 39
 40         w = (b-a)/n;
 41
 42         h = f(x);
 43
 44         for(int i=1; i<n; i++) {
 45            h += f(a+i*w);
 46
 47
 48         area = h*w;
 49
 50        while (function==5){
 51           cout << "The area under the curve of f5(x) is " << area <<endl;
 52              return 2*x*x;
 53           }
 54        while(function==4) {
 55           cout << "The area under the curve of f4(x) is " << area << endl;
 56              return 2*x*x*x+120;
 57           }
 58        while(function==3) {
 59           cout << "The area under the curve of f3(x) is " << area << endl;
 60              return 5*x+3;
 61           }
 62        while(function==2) {
 63           cout << "The area under the curve of f2(x) is " << area << endl;
 64              return 6*x*x-x+10;
 65           }
 66        while(function==1) {
 67           cout << "The area under the curve of f1(x) is " << area << endl;
 68              return 2*x*x*x*x*x+x*x*x-10*x+2;
 69           }
 70       }
 71       }
 72       else if(area_method == 2){
 73
 74          cout << "What is the left x value? " << endl;
 75          cin >> a;
 76          cout << "What is the right x value? " << endl;
 77          cin >> b;
 78          cout << "How many pieces are there? " << endl;
 79          cin >> n;
 80
 81          w = (b-a)/n;
 82
 83          h = (f(a)+f(b))/2;
 84
 85          for(int i=1; i<n; i++) {
 86             h += f(a+i*w);
 87          }
 88
 89          area = h*w;
 90            while (function==5){
 91               cout << "The area under the curve of f5(x) is " << area << endl;
 92                  return 2*x*x;
 93               }
 94            while(function==4) {
 95               cout << "The area under the curve of f4(x) is " << area << endl;
 96                  return 2*x*x*x+120;
 97               }
 98            while(function==3) {
 99               cout << "The area under the curve of f3(x) is " << area << endl;
100                  return 5*x+3;
101               }
102            while(function==2) {
103               cout << "The area under the curve of f2(x) is " << area << endl;
104                  return 6*x*x-x+10;
105               }
106            while(function==1) {
107               cout << "The area under the curve of f1(x) is " << area << endl;
108                  return 2*x*x*x*x*x+x*x*x-10*x+2;
109               }
110          }
111       else {
112          cout << "What is the left x value? " << endl;
113          cin >> a;
114          cout << "What is the right x value? " << endl;
115          cin >> b;
116          cout << "How many pieces are there? " << endl;
117          cin >> n;
118
119          w = (b-a)/n;
120
121          h = f(x);
122
123          for(int i=1; i<n; i++) {
124            h += f(a+i*w);
125          }
126
127          area = h*w;
128
129          while (function==5){
130             cout << "The area under the curve of f5(x) is " << area << endl;
131                return 2*x*x;
132             }
133          while(function==4) {
134             cout << "The area under the curve of f4(x) is " << area << endl;
135                return 2*x*x*x+120;
136             }
137          while(function==3) {
138             cout << "The area under the curve of f3(x) is " << area << endl;
139                return 5*x+3;
140             }
141          while(function==2) {
142             cout << "The area under the curve of f2(x) is " << area << endl;
143                return 6*x*x-x+10;
144             }
145          while(function==1) {
146             cout << "The area under the curve of f1(x) is " << area << endl;
147                return 2*x*x*x*x*x+x*x*x-10*x+2;
148             }
149
150
151          cout << "What is the left x value? " << endl;
152          cin >> a;
153          cout << "What is the right x value? " << endl;
154          cin >> b;
155          cout << "How many pieces are there? " << endl;
156          cin >> n;
157
158          w = (b-a)/n;
159
160          h = (f(a)+f(b))/2;
161
162          for(int i=1; i<n; i++) {
163          h += f(a+i*w);
164          }
165
166          area = h*w;
167
168          while (function==5){
169             cout << "The area under the curve of f5(x) is " << area << endl;
170                return 2*x*x;
171          }
172          while(function==4) {
173             cout << "The area under the curve of f4(x) is " << area << endl;
174                return 2*x*x*x+120;
175          }
176          while(function==3) {
177             cout << "The area under the curve of f3(x) is " << area << endl;
178                return 5*x+3;
179          }
180          while(function==2) {
181             cout << "The area under the curve of f2(x) is " << area << endl;
182                return 6*x*x-x+10;
183          }
184          while(function==1) {
185             cout << "The area under the curve of f1(x) is " << area << endl;
186                return 2*x*x*x*x*x+x*x*x-10*x+2;
187          }
188       }
189    }
190 }
191 double func_1(double x){
192 }
193 double func_2(double x){
194 }
195 double func_3(double x){
196 }
197 double func_4(double x){
198 }
199 double func_5(double x){
200 }
201 double f(double x){
202 }
                                                                                                                                                    
also, what is the best way to copy code from vim onto here? I won't tell you how i did it, or you are sure to all have a good laugh.
do I have to use all those while loops that I am using? or is there a better way?
Topic archived. No new replies allowed.