6.1k views
5 votes
Complete method printPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than 2, print "Too small". If greater than 10, print "Too large". Otherwise, compute and print 6 * bagOunces followed by " seconds". End with a newline. Example output for ounces

User Atos
by
4.2k points

1 Answer

5 votes

Answer:

The complete method in cpp is given below.

void printPopcornTime(int bagOunces)

{

if(bagOunces<2)

cout<<"Too small"<<endl;

else if(bagOunces>10)

cout<<"Too large"<<endl;

else

cout<<6*bagOunces << " seconds"<<endl;

}

The program implementing the above method is given below.

#include <iostream>

using namespace std;

//method declared

void printPopcornTime(int bagOunces);

int main() {

//variable declared

int bagOunces;

//variable initialized

bagOunces = 10;

//method called which accepts integer parameter

printPopcornTime(bagOunces);

//program ends

return 0;

}

//method defined

void printPopcornTime(int bagOunces)

{

//output displayed based on the value of the variable

if(bagOunces<2)

cout<<"Too small"<<endl;

else if(bagOunces>10)

cout<<"Too large"<<endl;

else

cout<<6*bagOunces << " seconds"<<endl;

}

OUTPUT1

60 seconds

Explanation:

Program explanation is given below.

1. The method, printPopcornTime(), having return type as void and accepting an integer parameter, bagOunces, is declared as shown.

void printPopcornTime(int bagOunces);

2. Method declaration only contains return type, method name and parameters taken by the method, if any.

3. Method definition consists of the complete method. All the code to be written inside the method is also included in its definition.

4. The method definition for printPopcornTime() is shown in the beginning.

5. The value of the integer parameter is tested using multiple if-else statements.

6. The output is based on the value of the parameter. The output is ends with a newline inserted by endl.

7. The main() method has a return type integer and takes no parameters. The main() method is declared and defined together unlike as shown for the other method, printPopcornTime().

8. The integer variable, bagOunces, is declared and initialized inside main().

9. This variable is passed as parameter to the printPopcornTime() method and calling the method.

printPopcornTime(bagOunces);

10. The program ends with a return statement.

11. All the results are shown based on the different values of the variable, bagOunces. The original output is obtained when the value of bagOunces is 10.

12. If the value of bagOunces is 1, the message displayed is shown.

OUTPUT2

Too small

13. If the value of bagOunces is 11, the message displayed is shown.

OUTPUT3

Too large

User Varun Patro
by
4.6k points