Write a program with the following functions: int menuAndValidate(); This functi
ID: 3622009 • Letter: W
Question
Write a program with the following functions:
int menuAndValidate();
This function displays
1. Add 2 numbers
2. Add 3 numbers
3. Swap 2 numbers
4. Swap 3 numbers
5. Quit
and validates (in a loop) and then returns the choice.
int addition(???);
Using default parameters have this one function add either 2, or 3 integers.
swap(???);
Overload this function so that it swaps 2 integers or 3 integers (1st to 2nd etc).
Then implement the main function which will use the menu function to read your choice and use a switch statement to execute the choice. Have the program loop until quit is selected. To execute a choice, read the appropriate number of integer arguments for processing.
Example run (reproduce in detail):
1. Add 2 numbers
2. Add 3 numbers
3. Swap 2 numbers
4. Swap 3 numbers
5. Quit
Your choice: 2
Enter 2 integers: 7 13
Result is 20
1. Add 2 numbers
2. Add 3 numbers
3. Swap 2 numbers
4. Swap 3 numbers
5. Quit
Your choice: 5Thank you.
.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int menuAndValidate();
int addition(int,int,int=0);
void swap(int&,int&);
void swap(int&,int&,int&);
int main()
{int choice,a,b,c;
choice=menuAndValidate();
while(choice!=5)
{switch (choice)
{case 1:cout<<"Enter 2 integers: ";
cin>>a>>b;
cout<<"Result is "<<addition(a,b)<<endl;
break;
case 2:cout<<"Enter 3 integers: ";
cin>>a>>b>>c;
cout<<"Result is "<<addition(a,b,c)<<endl;
break;
case 3:cout<<"Enter 2 integers: ";
cin>>a>>b;
swap(a,b);
cout<<"swapped they are "<<a<<" "<<b<<endl;
break;
case 4:cout<<"Enter 3 integers: ";
cin>>a>>b>>c;
swap(a,b,c);
cout<<"swapped they are "<<a<<" "<<b<<" "<<c<<endl;
break;
}
choice=menuAndValidate();
}
cout<<"Thank You ";
system("pause");
return 0;
}
int addition(int a,int b,int c)
{return a+b+c;
}
void swap(int& a,int& b)
{int t;
t=a;
a=b;
b=t;
}
void swap(int& a,int& b,int& c)
{int t;
t=a;
a=b;
b=c;
c=t;
}
int menuAndValidate()
{int c=9;
do
{cout<<"1. Add 2 numbers ";
cout<<"2. Add 3 numbers ";
cout<<"3. Swap 2 numbers ";
cout<<"4. Swap 3 numbers ";
cout<<"5. Quit ";
cin>>c;
if(c<1||c>5)
cout<<"Invalid input ";
}while(c<1||c>5);
return c;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.