Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Problem 2 This program is designed to analyze the growth of two citi

ID: 1845876 • Letter: P

Question

Programming Problem 2

This program is designed to analyze the growth of two cities. Each city has a starting population and annual growth rate. The smaller city has the larger growth rate (required). Show the comparative populations of each city year by year until the smaller city has grown larger than the bigger city.

As an example, Dogville has a population of 5000 growing at 20% annually while Cattown has a population of 7000 growing at 10% annually. The projected populations are:

            Year    Dogville          Cattown

            1          6000                7700

            2          7200                8470

            3          8640                9317

            4          10368              10249

4. Identify three significant test cases including one for incorrect input (ie Small town has lower growth rate). For each of the three test cases show what inputs you will use and calculate what your expected outputs should be.

Explanation / Answer

Program 2:
#include <iostream> //for input/output stream
#include <iomanip> //for manip setw
#include <string> //for string and function getline
using namespace std;
int main()
{
int popSmall, popLarge, year = 1; //population variables
double groRate1, groRate2; //growth rate of small and large city
string smallCity, largeCity;
cout << "What is the name of the smaller city?" << endl;
getline (cin, smallCity); //get the whole name of the small city
cout << "What is the name of the larger city?" << endl;
getline (cin, largeCity); //get the whole name of the large city
do
{
cout << "What is the population of ";
cout << smallCity << endl;
cin >> popSmall; //get population of smmall city
cout << "What is the population of ";
cout << largeCity << endl;
cin >> popLarge; //get population of large city
if (popSmall >= popLarge)
{
cout << "The smaller city should have the smallest population";
cout << endl;
} //if small pop is greater than or equal to large pop output "The smaller city....."
}
while (popSmall >= popLarge); //while the population of small city is more than large city redo loop
do
{
cout << "What is the annual growth rate of ";
cout << smallCity << endl;
cin >> groRate1; //get annual growth rate of small city
cout << "What is the annual growth rate of ";
cout << largeCity << endl;
cin >> groRate2; //get annual growth rate of large city
if (groRate1 <= groRate2)
{
cout << "Annual growth rate of smaller city should be larger ";
cout <<"than annual growth rate of larger city.";
cout << endl;
} //if growth rate of small city is less than growth rate of large city output "Annual growth rate......."
}
while (groRate1 <= groRate2); //while growth rate of small city is less than growth rate of large city redo loop
cout << "The projected populations are:"; //beginning of output
cout << endl << endl; //skip two lines for easier to read format
cout << "Year";
cout << setw (10); //output year and skip 10 spaces
cout << smallCity;
cout << setw (15); //output smallCity value and skip 15 spaces
cout << largeCity;
cout << endl; //output largeCity value and endline
while (popSmall <= popLarge) //while statement: loop below repeats until the pop of small city is great than pop of large city
{
popSmall = popSmall*groRate1+popSmall;
popLarge = popLarge*groRate2+popLarge; //formulas to find yearly pop growth of small and large city
cout << year;
cout << setw (10); //year number output and skip 10 spaces
cout << popSmall;
cout << setw (15); //output popSmall value and skip 15 spaces
cout << popLarge;
cout << endl; //output popLarge value and endline
year++; //add 1 to variable year in order show progress from year 1, year 2, year 3, etc.
}
system ("pause"); //used to keep cmd open for execution test.
return 0;
}