Question
C++ assignment question using pass-by-refference. For this assignment, we will practice with pass-by-reference. We will imagine we are working for a company that manufactures cubes
C++ assignment question using pass-by-refference.
For this assignment, we will practice with pass-by-reference. We will imagine we are working for a company that manufactures cubes of different materials and sells them to end users. This company wants to automate its quote system, so it is asking you to write a function to calculate the total cost of manufacturing and shipping a cube. The function totalCostreturns the total cost, and takes the cube side length, the cube material density, and the material cost per kg as parameters, as well as reference parameters to extract shipping and manufacture separately.
The shipping cost is $100/cubic metre of volume of the cube plus $10/kg of mass.
The manufacturing cost is simply the costPerKg of the material by the mass of the cube.
/** * Function to calculate the total cost of creating and * shipping a cube product from a specified material. * The function also uses pass-by-reference to give * the manufacture and shipping costs separately. * * Shipping cost is calculated as $100 per cubic * metre of volume of the cube plus + $10/kg of mass, * scaled as needed. (For example, a 1.5 kg item would * add $15 to the shipping cost beyond the volume amount.) * * Manufacturing cost is determined by multiplying costPerKg * by the mass of the manufactured cube. * * Total cost is the sum of the above. * * @params len - length of side of the cube * (in metres) @pre > 0 * dens - density of the material cube is * made from (in kg/m^3) @pre > 0 * costPerKg - manufacturing cost per Kg of material * being used (in $CAD) @pre > 0 * shippingCost - output param to hold shipping cost * of the manufactured product * manufacturingCost - output param to hold cost of * manufacture of specified * product * * @modifies shippingCost and manufacturingCost to hold the * cost of shipping the specified cube and * its cost of manufacturing, respectively. * * @returns total cost of the cube (shipping + manufacture) */ double totalCost(double len, double dens, double costPerKg, double& shippingCost, double& manufacturingCost);
A co-worker has already started work in assign4.cpp and written a test case (in main) as well as a function, cubeDetails, that calculate the volume and mass of a cube when given length and density. The results are made available through pass-by-reference parameters. For this assignment, your totalCost function must use the provided cubeDetails function or you will get 0 for functionality.
/** * Function to calculate volume and mass * for a regular cubic solid from * its side length and material density. * * @params sideLength - length of side of the cube side * (in metres) @pre > 0 * density - density of the material cube is made * from (in kg/m^3) @pre > 0 * volume - output param to hold volume of cube * (in m^3) * mass - output param to hold mass of cube (in kg) * * @modifies volume and mass to contain the values * of the volume and mass (respectively) * of the specified cube. * * @returns nothing */ void cubeDetails(double sideLength, double density, double& volume, double& mass);
Starting from the provided assign4.cpp, you must write an implementation of the totalCost function that makes use of cubeDetails and both calculates the correct total cost as well as uses the pass-by-reference parameters correctly.
Proided assing4.cpp:
#include
//////////////////////////////////////////////////////////// // Function declaration for Assignment 4 ///////////////////
/** * Function to calculate the total cost of creating and * shipping a cube product from a specified material. * The function also uses pass-by-reference to give * the manufacture and shipping costs separately. * * Shipping cost is calculated as $100 per cubic * metre of volume of the cube plus + $10/kg of mass, * scaled as needed. (For example, a 1.5 kg item would * add $15 to the shipping cost beyond the volume amount.) * * Manufacturing cost is determined by multiplying costPerKg * by the mass of the manufactured cube. * * Total cost is the sum of the above. * * @params len - length of side of the cube * (in metres) @pre > 0 * dens - density of the material cube is * made from (in kg/m^3) @pre > 0 * costPerKg - manufacturing cost per Kg of material * being used (in $CAD) @pre > 0 * shippingCost - output param to hold shipping cost * of the manufactured product * manufacturingCost - output param to hold cost of * manufacture of specified * product * * @modifies shippingCost and manufacturingCost to hold the * cost of shipping the specified cube and * its cost of manufacturing, respectively. * * @returns total cost of the cube (shipping + manufacture) */ double totalCost(double len, double dens, double costPerKg, double& shippingCost, double& manufacturingCost);
//////////////////////////////////////////////////////////// // Functions for you to use in A4/testing /////////////////
/** * Function to calculate volume and mass * for a regular cubic solid from * its side length and material density. * * @params sideLength - length of side of the cube side * (in metres) @pre > 0 * density - density of the material cube is made * from (in kg/m^3) @pre > 0 * volume - output param to hold volume of cube * (in m^3) * mass - output param to hold mass of cube (in kg) * * @modifies volume and mass to contain the values * of the volume and mass (respectively) * of the specified cube. * * @returns nothing */ void cubeDetails(double sideLength, double density, double& volume, double& mass);
//////////////////////////////////////////////////////////// // Main function with some basic tests ///////////////////// int main() { // Test case 1: Large iron cube // density is 7874 kg/m^3, say $1/kg cost, // 1m per side dimensions. double totalRes, shippingRes, manuRes; totalRes = totalCost(1, 7874, 1, shippingRes, manuRes); if ((totalRes == 86714) and (shippingRes == 78840) and (manuRes == 7874)) { cout << "Test case 1 OK" << endl; } else { cout.precision(17); cout << "Test case 1 FAIL: calculated "; cout << totalRes << ", " << shippingRes << ", " << manuRes << endl; cout << "instead of 86714, 78840, 7874 for total, shipping, and manufacturing" << endl; } // Try some more test cases yourself... // Program done return 0; }
void cubeDetails(double sideLength, double density, double& volume, double& mass) { volume = sideLength * sideLength * sideLength; mass = volume * density; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started