60.4k views
5 votes
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:a. The length of the yard.b. The radius of a fully grown tree.c. The required space between fully grown trees.The program outputs the number of trees that can be planted in the yard and the total space that will be occupied by the fully grown trees.

User Harre
by
7.4k points

1 Answer

4 votes

Below is a C++ program that prompts the user for input and calculates the above information:

#include <iostream>

#include <iomanip>

int main() {

// Declare variables

double yardLength, treeRadius, spaceBetweenTrees;

// Prompt user for input

std::cout << "Enter the length of the yard: ";

std::cin >> yardLength;

std::cout << "Enter the radius of a fully grown tree: ";

std::cin >> treeRadius;

std::cout << "Enter the required space between fully grown trees: ";

std::cin >> spaceBetweenTrees;

// Calculate the number of trees that can be planted

double spaceOccupiedByOneTree = 2 * treeRadius + spaceBetweenTrees;

int numberOfTrees = static_cast<int>(yardLength / spaceOccupiedByOneTree);

// Calculate the total space occupied by fully grown trees

double totalSpaceOccupied = numberOfTrees * spaceOccupiedByOneTree;

// Output the results with setprecision(2)

std::cout << std::fixed << std::setprecision(2);

std::cout << "Number of trees that can be planted: " << numberOfTrees << std::endl;

std::cout << "Total space occupied by fully grown trees: " << totalSpaceOccupied << std::endl;

return 0;

}

So, the above program is one that figures out how many trees can be planted in the yard and how much space they will take up when they are fully grown. It uses the information the user gives.

Hence, one need to make use of setprecision(2) to make sure the number is shown with two decimal places.

See text below

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 length of the yard.

The radius of a fully grown tree. (Use 3.14159 as the constant value for any calculations that may need \pi

π).

The required space between fully grown trees.

The program outputs:

The number of trees that can be planted in the yard

The total space that will be occupied by the fully grown trees.

Format your output with setprecision(2) to ensure the proper number of decimals for testing!

User Reshmi Majumder
by
6.9k points