In: Computer Science
I have a quick question about this C ++ problem in my intro class. I have been able to get the first answer consistently when I test it, but my second seems to either be high or low no matter how I change it. Can you show me how you would do this problem with the setprecision(2)
Paula and Danny want to plant evergreen trees along the back side of their yard. They do not want to have an excessive number of trees.
Write a program that prompts the user to input the following:
The program outputs:
Format your output with setprecision(2) to ensure the proper number of decimals for testing!
This is all of the information given by the question. So far this is what I have come up with, but I believe I am off with one of the calculations.
This is the information given from when I tested it:
Test Case Incomplete
Number and space for trees correctly output test 1
Input
200 8 24
Output
Enter the length of the yard: 200 Enter the radius of a fully-grown tree: 8 Enter the space between the fully grown tree: 24 The numbers of trees that can be planted in the yard is 5The total space that will be occupied by the fully grown trees 1e+03
Results
5
1005.31 (this is the number I am having a difficult time reaching).
Here is an example of my attempt and the test result right
after.
#include
#include
using namespace std;
int main()
{
// declare variables as double type
double lengthofYard;
double radius;
double spaceBetween;
double areaofTrees;
double numberofTrees;
const float pi = 3.14159;
// Prompt the user to enter the length of the yard.
cout << setprecision(2) ;
cout << "Enter the length of the yard: ";
cin >> lengthofYard;
// Prompt the user to enter the radius of the fully grown tree.
cout << "Enter the radius of a fully-grown tree: ";
cin >> radius;
//Prompt the space between the fully-grown trees from user
cout << "Enter the space between the fully grown tree: ";
cin >> spaceBetween;
//calculate number of trees
numberofTrees = lengthofYard/((2*radius)+spaceBetween);
//calculate areaofTrees
areaofTrees = (pi*radius*radius)*numberofTrees;
cout << "The numbers of trees that can be planted in the yard
is " ;
cout << numberofTrees;
cout << "The total space that will be occupied by the fully
grown trees ";
cout << areaofTrees;
return 0;
}
Test CaseIncomplete
Number and space for trees correctly output test 1
Input
200 8 24
Output
Enter the length of the yard: 200 Enter the radius of a fully-grown tree: 8 Enter the space between the fully grown tree: 24 The numbers of trees that can be planted in the yard is 5The total space that will be occupied by the fully grown trees 1e+03
Results
5
1005.31
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
// declare variables as double type
double lengthofYard;
double radius;
double spaceBetween;
double areaofTrees;
double numberofTrees;
const float pi = 3.14159;
// Prompt the user to enter the length of the yard.
cout << setprecision(6) ;
cout << "Enter the length of the yard: ";
cin >> lengthofYard;
// Prompt the user to enter the radius of the fully grown tree.
cout << "Enter the radius of a fully-grown tree: ";
cin >> radius;
//Prompt the space between the fully-grown trees from user
cout << "Enter the space between the fully grown tree: ";
cin >> spaceBetween;
//calculate number of trees
numberofTrees = lengthofYard/((2*radius)+spaceBetween);
//calculate areaofTrees
areaofTrees = (pi*radius*radius)*numberofTrees;
cout << "The numbers of trees that can be planted in the yard
is " ;
cout << numberofTrees<<endl;
cout << "The total space that will be occupied by the fully
grown trees ";
cout << areaofTrees;
return 0;
}
Expected output: