Question

Write a C++ program to run a menu-driven program with the following choices: Compute the factorial...

Write a C++ program to run a menu-driven program with the following choices:

  1. Compute the factorial of a number
  2. Compute the alternating factorial of a number
  3. Quit
#include <iostream>
using namespace std;
void getValidUserInputPosNumGT0 (int *a) {

    int num;
    cout << "Enter in a positive number greater than 0... ";
    cin >> *a;
}
long double factorial (int num) {
    int fact = 1;
    while (num > 1) {
        fact *= num;
        num--;
    }
    return fact;
}
long double AlternatingFactorial (int num) {
    int fact = 1;
    while (num < 1) {
        fact *=num;
        num++;
    }
}
int main (){
    int ch;
    int num;
    cout << "Welcome to the playing with numbers program!\n";
    do {
        cout << "1)Compute the factorial of a number\n";
        cout << "2) Compute the alternating factorial of a number\n";
        cout << "3)Quit\n";
        while (true) {
            cout << "Select an option (1..3)..";
            cin >> ch;
            if (ch == 1 || ch == 2 || ch == 3) {
                break;
            }
        }
        switch (ch) {
            case 1:
                getValidUserInputPosNumGT0(&num);
                cout << "Factorial("<<num<<")="<< factorial(num) << endl;
            case 2:
                long double AlternatingFactorial (int num);
                cout << "AlternatingFactorial" << num << AlternatingFactorial(num)<< endl;
        }
    }       while (ch!=3);
    return 0;


}

I've tried to run this and option one works, but the alternating factorial (option 2) won't run.

Homework Answers

Answer #1

#include <iostream>
using namespace std;
void getValidUserInputPosNumGT0 (int *a) {

int num;
cout << "Enter in a positive number greater than 0... ";
cin >> *a;
}
long double factorial (int num) {
int fact = 1;
while (num > 1) {
fact *= num;
num--;
}
return fact;
}
long double AlternatingFactorial (int num) {
int res=0;
for(int m=1;m<=num;m++){
if((num-m)%2==0){
res=res+factorial(m);
}
else{
res=res-factorial(m);
}
}
  
return res;
}
int main (){
int ch;
int num;
cout << "Welcome to the playing with numbers program!\n";
do {
cout << "1)Compute the factorial of a number\n";
cout << "2) Compute the alternating factorial of a number\n";
cout << "3)Quit\n";
while (true) {
cout << "Select an option (1..3)..";
cin >> ch;
if (ch == 1 || ch == 2 || ch == 3) {
break;
}
}
switch (ch) {
case 1:
getValidUserInputPosNumGT0(&num);
cout << "Factorial("<<num<<")="<< factorial(num) << endl;
break;
case 2:
getValidUserInputPosNumGT0(&num);
cout << "AlternatingFactorial(" << num <<")="<< AlternatingFactorial(num)<< endl;
}
} while (ch!=3);
return 0;


}

Note:

The return value is not provided in the function AlternatingFactorial(). So it is modified. And the errors in the switch case statements are removed.

Screenshots:

The screenshots are attached below for reference.

Please follow them for output.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std;...
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std; int main() {        int x;        cout << "Selection option " << endl;        cin >> x;        if (x == 1)               cout << "You select option 1" << endl;        else if (x >= 2 || x <= 4)               cout << "You select options 2 or 3 or 4" << endl;               else if (x == 10)               cout << "You select option 10" << endl;               else...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...
Write a program that reads a string and outputs the number of lowercase vowels in the...
Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be: There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy Where XXXX is the count of lowercase...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT