2014-01-18 3 views
1

편집 : 전역 변수는 할당의 일부로 제한됩니다 (이 부분은 잊으 셨습니다).반복 결과 업데이트

이 프로그램은 지정된 연도 동안 매년 예상 인구를 계산합니다. N은 새로운 인구 크기가

N = P(1 + B)(1 - D) 

는, P는 B는 출산율이고, D는 사망률이며, 이전의 인구 크기 : 여기 방정식이다. B와 D는 십진수입니다.

내 문제는 첫 번째 반복 결과 만 얻을 수 있으며 결과를 업데이트하여 (1 + B)와 (1 - D)를 곱하는 방법을 알지 못합니다. 내 생각 엔 결합 된 대입 연산자 중 하나와 관련이 있다고 생각하지만 성공하지 못했습니다.

#include <iostream> 
#include <iomanip> 
#include <cmath> 

using namespace std; 

int get_pop(); 
int get_numYears(); 
double get_annBirthRate(); 
double get_annDeathRate(); 
double pop_new(int& A, double& B, double& C, double& D); 

int main() 
{ 
    int P, 
    num, 
    Y; 

    double B, 
    D, 
    N; 

    P = get_pop(); 
    B = get_annBirthRate(); 
    D = get_annDeathRate(); 
    Y = get_numYears(); 
    for (num = 1; num != Y; num++) 
    { 
     N = pop_new(P, B, D, N); 
     cout << "\n\nThe projected population for the end of year " << num << " is " << N << endl; 
     cin.ignore(); 
    } 

    return 0; 
} 

int get_pop() 
{ 
    int pop; 

    cout << "Enter the starting population:" << endl; 
    cin >> pop; 
    while (pop < 2) 
    { 
     cout << "\n\nError - starting population cannot be less than 2:" << endl; 
     cin >> pop; 
    } 
    return pop; 
} 


double get_annBirthRate() 
{ 
    double annBirthRate; 

    cout << "\n\nEnter the annual birth rate \n(a positive percentage in decimal form):" << endl; 
    cin >> annBirthRate; 
    while (annBirthRate < 0) 
    { 
     cout << "\n\nError - annual birth rate cannot be negative:" << endl; 
     cin >> annBirthRate; 
    } 
    return annBirthRate; 
} 


double get_annDeathRate() 
{ 
    double annDeathRate; 

    cout << "\n\nEnter the annual death rate \n(a positive percentage in decimal form):" << endl; 
    cin >> annDeathRate; 
    while (annDeathRate < 0) 
    { 
     cout << "\n\nError - death rate cannot be negative:" << endl; 
     cin >> annDeathRate; 
    } 
    return annDeathRate; 
} 


int get_numYears() 
{ 
    int numYears; 

    cout << "\n\nEnter the number of years to display:" << endl; 
    cin >> numYears; 
    while (numYears < 0) 
    { 
     cout << "\n\nError - number of years cannot be less than 1:" << endl; 
     cin >> numYears; 
    } 
    return numYears; 
} 

double pop_new(int& P, double& B, double& D, double& N) 
{ 
    N = P * (1 + B) * (1 - D); 

    return N; 
} 

답변

0

값 P, B 및 D는 변경되지 않습니다.

double pop_new(int P, double B, double D) 
{ 
    double N = P * (1 + B) * (1 - D); 
    return N; 
} 

과 :

을 확인

P = pop_new(P, B, D); 
cout << "\n\nThe projected population for the end of year " 
    << num << " is " << P << endl; 

참고 : 당신은 참조를 통과 할 필요가 없습니다 (그리고, 당신은 이중 & const를 통과해야한다)