SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Procedural Abstraction And Functions That Return A Value
Exercise:
Self-test Exercises
Question:29 | ISBN:9780321531346 | Edition: 7

Question

The definition of unitprice does not do any input or output and so does

not use the library iostream.

In main we needed the using directive because cin and cout are defined in iostream and those definitions place cin and cout in the std namespace.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

#include <iostream>

double unitprice(double price, int quantity) {
    return price / quantity;
}

int main() {
    using namespace std;  // Using directive to bring the std namespace into scope

    double price;
    int quantity;

    cout << "Enter the price: ";
    cin >> price;

    cout << "Enter the quantity: ";
    cin >> quantity;

    double result = unitprice(price, quantity);

    cout << "The unit price is: " << result << endl;

    return 0;
}

OUTPUT OF THE PROGRAM CODE:

Enter the price: 100
Enter the quantity: 2000
The unit price is: 0.05

 

0 0

Discussions

Post the discussion to improve the above solution.