Question

In: Computer Science

Function Return Value In this program, you will be using C++ programming constructs, such as functions...

Function Return Value

In this program, you will be using C++ programming constructs, such as functions and loops.

main.cpp

Write a program that allows the user to enter the information for multiple packages to determine the shipping charges for each package. The program will exit when the user enters 0 or negative for the package weight.

Your program will ask the user to enter the weight of a package they want to ship. If the weight they enter is a positive number, your program will then prompt the user to enter the distance the package will be shipped. Your program will then output the shipping charges with a precision of 2 digits past the decimal point, and will prompt the user for the next package.

calculateCharge

Create a function called calculateCharge that contains 2 parameters: a double to represent the weight of the package, and an integer to represent the distance the package will be shipped. This function returns the shipping charge. See types.hpp for the function prototype for this function.

This function calculates the charge based on the package weight as well as the distance. The rates per weight are defined in types.hpp. And that rate is multiplied by how many 500 mile segments the package will be traveling. For instance, if the distance is 1-500 then the rate is multiplied by one. If the distance is 501-1000 then the rate is multiplied by two. 1001-1500, multiplied by three, and so forth.

Input Validation

  1. You can assume the user will always input valid data types (floating-point for package weight and integer for distance).

Hints

  1. Be sure to include the file types.hpp with the #include files so that the compiler knows where to find the program constants and function prototype.
  2. Shipping charges should be displayed with a precision of 2 digits past the decimal point.
  3. Don't forget to add comments to explain what the code is doing and where control of the program is executing.
  4. Choose variable names and function names that describe the purpose of the variable.

Sample Output

Welcome to Fast Freight Shipping Company

Enter the package weight in lbs (or 0 to exit): 0
Welcome to Fast Freight Shipping Company

Enter the package weight in lbs (or 0 to exit): 33
Enter shipping distance in miles: 3

Shipping cost: $6.40

Enter the package weight in lbs (or 0 to exit): -1
Welcome to Fast Freight Shipping Company

Enter the package weight in lbs (or 0 to exit): 3.4
Enter shipping distance in miles: 501

Shipping cost: $8.40

Enter the package weight in lbs (or 0 to exit): 3.4
Enter shipping distance in miles: 500

Shipping cost: $4.20

Enter the package weight in lbs (or 0 to exit): 1.1
Enter shipping distance in miles: 1100

Shipping cost: $9.30

Enter the package weight in lbs (or 0 to exit): 1.1
Enter shipping distance in miles: 1

Shipping cost: $3.10

Enter the package weight in lbs (or 0 to exit): 0

Here is is the information on the types.hpp file to be used:


//-----------
// Constants
//-----------

// shipping distance per segment

const int SEGMENT_MILES = 500;

// rates per 500 miles shipped

const double RATE1 = 3.10; // pkgs weighing <= 2 lb
const double RATE2 = 4.20; // pkgs > 2 lb but <= 6 lb
const double RATE3 = 5.30; // pkgs > 6 lb but <= 10 lb
const double RATE4 = 6.40; // pkgs > 10 lb


//---------------------
// Function prototypes
//---------------------

// This function receives a package weight in lbs and
// a shipping distance in miles. It uses these to compute
// and return the shipping charge.

double calculateCharge(double weight, int distance);

Solutions

Expert Solution

Check out the solution and do COMMENT for any queries.

_________________

// .cpp file

// include required headers
#include <iostream>
#include <iomanip>
#include "types.hpp" // include .hpp file

using namespace std;

// main function
int main() {
// variable declaration
double weight, charge;
int distance;

// infinte loop and loop terminates when user enters 0 or negative weight
while(true)
{
// display statement as required
cout << "\n\nWelcome to Fast Freight Shipping Company";
cout << "\n\nEnter the package weight in lbs (or 0 to exit) : ";
// user input
cin >> weight;
// if weight > 0 then go ahead
if(weight > 0)
{
// get distance value from user
cout << "Enter shipping distance in miles : ";
cin >> distance;
// function call
charge = calculateCharge(weight, distance);

cout << fixed << showpoint << setprecision(2); // to print 2 digits after decimal
// print the result
cout << "\nShipping cost : $ " << charge;
}
else
{
// if user enters 0 or negative then print appropriate message
cout << "\nProgram terminnated!!!";
// break out of the infinite while loop
break;
}

}

return 0;
}

// function definition
double calculateCharge(double weight, int distance)
{
// required declaration
double rate, charge;
int n = 0;

// check out for weight and assign rate accordingly from constants in .hpp file
if(weight <= 2)
rate = RATE1;
else if(weight > 2 && weight <= 6)
rate = RATE2;
else if(weight > 6 && weight <= 10)
rate = RATE3;
else if(weight > 10)
rate = RATE4;

// as rates are mentioned per 500 miles
// so find number of multiples of 500 present in the given distance
while(distance > 500)
{
n = distance / 500;
distance = distance % 500;
}
// if distance <= 500 then its as equal to multiplying rate with 1
if(distance <= 500)
n = n + 1; // make n as 1 if distance <= 500 or add 1 to previously calculated 'n'

// determine the shipping charge
charge = rate * n;
// return the result
return charge;
}

---------------------------

// types.hpp file - given user requirement

// Constants
const int SEGMENT_MILES = 500;
// rates
const double RATE1 = 3.10;
const double RATE2 = 4.20;
const double RATE3 = 5.30;
const double RATE4 = 6.40;

// function prototype
double calculateCharge(double weight, int distance);

----------------------------

Code ::

_____

________

Output ::


Related Solutions

The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
C Programming question: Develop a program named “sample” that prints the value of two functions: 1....
C Programming question: Develop a program named “sample” that prints the value of two functions: 1. Function f1 takes a single input argument of type int and returns the value of its input argument as float 2. Function f2 takes a single input argument of type char and returns an int. The output of f2 is the result of the sum of the values of the global variables, and the input argument minus the value of char ‘a’ plus the...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Intro C++ Programming Chapter 6 Functions You have been tasked to write a new program for...
Intro C++ Programming Chapter 6 Functions You have been tasked to write a new program for the Research Center's shipping department. The shipping charges for the center are as follows: Weight of Package (in kilograms)                Rate per mile Shipped 2 kg or less                                                      $0.05 Over 2 kg but no more than 6 kg    $0.09 Over 6 kg but not more than 10 kg    $0.12 Over 10 kg    $0.20 Write a function in a program that asks for...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
C++ programming: FUNCTIONS WITH DIFFERENT RETURN TYPES AND PARAMETERS NuMetro has a special on movies for...
C++ programming: FUNCTIONS WITH DIFFERENT RETURN TYPES AND PARAMETERS NuMetro has a special on movies for all members of the public but special discounts for students and pensioners. If pensioners or students buy a movie ticket they receive 10% if they buy a movie and popcorns, they receive 20% discount. Other customers only receive a discount when they buy a movie ticket and popcorn; there is no discount for just a movie ticket alone. Write a program named question5b.cpp that...
C Programming Please write a single function and not the whole program Can you please provide...
C Programming Please write a single function and not the whole program Can you please provide comments and explanations for all questions. Thank you Write a single function for the following: void split_date(int day_of_year, int year, int *month, int *day);             day_of_year is an integer between 1 and 366, specifying a particular day within the year designated by the parameter year. Month and day point to variables in which the function will store the equivalent month (1 – 12) and day...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT