Back to Code List
c++

Population Growth

A simple program to calculate population growths.

   Source.cpp

#include <iostream>
#include <iomanip>
using namespace std;

char inputOption();
void getPopulationParametersNoOutIn(_int64 &P, int &B, int &D, int &Y );
void getPopulationParametersWithOutIn(_int64 &P, int &B, int &D, int &I, int &O, int &Y );
void calculateAndPrintPopulationChange(_int64 P, int B, int D, int Y );
void calculateAndPrintPopulationChange(_int64 P, int B, int D, int I, int O, int Y );

/*
P = population starting size  (_int64 integer)
B = annual birth rate (0 < int <100)
D= annual death rate (0 < int <100)
I = In-migration rate (0 < int < 100)
O = Out-migration rate (0 < int < 100)
Y = number of years for which the populations are to be calculated. (0 < int < 100)
NP = new population (_int64 integer)
*/

int main() {
	char userInput = inputOption();
	while (userInput != 'E') {
		_int64 P=0;
		int B=0,D=0,I=0,O=0,Y=0;
		switch (userInput) {
			case 'P':
				getPopulationParametersNoOutIn(P, B, D, Y);
				calculateAndPrintPopulationChange(P, B, D, Y);
				break;
			case 'C':
				getPopulationParametersWithOutIn(P, B, D, I, O, Y);
				calculateAndPrintPopulationChange(P, B, D, I, O, Y);
				break;
		}
		userInput = inputOption();
	}
	cout << "Thank you for using this program." << endl << endl;

	return 0;
}

char inputOption() {
	char userInput;
	bool invalid = true;

	cout << "Welcome to the population growth calculator. Please select an option:" << endl << endl;
	cout << "\"P\" for population growth without considering in or out-migration" << endl;
	cout << "\"C\" for population growth with in and out-migration considered" << endl;
	cout << "\"E\" to stop execution" << endl;
	
	while (invalid) {
		cout << endl << "Input your choice: ";
		cin >> userInput;
		switch (userInput) {
			case 'P':
			case 'C':
			case 'E':
				invalid = false;
				break;
			default:
				cout << "Input must be uppercase P, C, or E." << endl;
		}
	}
	return userInput;
}

void getPopulationParametersNoOutIn (_int64 &P, int &B, int &D, int &Y ) {
	cout << "Please input values for the following:" << endl;
	cout << "Population Starting Size (P>0): ";
		cin >> P;
		while (P < 1) {
			cout << "Population Starting Size must be an integer greater than 0. Please input valid number: ";
			cin >> P;
		}
	cout << "Annual Birth Rate (0<B<100): ";
		cin >> B;
		while (B < 1 || B > 99) {
			cout << "Annual Birth Rate must be an integer between 0 and 100. Please input valid number: ";
			cin >> B;
		}
	cout << "Annual Death Rate (0<D<100): ";
		cin >> D;
		while (D < 1 || D > 99) {
			cout << "Annual Death Rate must be an integer between 0 and 100. Please input valid number: ";
			cin >> D;
		}
	cout << "Number of Years for which the populations are to be calculated (0<Y<100): ";
		cin >> Y;
		while (Y < 1 || Y > 99) {
			cout << "Number of Years must be an integer between 0 and 100. Please input valid number: ";
			cin >> Y;
		}
}

void getPopulationParametersWithOutIn(_int64 &P, int &B, int &D, int &I, int &O, int &Y ) {
	cout << "Please input values for the following:" << endl;
	cout << "Population Starting Size (P>0): ";
		cin >> P;
		while (P < 1) {
			cout << "Population Starting Size must be an integer greater than 0. Please input valid number: ";
			cin >> P;
		}
	cout << "Annual Birth Rate (0<B<100): ";
		cin >> B;
		while (B < 1 || B > 99) {
			cout << "Annual Birth Rate must be an integer between 0 and 100. Please input valid number: ";
			cin >> B;
		}
	cout << "Annual Death Rate (0<D<100): ";
		cin >> D;
		while (D < 1 || D > 99) {
			cout << "Annual Death Rate must be an integer between 0 and 100. Please input valid number: ";
			cin >> D;
		}
	cout << "In-migration Rate (0<I<100): ";
		cin >> I;
		while (I < 1 || I > 99) {
			cout << "In-migration Rate must be an integer between 0 and 100. Please input valid number: ";
			cin >> I;
		}
	cout << "Out-migration Rate (0<D<100): ";
		cin >> O;
		while (O < 1 || O > 99) {
			cout << "Out-migration Rate must be an integer between 0 and 100. Please input valid number: ";
			cin >> O;
		}
	cout << "Number of Years for which the populations are to be calculated (0<Y<100): ";
		cin >> Y;
		while (Y < 1 || Y > 99) {
			cout << "Number of Years must be an integer between 0 and 100. Please input valid number: ";
			cin >> Y;
		}
}

void calculateAndPrintPopulationChange(_int64 P, int B, int D, int Y ) {
	cout << "User Given Values" << endl;
	cout << "=================" << endl;
	cout << setw(21) << "Population Start: " << P << endl;
	cout << setw(21) << "Annual Birth Rate: " << B << endl;
	cout << setw(21) << "Annual Death Rate: " << D << endl;
	cout << setw(21) << "Number of Years: " << Y << endl << endl;

	cout << "Year    New Population" << endl;
	cout << "======================" << endl;

	_int64 NP = P;
	for (int i=0; i<Y; i++) {
		NP = NP*(1 + (double)B / 100) - ((double)D / 100 * NP);
		cout << left << " " << setw(7) << i+1 << NP << endl;
	}
	cout << endl;
}

void calculateAndPrintPopulationChange(_int64 P, int B, int D, int I, int O, int Y ) {
	cout << "User Given Values" << endl;
	cout << "=================" << endl;
	cout << setw(21) << "Population Start: " << P << endl;
	cout << setw(21) << "Annual Birth Rate: " << B << endl;
	cout << setw(21) << "Annual Death Rate: " << D << endl;
	cout << setw(21) << "In-migration Rate: " << I << endl;
	cout << setw(21) << "Out-migration Rate: " << O << endl;
	cout << setw(21) << "Number of Years: " << Y << endl << endl;

	cout << "Year    New Population" << endl;
	cout << "======================" << endl;

	_int64 NP = P;
	for (int i=0; i<Y; i++) {
		NP = NP*(1+(double)B/100) - ((double)D/100*NP) + NP*((double)I/100) - NP*((double)O/100);
		cout << left << " " << setw(7) << i+1 << NP << endl;
	}
	cout << endl;
}