Answer to Question #49102 in C++ for Marc

Question #49102
Hi
I need help with my C++ assignment please. It has to be handed in next week.

Write a program that reads in a distance in miles, then calculates and displays the corresponding shipping cost. Each shipment incurs a fixed cost of £50 and the price per mile is calculated according to the rules below
•Each of the first 100 miles (inclusive) cost £5.50
•Over 100 miles and up to 500 miles (inclusive): £4.00 per mile.
•Over 500 miles: £2.50 per mile.

For example, the cost of shipment over a distance of 550.50 miles is £2326.25
(i.e., £50 + (100*5.5) + (400*4.0) + (50.5*2.5)).

if the user enters a negative amount or zero for the distance the program should display an error message

Thank you ever so much.
1
Expert's answer
2014-11-19T13:16:32-0500
#include<iostream>

#define FIXED_COST 50

double calculate(double miles) {
double totalCost = 0;

if ( miles <= 0 ) {
return 0;
} else
if ( miles <= 100 ) {
totalCost = miles * 5.5 + FIXED_COST;
} else if ( miles > 100 && miles<= 500 ) {
totalCost = ( miles - 100 ) * 4 + 100 *5.5 + FIXED_COST;
} else if ( miles > 500 ) {
totalCost = ( miles - 500 ) * 2.5 + 400* 4 + 100 * 5.5 + FIXED_COST;
}

return totalCost;
}

int main () {
double miles;

std::cin >> miles;

if ( !calculate(miles) ) {
std::cout << "Wrongnumber" << std::endl;
return 0;
}

std::cout << "Total cost: "<< calculate(miles) << " pounds" << std::endl;


return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS