Second part: Your program should allow the selection of multiple menu options. O
ID: 3830984 • Letter: S
Question
Second part:
Your program should allow the selection of multiple menu options.
Or in other words: after a selection is made, you should present the menu
again, and let the user select an option again
(keep doing this until option 0 is chosen)
The operations you need to implement for this assignment are the following:
Option 4: Calculate a power. When this operation is chosen you should ask for a base and exponent and then calculate the base raised to the exponent and print the result
Option 5: Calculate a factorial. When this operation is chosen you should ask for a value and calculate the factorial of this value and print the result.
Option 6: Print a multiplication table. Ask the user to enter a max value. Your program should then print a multiplication table from 0 to the max value.
Note on the multiplication table: For full credit the multiplication table should be of the following format.
If you do not include the headers then your max score for the assignment will be 9.75
The example uses max value 3.
The top row shows and first column show the value for the row/column of the table.
0 1 2 3
0 0 0 0 0
1 0 1 2 3
2 0 2 4 6
3 0 3 6 9
Note on power: There are no restrictions on how you calculate this portion. The intended method is to use loops, but if you use google you can find
another way to do this. Another note however: if you try what might be your first instinct, try it for a few values to see if it works the way you think
(it likely won't)
Explanation / Answer
Following is the required C++ code :
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;
char student_name[] = "YOUR NAME";
char student_id[] = "YOUR ID";
int do_int_operation( int first, int second, char operation ){
if(operation=='+'){
return first+second;
}
else if( operation=='-'){
return first-second;
}
else if( operation=='*'){
return first*second;
}
else if( operation=='/'){
return first/second;
}
else if( operation=='%'){
return first%second;
}
return -1; //or raise error
}
float do_float_operation( float first, float second, char operation ){
if(operation=='+'){
return first+second;
}
else if( operation=='-'){
return first-second;
}
else if( operation=='*'){
return first*second;
}
else if( operation=='/'){
return first/second;
}
return -1; //or raise error
}
int main(){
int option;
while( true ){
printf("Enter your option : ");
scanf("%d", &option );
if( option == 0 ){
/*printf("Exiting... ");*/
break;
}
else if( option == 1 ){
printf("Student Name : %s ", student_name );
printf("Student Id : %s ", student_id );
}
else if( option == 2 ){
int first, second;
char operation;
scanf("%d%c%d", &first, &operation, &second );
int answer = do_int_operation( first, second, operation );
printf("OUTPUT: %d %c %d = %d ",first,operation,second,answer);
}
else if( option == 3 ){
float first, second;
char operation;
scanf("%f%c%f", &first, &operation, &second );
float answer = do_float_operation( first, second, operation );
printf("OUTPUT: %.2f %c %.2f = %.2f ",first,operation,second,answer);
}
else if( option == 4 ){
float base;
int exponent;
printf("Enter a base : ");
scanf("%f",&base);
printf("Enter a exponent : ");
scanf("%d",&exponent);
float answer = pow( base, exponent ); //using function from cmath
printf("OUTPUT: %.2f ^ %d = %.2f ",base,exponent,answer);
}
else if( option == 5 ){
int number;
printf("Enter a number : ");
scanf("%d",&number);
int factorial = 1;
for( int i = 2; i <= number; i++ ){
factorial = factorial*i;
}
printf("OUTPUT: %d! = %d ",number,factorial);
}
else if( option == 6 ){
int number;
printf("Enter a max value : ");
scanf("%d",&number);
printf(" ");
for( int i = 0; i <= number; i++ ){
printf(" %d",i);
}
printf(" ");
for( int i = 0; i <= number; i++ ){
printf("%d",i);
for( int j = 0; j <= number; j++ ){
printf(" %d", j*i );
}
printf(" ");
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.