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

Rewrite Previous Programming Assignment found ( BELOW ) so that it utilizes func

ID: 3704382 • Letter: R

Question

Rewrite Previous Programming Assignment found (BELOW) so that it utilizes functions. You will need at least 4 functions that are called by main: menu, summation, factorial, and exponential. The functions operate as follows: int menu() displays the menu and prompts for the user's choice and returns that choice. void summation() prompts the user for their integer and then does the summation and the displays the result. It is passed nothing and returns nothing. void factorial() is same as summation void exponential is same as summation - make sure to use for loops to do the calculations and not pow or exp. Each function should have it's own header block. It should state the name of the function, the purpose of the function and what it is passed or what it returns. These header blocks are created using comment lines within your code. Rule #1: Absolutely no global variables. Variables used only inside of a function should be declared locally to that function. For example, if you have a counter variable down in Summation, then declare it locally to Summation. OR, if main() needs to know what choice the user made from the menu, then the function menu() returns that value. No global variables under any circumstances. Same input validation requirements as when you submitted for previous code from (BELOW). VISUAL BASIC C++

PREVIOUS ASSIGNMENT CODE;

Pseudocode for the main function might look like this: (I underlined a few side comments for you - but they are not part of the p-code.)

#include "stdafx.h" sumnationcounter; include using namespace std; /Result display cout

Explanation / Answer

#include "stafx.h"
#include <iostream>
using namespace std;


/*
   Function to read a positve number n and
   calculate sumation of 1 + 2 +3 .... + n
   And Display the result Summation vaule
*/
void summation(){
   int summation = 0, input;
  
   // Ask user input a positive integer
   cout << "Enter any positive integer : "<<endl;
   cin >> input;

   // prompt input a positive integer
   while (input < 1){
       cout << " | " << input << " is not a positve integer. "<< endl;
       cout << "Enter a positive integer again : "<< endl;
       cin >> input ;
   }
   for(int counter = 1; counter <= input ; counter++){
       summation +=counter;
   }  
   //Result Display
   cout << " The summation of number 1 - "<<input
       <<" is : "<<summation<<endl;
  
}

/*
   Function to read a positve number n and
   calculate factorial n! = 1 * 2 * 3 *.....*n
   And Display factorial vaule
*/
void factorial(){
   int value = 1, input;
   // Ask user input a positive integer
   cout << "Enter any positive integer : "<<endl;
   cin >> input;

   // prompt input a positive integer
   while (input < 1){
       cout << " | " << input << " is not a positve integer. "<< endl;
       cout << "Enter a positive integer again : "<< endl;
       cin >> input ;
   }

   for(int counter = 1 ; counter <= input ; counter++){
       value *= counter;
   }
   // Result Display
   cout<< " The value of "<< input <<" factorial is : ";
   cout<< value<< endl;
  
}

/*
   Function to read a [positve number n and
   Calculate exponent of 2 for n (ie 2 * 2 * 2 *.... n times)
   Display the result      
*/
void exponential (){
   int num = 1, input;

   // Ask user input a positive integer
   cout << "Enter any positive integer : "<<endl;
   cin >> input;

   // prompt input a positive integer
   while (input < 1){
       cout << " | " << input << " is not a positve integer. "<< endl;
       cout << "Enter a positive integer again : "<< endl;
       cin >> input ;
   }

  
   for(int counter = num ; counter <= input ; counter++){
       num *= 2;
   }
   // Result Display
   cout<< " The reuslt is base 2 raised to the power of "
       << input <<" is : ";
   cout<< num<< endl;
   num = 1;

  
}
int displayMenu(){
   int choice_menu;
       // Display the Menu of Arithmatic Functions
       cout <<" Arithmetic Function "<< endl;
       cout <<"1. Summation"<< endl;
       cout <<"2. Factorial"<< endl;
       cout <<"3. Exponent"<< endl;
       cout <<"4. GoodBYE! "<< endl;
       cout <<" Calculator choice: "<< endl;
       cin >> choice_menu;
  
       while (choice_menu < 1 || choice_menu > 4){
           cout << "Enter valid choice "<< endl;
           cin >> choice_menu;
       }
   return choice_menu;
}

int main()
{
   int choice;
   do{
       choice = displayMenu();       // Call Display Menu function
      
       switch (choice){
              
               case 1: summation();
                   break;
                  
               case 2: factorial();
                   break;
                  
               case 3: exponential();
                   break;
          
       }
      
   }while(choice != 4);
}

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