A farmer wants to build a fence to enclose an area of his field. The field is bo
ID: 644265 • Letter: A
Question
A farmer wants to build a fence to enclose an area of his field. The field is bordered on the north by a river, which does not need a fence (see diagram below). The farmer can choose between wood, iron and pvc fencing materials.
Write a program which calculates the cost of the fence materials and the labor needed to build the fence, then print out the cost of the two.
The Program must:
Prompt the user for the length and width of area to be enclosed.
Prompt the user to determine the type of material to be used on the fence. They should enter 1 for wood, 2 for iron, and 3 for pvc fencing.
Create a function to calculate the cost of the fencing material using the following costs:
Wood fencing will cost $10 per foot
Iron fencing will cost $30 per foot
Pvc fencing will cost $25 per foot
The function will have parameters that include the length, width, and fence type. The function will return the cost of the material.
Use a separate function to calculate the cost of the labor using the following costs:
If the total length of fencing 200 feet or more, the farmer will pay of $10 per foot
If the total length is less than 200 feet, the farmer will pay $15 per foot.
This function will accept the length and width as parameters and will return the cost of the labor.
Use a third function to display the output as follows:
The total length of fencing required is ### feet.
The cost for a fence of this length is .
The cost for the labor for this length of fence is .
The total cost for the fence is .
This function will not return any value. You will need pass the following parameters to the function: length, width, material type, material cost, labor cost.
Explanation / Answer
#include <iostream>
using namespace std;
double getMaterialCost(double length, double width, int type){
double cost;
if(type == 1){
cost = 10;
}
else if(type == 2){
cost = 30;
}
else if(type == 3){
cost = 25;
}
return (length + width) * cost;
}
double getLaborCost(double length, double width){
if(length + width >= 200){
return (length + width) * 10;
}
else{
return (length + width) * 15;
}
}
void print(double length, double width, int type, double mcost, double lcost){
cout << "The total length of fencing required is " << (length + width) << " feet." << endl;
cout << "The cost for a fence of this length is " << mcost << endl;
cout << "The cost for the labor for this length of fence is " << lcost << endl;
cout << "The total cost for the fence is " << mcost + lcost << endl;
}
int main(){
double length, width;
double mcost, lcost;
int type;
cout << "Length of the field: ";
cin >> length;
cout << "Width of the field: ";
cin >> width;
cout << "enter 1 for wood, 2 for iron, and 3 for pvc fencing. ?: ";
cin >> type;
if(type < 1 || type > 3){
type = 1;
}
mcost = getMaterialCost(length, width, type);
lcost = getLaborCost(length, width);
print(length, width, type, mcost, lcost);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.