Question: /******** Instructor's Feedback (fix program a cording to Instructor's Feedback) * cant use while(1) /while (true) loop/goto * modularization each menu option should have it

/******** Instructor's Feedback (fix program a cording to Instructor's Feedback)

* cant use while(1) /while (true) loop/goto

* modularization each menu option should have it is own function

* unnecessary abrupt program termination in case quit

* If a user enters a value of a wrong data type , the program goes into infinite loop or prints menu multiple times when a user enters a value of a wrong data type ( add cin.clear(); cin.ignore(...); in the default case of the switch)

* does not validate number of terms properly, goes into infinite recursion and/or segmentation fault (core dumped) when 1/0 or negative number of terms entered

* output does not comply with the assignment description

* test runs not included

* should have a separate function to input and validate number of terms

*********/

#include #include using namespace std;

float series1(int n); int series2(int n);

int main() { int choice, num; while (true) { //loop until user exit cout << " Enter choice: " " (1) for sum of series: 1/(0+1)+1/ " "(1+2)+1/(2+3)+1/(3+4)+...+1/((n-1)+n)- starting with 1 " " (2) for sum of series: (n*n)+((n-1) " "*(n-1)+...+(3*3)+(2*2)+(1*1) [ starting with n] " " (3) To exit " " Enter your choice: "; cin >> choice;

if (choice == 1 || choice == 2) { cout << " Enter the value of n: "; // get input cin >> num; } // switch case switch (choice) { case 1: cout << "1 + "; for (int i = 0; i < num; i++) { if (i != 0){ cout << "1/(" << i << "+" << (i + 1) << ")"; if (i != num-1) cout << " + "; } } cout << " = " << setprecision(4) << fixed << series1(num) << endl; break; case 2: cout << num << "*" << num << " + "; for (int i = num - 1; i >= 1; i--) { if (i != 1) cout << i << "*" << i << " + "; else cout << i ; } cout << " = " << series2(num) << endl; break; case 3: cout << " goodbye"; exit(0); // to exit default: cout << " Invalid choice. Please enter a valid choice " ; } } }

// recursion function for the series: 1/(0+1)+1/ (1+2)+1/(2+3)+1/(3+4)+...+1/((n-1)+n) //starting with 1 float series1(int n) { if (n == 1) { return 1.0; } return float(1 / float(n + n - 1)) + series1(n - 1); // recursive case }

// recursion function for the series: (n*n)+((n-1) *(n-1)+...+(3*3)+(2*2)+(1*1) // [ starting with n] int series2(int n) { if (n == 1) { return 1; } return n * n + series2(n - 1); // recursive case }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!