129k views
4 votes
CODING HW HELP

Instructions

Redo Programming Exercise 16 of Chapter 4 so that all the named constants are defined in a namespace royaltyRates.


Instructions for Programming Exercise 16 of Chapter 4 have been posted below for your convenience.


Exercise 16

A new author is in the process of negotiating a contract for a new romance novel. The publisher is offering three options. In the first option, the author is paid $5,000 upon delivery of the final manuscript and $20,000 when the novel is published. In the second option, the author is paid 12.5% of the net price of the novel for each copy of the novel sold. In the third option, the author is paid 10% of the net price for the first 4,000 copies sold, and 14% of the net price for the copies sold over 4,000. The author has some idea about the number of copies that will be sold and would like to have an estimate of the royalties generated under each option. Write a program that prompts the author to enter the net price of each copy of the novel and the estimated number of copies that will be sold. The program then outputs the royalties under each option and the best option the author could choose. (Use appropriate named constants to store the special values such as royalty rates and fixed royalties.)


CODE:

#include


using namespace std;


int main() {

// Write your main here

return 0;

}


CHECKS:

Program Executes Correctly


0 out of 3 checks passed. Review the results below for more details.


Test Case: Incomplete

Successful Output


Test Case: Incomplete

Successful Output II


Code Pattern: Incomplete

Check for constant declaration innamespace

1 Answer

2 votes

Answer:

To define all named constants in a namespace called royaltyRates, we can use the namespace keyword to enclose the constant definitions within it. For example:

namespace royaltyRates {

const double DELIVERY_PAYMENT = 5000.0;

const double PUBLICATION_PAYMENT = 20000.0;

const double ROYALTY_RATE_FIRST = 0.1;

const double ROYALTY_RATE_SECOND = 0.14;

const double NET_PRICE_THRESHOLD = 4000.0;

}


Then, we can use these constants in the program by referring to them as royaltyRates::CONSTANT_NAME. For instance, royaltyRates::DELIVERY_PAYMENT will refer to the DELIVERY_PAYMENT constant inside the royaltyRates namespace.

Step-by-step explanation:

The namespace keyword in C++ is used to define a named scope that can contain variables, functions, and other types of identifiers. By enclosing the constants within the royaltyRates namespace, we ensure that they are not globally defined and do not interfere with other variables and functions defined in the program.

We can access the constants defined in the royaltyRates namespace by prefixing them with the namespace name and scope resolution operator ::. This makes it easier to understand and organize the code, and reduces the likelihood of naming conflicts.

User DennyHiu
by
8.2k points