Respuesta :
Answer:
The function definition, in cpp, for the function printAttitude is given below.
void printAttitude(int n)
{
switch(n)
{
case 1: cout<<"disagree"<<endl; break;
case 2: cout<<"no opinion"<<endl; break;
case 3: cout<<"agree"<<endl; break;
default: break;
}
}
Explanation:
As seen, the method takes an integer parameter.
The method does not returns any value hence, return type is void.
The message is displayed to the console using switch statement which executes on the integer parameter, n.
The values for which output needs to be displayed are taken in cases.
The values for which no output needs to be displayed, is taken in default case.
Every case ends with break statement so that the program execution can be terminated.
This method can be used inside a program as shown.
PROGRAM
#include <iostream>
using namespace std;
void printAttitude(int n)
{
switch(n)
{
case 1: cout<<"disagree"<<endl; break;
case 2: cout<<"no opinion"<<endl; break;
case 3: cout<<"agree"<<endl; break;
default: break;
}
}
int main() {
// variables to hold respective value
int n;
// user input taken for n
cout<<"Enter any number: ";
cin>>n;
// calling the function taking integer parameter
printAttitude(n);
return 0;
}
OUTPUT
Enter any number: 11
1. The user input is taken for the integer parameter.
2. Inside main(), the method, printAttitude(), is then called and the user input is passed as a parameter.
3. Since the user entered value is 11 which does not satisfies any of the values in the cases, the default case is entered which does nothing and executes the break statement.
4. Hence, the program displays nothing for the value of 11.
5. When the user enters value 3, the case statement is executed since 3 satisfies one of the case values.
6. The program output for the user input of 3 is as shown below.
OUTPUT
Enter any number: 3
agree
The program ends with return statement.