Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I reduce this 15 lines or less in the main code? #include <iostream> #in

ID: 3550999 • Letter: H

Question


How can I reduce this 15 lines or less in the main code?


#include <iostream>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;

double func_1(double);
double func_2(double);
double func_3(double);
double func_4(double);
double func_5(double);
double rect_integral(double a, double b, int n, int choice);
double trap_integral(double a, double b, int n, int choice);


const char * funcname[6] = {
"",
"5x^4 + 3x^3 - 10x + 2",
"6x^2 âx+10",
"5x + 3",
"2x^3 + 120",
" 2x^2" };



int main () {

int choice;//a function to be integrated.
int n;// number of recta/trap
int num = 1;//condition of the while loop
double a;// Lower limit of integration
double b;// Upper limit of integration
bool trap = false;
bool rect = false;

cout << "Functions available: ";

for (int i=1; i<6; i++)

cout << " " << i << " " << funcname[i] << ' ';

cout << endl;

while(num==1){

choice=0;


cout << "Choose a function (1, 2, 3, 4, 5, other(quit)) " << endl;//promptthe userto choose function


cin >> choice; //the user choice of the function


if (choice >= 1 && choice <= 5) // checking the user input
{

char method = ' ';
do {
cout << "Would you like to calculate the area using the rectangle," //prompt the user choose a method to integrate

<< " trapezoid, or both (1, 2, 3): " << endl;

cin >> method;

} while (method < '1' || method > '3'); //cheking the user input

switch (method) {
case '1':

rect = true;

cout <<"How many rectangles do you want? " << endl;

break;
case '2':

trap = true;

cout <<"How many trapezoids do you want? " << endl;

break;
case '3':

trap = true;

rect = true;

cout <<"How many rectangles/trapezoids do you want? " << endl;

}

cin >> n;

cout <<"Please select a starting point, a: " << endl;//ask the user of a starting point
cin >> a;

cout <<"Please select an ending point, b: " << endl;//ask the user of an ending point
cin >> b;

if (rect) {

cout << "The area under rect " << funcname[choice] //output the area of the curve of rect

<< " between " << a << " and " << b

<< " is: " << rect_integral(a, b, n, choice) << endl;
}

if (trap) {

cout << "The area under trab " << funcname[choice] << " between " << a << " and " << b<<
"is:" << trap_integral(a, b, n, choice) << endl;//output the area of the curve of trap

}


}
cout<<"Do you want to continue<1/0>?"<<endl;
cin >> num;

}
return 0;
}


double func_1 (double x)
{
return (2*pow(x,5))+(pow(x,3))-(10*x)+2;
}

double func_2 (double x)
{
return (6*pow(x,2))-(x)+10;
}

double func_3 (double x)
{
return (5*x)+3;
}

double func_4 (double x)
{
return (2* pow(x,3))+120;
}

double func_5 (double x)
{
return 2*pow(x,2);
}


double rect_integral(double a, double b, int n, int choice)
{



double (* fx) (double);

switch (choice)
{
case 1: fx = func_1; break;
case 2: fx = func_2; break;
case 3: fx = func_3; break; //choose the funcktion
case 4: fx = func_4; break;
case 5: fx = func_5; break;
}


double x;
double total_area;
double width;


width = (b-a)/n; //the equation of the width or delta x
total_area = 0; //inatilizion total area
x = a;

for(int i=1; i<=n; i++) { //for loop to calculate the area under the curve
x = a;
x+=width;
total_area+=width*fx(x);
}

return total_area;

}


double trap_integral(double a, double b, int n, int choice)
{


{

double (* fx) (double);

switch (choice)
{
case 1: fx = func_1; break;
case 2: fx = func_2; break;
case 3: fx = func_3; break; //choose the funcktion
case 4: fx = func_4; break;
case 5: fx = func_5; break;
}


double x;
double total_area;
double width;



width = (b-a)/n; //the equation of the width or delta x
total_area = 0; //inatilizion total area
x = a;

for(int i=1; i<=n; i++) { //for loop to calculate the area under the curve by trap
x = a;
x+=width;
total_area+=width*(fx(x)+(fx(x)+width))/2;
}

return total_area;

}



}

Explanation / Answer

//Replace int main with these functions and this int main

//for complete code see comments

void showfunctions(int totalFunction)

{

cout << "Functions available: ";

for (int i=1; i<totalFunction; i++)cout << " " << i << " " << funcname[i] << ' ';

cout << endl;

}

void performOperation(int choice)

{

int n;

double a,b;

bool trap = false,rect = false;

char method =' ';

do {

cout << "Would you like to calculate the area using the rectangle,"<< " trapezoid, or both (1, 2, 3): " << endl;

cin >> method;

} while (method < '1' || method > '3');

switch (method)

{

case '1':

rect = true;

cout <<"How many rectangles do you want? " << endl;

break;

case '2':

trap = true;

cout <<"How many trapezoids do you want? " << endl;

break;

case '3':

trap = true;

rect = true;

cout <<"How many rectangles/trapezoids do you want? " << endl;

}

cin >> n;

cout <<"Please select a starting point, a: " << endl;

cin >> a;

cout <<"Please select an ending point, b: " << endl;

cin >> b;

if (rect)

cout << "The area under rect " << funcname[choice]<< " between " << a << " and " << b<< " is: " << rect_integral(a, b, n, choice) << endl;

if (trap)

cout << "The area under trab " << funcname[choice] << " between " << a << " and " << b<<"is:" << trap_integral(a, b, n, choice) << endl;

}

int main ()

{

int choice,n,num=1,totalFunction=5;

showfunctions(totalFunction);

while(num==1)

{

choice=0;

cout << "Choose a function (1, 2, 3, 4, 5, other(quit)) " << endl;

cin >> choice;

if(choice >= 1 && choice <= 5)

performOperation(choice);

cout<<"Do you want to continue<1/0>?"<<endl;

cin >> num;

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote