Author Topic: Population Growth  (Read 4786 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Population Growth
« on: December 05, 2005, 02:16:03 PM »
Code: [Select]

/*
    Title:  Population Growth
    Author: LF Cervantes
    Date:   Dec 3, 2005
*/
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    cout << "-- POPULATION GROWTH --\n";
    double yr1, yr2, yr3, pop1, pop2, pop3;
    double k, elapsed;
    char answer;
    cout << "A program to calculate projected population\n\n";
    do
    {
        cout << "First Census Year and Population (space delimited)     :  ";
        cin >> yr1 >> pop1;
        cout << "Second Census Year and Population (space delimited)    :  ";
        cin >> yr2 >> pop2;
        cout << "Predict population for what year?                      :  ";
        cin >> yr3;
       
        elapsed = yr2-yr1;        
        k = 1/elapsed * log( pop2 / pop1 );
         
        pop3 = pop1 * exp( k * (yr3 - yr1) );
        cout << "\n\nIn year " << yr3 << ", the population will be "
            << pop3 << endl;
               
        cout << "Repeat? y/n  :";
        cin >> answer;
    } while (answer == 'y' || answer == 'Y');
       
    cout << endl;    
    system("PAUSE");
    return 0;
}


Highlights:
The input of multiple values using cin
Method allowing user to choose to repeat
More math expressions conversion to C++ statements
Analyze. Design. Develop. Debug. Deploy. Maintain.