COS1512 2024 Assignment 2 ready
to submit solutions
Question 1
#include <iostream>
#include <cmath> // For ceil function
using namespace std;
// Constants for cost per kg
const double GLOBALMAIL_COST_ZONE_1_TO_3 = 108.0;
const double GLOBALMAIL_COST_ZONE_4_TO_6 = 130.0;
const double DHL_COST_PER_KG = 70.0;
const int VOLUMETRIC_WEIGHT_DIVISOR = 5000;
// Function to calculate volumetric weight
double calculateVolumetricWeight(double length, double width,
double height) {
return (length * width * height) / VOLUMETRIC_WEIGHT_DIVISOR;
}
// Function to calculate cost using GlobalMail
double calcPostage(double weight, int zone) {
if (zone >= 1 && zone <= 3) {
return weight * GLOBALMAIL_COST_ZONE_1_TO_3;
} else if (zone >= 4 && zone <= 6) {
return weight * GLOBALMAIL_COST_ZONE_4_TO_6;
} else {
cout << "Invalid zone. Please choose a zone between 1 and
6." <<endl;
return 0.0;
}
, }
// Function to calculate cost using DHL
double calcPostage(double actualWeight, double length, double
width, double height) {
double volumetricWeight = calculateVolumetricWeight(length,
width, height);
double chargeableWeight = max(actualWeight, volumetricWeight);
return chargeableWeight * DHL_COST_PER_KG;
}
int main() {
char shippingMethod;
cout << "Choose shipping method (g for GlobalMail, d for DHL):
";
cin >> shippingMethod;
if (shippingMethod == 'g' || shippingMethod == 'G') {
int zone;
cout << "Enter the zone (1 to 6): ";
cin >> zone;
double weight;
cout << "Enter the weight of the parcel (in kg): ";
cin >> weight;
double totalCost = calcPostage(weight, zone);
cout << "Total cost using GlobalMail: R" << totalCost <<
endl;
} else if (shippingMethod == 'd' || shippingMethod == 'D') {
double length, width, height, actualWeight;
cout << "Enter the length of the box (in cm): ";
cin >> length;
cout << "Enter the width of the box (in cm): ";
cin >> width;
to submit solutions
Question 1
#include <iostream>
#include <cmath> // For ceil function
using namespace std;
// Constants for cost per kg
const double GLOBALMAIL_COST_ZONE_1_TO_3 = 108.0;
const double GLOBALMAIL_COST_ZONE_4_TO_6 = 130.0;
const double DHL_COST_PER_KG = 70.0;
const int VOLUMETRIC_WEIGHT_DIVISOR = 5000;
// Function to calculate volumetric weight
double calculateVolumetricWeight(double length, double width,
double height) {
return (length * width * height) / VOLUMETRIC_WEIGHT_DIVISOR;
}
// Function to calculate cost using GlobalMail
double calcPostage(double weight, int zone) {
if (zone >= 1 && zone <= 3) {
return weight * GLOBALMAIL_COST_ZONE_1_TO_3;
} else if (zone >= 4 && zone <= 6) {
return weight * GLOBALMAIL_COST_ZONE_4_TO_6;
} else {
cout << "Invalid zone. Please choose a zone between 1 and
6." <<endl;
return 0.0;
}
, }
// Function to calculate cost using DHL
double calcPostage(double actualWeight, double length, double
width, double height) {
double volumetricWeight = calculateVolumetricWeight(length,
width, height);
double chargeableWeight = max(actualWeight, volumetricWeight);
return chargeableWeight * DHL_COST_PER_KG;
}
int main() {
char shippingMethod;
cout << "Choose shipping method (g for GlobalMail, d for DHL):
";
cin >> shippingMethod;
if (shippingMethod == 'g' || shippingMethod == 'G') {
int zone;
cout << "Enter the zone (1 to 6): ";
cin >> zone;
double weight;
cout << "Enter the weight of the parcel (in kg): ";
cin >> weight;
double totalCost = calcPostage(weight, zone);
cout << "Total cost using GlobalMail: R" << totalCost <<
endl;
} else if (shippingMethod == 'd' || shippingMethod == 'D') {
double length, width, height, actualWeight;
cout << "Enter the length of the box (in cm): ";
cin >> length;
cout << "Enter the width of the box (in cm): ";
cin >> width;