In: Computer Science
Using C++
If you have 2 plants (A and B) with the hight of plant A is less than the hight of plant B. However, the hight of plant A is growing faster than the hight of plant B (the hight is measured in centimetres and the hight growth rate is matured daily). Write a program that prompts the user to enter the hight and the hight growth rate of each plant. The program should check that the hight of plant A is less than the hight of plant B and that the growth rate of plant A is higher than the growth rate of plant B and it should not work otherwise. The program outputs after how many days the hight of plant A will be greater than or equal to the hight of plant B, and the hight of both plants at that time.
A sample input and output is as follows:
First Run:
Enter the hight of plan A (in centimetres): 26
Enter the hight growth rate of plant A: 0.04 Enter the hight of plant B: 25
Enter the hight growth rate of plant B: 0.02
Plant A hight should be less than plant B hight and plant A growth rate should be higher than plant B growth rate.
Second Run:
Enter the hight of plan A (in centimetres): 22
Enter the hight growth rate of plant A: 0.04
Enter the hight of plant B: 25
Enter the hight growth rate of plant B: 0.02
The hight of plant A will be greater than or equal to the hight of plant B after 7 days.
Plant A hight will become 28.9505
Plant B hight will become 28.7171
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
double plantAHeight, plantBHeight;
double plantAGrowth, plantBGrowth;
cout<<"Enter the hight of plan A (in
centimetres): ";
cin >> plantAHeight;
cout<<"Enter the hight growth rate of plant A:
";
cin >> plantAGrowth;
cout<<"Enter the hight of plan B (in
centimetres): ";
cin >> plantBHeight;
cout<<"Enter the hight growth
rate of plant B: ";
cin >> plantBGrowth;
if(plantAGrowth<plantBGrowth ||
plantAHeight>plantBHeight){
cout<<"Plant A hight should
be less than plant B hight and "
<<"plant A
growth rate should be higher than plant B growth rate.";
return 1;
}
int day = 0;
while(plantAHeight<plantBHeight){
day+=1;
plantAHeight+=
plantAHeight*plantAGrowth;
plantBHeight+=
plantBHeight*plantBGrowth;
}
cout<<"The hight of plant A will be greater than
or equal to the hight of plant B after "
<<day<<"
days.\n";
cout<<"Plant A hight will become
"<<plantAHeight<<endl;
cout<<"Plant B hight will become
"<<plantBHeight<<endl;
return 0;
}
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!