The population of a town A is less than the population of town B. However, the p
ID: 671675 • Letter: T
Question
The population of a town A is less than the population of town B. However, the population of town A is growing faster than the population of town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of town A will be greater than or equal to the population of town B and the populations of both the towns at that time. (A sample input is: Population of town A = 5000, growth rate of town A = 4%, population of town B = 8000, and growth rate of town B = 2%.)
Perform an analysis, design the algorithm, implement, compile, and test your code. Turn in the analysis, design, and source code.
Explanation / Answer
#include <iostream>
#include <Windows.h> //my compiler does not halt when running the program unless forced to. in the spirit of not adding unnecessary values, I use Sleep instead.
using std::cout;
using std::cin;
using std::endl;
int main(){
int years = 0;
double populationA, populationB, newMembersA, newMembersB, growthRateA, growthRateB;
cout << "--------------------------------------------------------------------------------n";
cout << "The population of a town A is less than the population of town B.n";
cout << "However, the poulation of town A is growing fastern";
cout << "than the population of town B.n";
cout << "This program outputs after how many years the poulationn";
cout << "of town A will be greater than or equal to the population of town Bn";
cout << "and the populations of both the towns at that time.n";
cout << "--------------------------------------------------------------------------------n";
cout << endl;
cout << "Please enter the population of town A and it's growth rate: ";
cin >> populationA >> growthRateA;
cout << endl << "Thank you." << endl << endl << "Now please enter the population of town B and it's growth rate: ";
cin >> populationB >> growthRateB;
double convertRateA = growthRateA/100.00, convertRateB = growthRateB/100.00; //this needs to go after the intialization (in this case, user input)
while (populationA < populationB){
if (growthRateA > 0 && growthRateA < 1){
newMembersA = populationA * growthRateA; //you had been using the logical equality operator (==) instead of the assignment (=) operator. Be wary.
populationA += newMembersA;
}
else if (growthRateA >= 1){
newMembersA = populationA * convertRateA; //again, == instead of =.
populationA += newMembersA;
}
else{
cout << "Invalid growth rate for town A.";
break;
}
if (0 < growthRateB && growthRateB < 1){
newMembersB = populationB * growthRateB; //once more.
populationB += newMembersB;
}
else if (1 <= growthRateB){
newMembersB = populationB * convertRateB; //once more.
populationB += newMembersB;
}
else{
cout << "Invalid growth rate for town B.";
break;
}
years++; //changed.
}
if (populationA >= populationB){
cout << populationA << populationB << years << endl;
}
Sleep(10000);//Sleeps (suspends) the program for 10000 milliseconds.
return 0;
}
//
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.