You want to write a program that will generate accurate shipping charges for a local grocery store. The shipping charges for the store are as follows: Weight of Package (in pounds)                Rate per mile Shipped  5 Lbs or less                                                         $0.20 Over 5 Lbs but no more than 8 Lbs                       $0.35 Over 8 Lbs but not more than 16 Lbs                    $0.45 Over 16 Lbs                                                          $0.50 Do NOT use global variables for this exercise. Write a function in a program that asks for the weight of a package in pounds and the distance it is to be shipped. These two arguments are to be passed into the funciton by REFERENCE. Using that data, write a second function (called by the main program) that calculates the shipping charge and returns it to the main program and displays the value inside the main program. For the second function, pass the two values (weight and distance) into the function as arguments. In the second function, if the shipping distance the user provided earlier is a distance greater than 20 miles, there will be an additional "Extended Distance" charge of $50 added to the order. For example, if an item weighing 21 pounds is shipped 25 miles, the total cost will be $62.50, calculated by the formula: ((25 miles * 0.50) + 50). Output to the user might read: A package weighing 21 pounds being shipped 25 miles will cost $62.50, which includes an "extended" distance charge of $50. input validation: Do not accept any weights less than 1 pound, nor any distances less than 0 miles.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
  1. You want to write a program that will generate accurate shipping charges for a local grocery store. The shipping charges for the store are as follows:

    Weight of Package (in pounds)                Rate per mile Shipped 
    5 Lbs or less                                                         $0.20
    Over 5 Lbs but no more than 8 Lbs                       $0.35
    Over 8 Lbs but not more than 16 Lbs                    $0.45
    Over 16 Lbs                                                          $0.50

    Do NOT use global variables for this exercise.

    Write a function in a program that asks for the weight of a package in pounds and the distance it is to be shipped. These two arguments are to be passed into the funciton by REFERENCE.

    Using that data, write a second function (called by the main program) that calculates the shipping charge and returns it to the main program and displays the value inside the main program. For the second function, pass the two values (weight and distance) into the function as arguments. In the second function, if the shipping distance the user provided earlier is a distance greater than 20 miles, there will be an additional "Extended Distance" charge of $50 added to the order. For example, if an item weighing 21 pounds is shipped 25 miles, the total cost will be $62.50, calculated by the formula: ((25 miles * 0.50) + 50).

    Output to the user might read:

    A package weighing 21 pounds being shipped 25 miles will cost $62.50, which includes an "extended" distance charge of $50.

    input validation: Do not accept any weights less than 1 pound, nor any distances less than 0 miles.

Expert Solution
Step 1

code:

#include <iostream>
using namespace std;

double getInfo();
double calculateCharges(double,double);

int main()
{
    //calling getInfo() function to get info and printing the rerturn value
    cout<<"The Shipping charges of Package are $"<<getInfo()<<endl;
    return 0;
}

//function to get weight and distance from user
double getInfo()
{
    double weight,distance;
    do{
        //getting input
        cout<<"Enter weight of the package(in pounds) : ";
        cin>>weight;
        cout<<"Enter the distance of package to be shipped(in miles) :";
        cin>>distance;
        //if input is not valid ask the values again using loop
        if(weight<1 && distance<0)
            cout<<"Weight can't be less than 1 pounds and Distance can't be less than 0 miles"<<endl;
        //otherwise break out of loop
        else
            break;
    }while(true);
    //calling calculateCharges() function with passing weight and distanc and returning the return value to main
    return calculateCharges(weight,distance);
}

//function to calculate Charges of package
double calculateCharges(double weight,double distance)
{
    double charges=0,chargesPerMile=0;
    //calculating charges per mile according to weight of package
    if(weight<=5)
        chargesPerMile=chargesPerMile+0.20;
    else if(weight<=10)
        chargesPerMile=chargesPerMile+0.30;
    else if(weight<=20)
        chargesPerMile=chargesPerMile+0.45;
    else if(weight>20)
        chargesPerMile=chargesPerMile+0.50;
    
    //calculating shipping charges according to distance of package to be shipped
    if(distance>20)
        charges=chargesPerMile*distance+50;
    else
        charges=chargesPerMile*distance;
    
    //returning charges value
    return cout<<" The shipping charges of the Package are= $", charges;
}

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education