/ I was given the basic outline of the program from my teacher and we were told
ID: 3544412 • Letter: #
Question
/I was given the basic outline of the program from my teacher and we were told to do all the functions. I have already done all the functions but im having trouble getting them into the outline correctly. Heres the outline and all the functions all i need is help copy and pasting it all together so it runs. It is very possible that there may be a small error in the functions. I need this program to be running smoothly. any help will be much appriciated.
#include <iostream>
#include <iomanip>
using namespace std;
/ Function Prototypes /
string Menu();
int getValue( string, int, int );
void doAverage();
double calcAverage( int, int );
void doFactorial();
int calcFactorial( int );
void doMtoNth();
int calcMtoNth( int, int );
void doRange();
int calcRange( int, int, int, int );
int main()
{
string userChoice;
userChoice = Menu();
while ( userChoice != "Q" && userChoice != "q" )
{
if (userChoice == "1")
{
doAverage();
}
else if ( userChoice == "2" )
{
doFactorial();
}
else if ( userChoice == "3" )
{
doMtoNth();
}
else if (userChoice == "4")
{
doRange();
}
userChoice = Menu();
}//end of the while loop
//system("pause");
return 0;
}
Code your Functions below this line
string Menu()
{
string userChoice;
cout << "Special Purpose Calculator";
cout << " 1) Average";
cout << " 2) Factorial ";
cout << " 3) M to the Nth Power";
cout << " 4) Range";
cout << " Q) Quit";
cout << " Enter your choice?";
cin >> userChoice;
return userChoice;
}
int getValue(string prompt, int lowBound, int upBound)
{
int number;
cout << prompt << lowBound << " and " << upBound << ": ";
do
{
if (number < lowBound || number > upBound)
{
cout << " Error: " << number << " is invalid. Try again: ";
}
}
while (number < lowBound || number > upBound);
return number;
}
void doAverage()
{
string state1 = "Enter a sum to be averaged between ";
string state2 = "Enter the number of entries ";
int lowBoundA = 1,
upBoundA = 1000,
lowBoundB = 1,
upBoundB = 1000,
sum,
count;
double average;
sum = getValue(state1, lowBoundA, upBoundA);
count = getValue(state2, lowBoundB, upBoundB);
average = calcAverage(sum, count);
cout << " The average is: ";
return;
}
double calcAverage( int sum, int count )
{
double average;
average = (double) sum/count;
return average;
}
void doFactorial()
{
string state1 = "Enter a number between: ";
int lowBound = 0,
upBound = 10,
numfactorial,
factorial;
numfactorial = getValue(state1, lowBound, upBound);
factorial = calcFactorial(numfactorial);
return;
}
int calcFactorial( int number )
{
int count,
factorial = 1,
numfactorial;
count = numfactorial;
cout << " The factorial value is: ";
while (count > 0)
{
factorial = factorial * count;
cout << count << " x";
count --;
}
cout << " = " << factorial;
return factorial;
}
void doMtoNth()
{
string state1 = "Enter a value: ";
string state2 = "Enter a value: ";
int lowBoundA = 1,
upBoundA = 10,
lowBoundB = 1,
upBoundB = 8,
M,
N,
MtoNth;
double average;
M = getValue(state1, lowBoundA, upBoundA);
N = getValue(state2, lowBoundB, upBoundB);
MtoNth = calcMtoNth (M, N);
cout << " " << M << " to the " << N << "th power is: ";
cout << MtoNth;
return;
}
int calcMtoNth( int M, int N )
{
int MtoNth = 1,
count = 1,
Nth;
while (count <= Nth)
{
MtoNth *=M;
count ++;
}
return MtoNth;
}
void doRange()
{
string state1 = "Enter a number between ";
string state2 = "Enter another number between ";
int lowBound = 0,
upBound = 1000,
num1,
num2,
num3,
num4,
range = 0;
double average;
num1 = getValue(state1, lowBound, upBound);
num2 = getValue(state2, lowBound, upBound);
num3 = getValue(state2, lowBound, upBound);
num4 = getValue(state2, lowBound, upBound);
range = calcRange(num1, num2, num3, num4);
cout << " The range of " << num1 << ", " << num2 << ", " << num3 << ", " << num4 << "is " << range;
return;
}
int calcRange( int number1, int number2, int number3, int number4 )
{
int max,
min,
range;
if (number1 >= number2)
max = number1;
else
max = number2;
if (number3 >= max)
max = number3;
if (number4 >= max)
max = number4;
if (number1 <= number2)
min = number1;
else
min = number2;
if (number3 <= min)
min = number3;
if (number4 <= min)
min = number4;
range = max - min;
return range;
}
and heres the outline all the functions have to go into-----------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
//Place your symbolic constant definitions after this line
//Place your function prototypes after this line
int main()
{
string userChoice;
//Get the user's menu choice
userChoice = Menu();
//While the user does not want to quit
while( userChoice != "Q" and userChoice != "q" )
{
//If the user selected option 1 from the menu, calculate an average
if( userChoice == "1" )
{
doAverage();
}
//If the user selected option 2 from the menu, calculate the factorial
//of a number
else if( userChoice == "2" )
{
doFactorial();
}
//If the user selected option 3 from the menu, calculate the result of
//of raising a number to a power
else if( userChoice == "3" )
{
doMtoNth();
}
//If the user selected option 4 from the menu, calculate the range of
//four numbers
else
{
doRange();
}
//Get the next menu choice from the user
userChoice = Menu();
}
return 0;
}
//Code your functions below this line//
Explanation / Answer
hey i dont have exact solution but hope this below code help you definatly
#include<iostream.h>
#include<conio.h>
int main()
{
int choice, num, i ;
unsigned long int fact;
clrscr();
while (1)
{
cout<<"1. Factorial ";
cout<<"2. Prime ";
cout<<"3. Odd / Even ";
cout<<"4. Exit ";
cout<<" Your choice ?";
cin>>choice ;
switch ( choice)
{
case 1 :
cout<<" Enter number:";
cin>>num ;
fact = 1 ;
for (i=1 ;i<=num ;i++)
fact = fact * i;
cout<<"' Factorial value = "<< fact;
break ;
case 2 :
cout<<"' Enter number:";
cin>>num ;
for(i=2;i<num;i++ )
{
if (num%i== 0 )
{
cout<<" Not a prime number. ";
break ;
}
}
if (i==num)
cout<<"' Prime number. ";
break;
case 3 :
cout<<" Enter number:";
cin>>num ;
if ( num %2==0 )
cout<<"' Even number. ";
else
cout<<" Odd number. ";
break;
case 4 :
return 0;
}
}
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.