2015-04-23 28 views
2

我的C++任務是編寫一個程序,顯示每個國家(墨西哥和美國)從1985年至今的年份的人口估計。將人口舍入到最接近的整數,並將信息顯示在三列中。這樣做之後,我將修改該計劃,以便在墨西哥人口超過美國人口時顯示相同的信息。我做了正確的程序,當我運行它時,它給了我正確的信息,除了我的教授之外,所有人都給了我0分,因爲他希望我不使用POW函數,而是總結性聲明,我不知道我該怎麼做,這聽起來對我來說更加複雜。這是我寫的方式將節目我只是不知道如何使用求和語句來改變它:如何通過使用求和聲明來改變我的程序?

//Program to compute when Mexico's pop exceeds the US pop 
#include<iostream> 
#include<cmath> 
#include<iomanip> 
using namespace std; 
int main() 
{ 
    int Years; 
    double UsPop, MxPop; 
    cout<<setw(7)<<"Year"<<setw(13)<<"US Pop"<<setw(13)<<"Mex Pop"<<endl; 
    cout<<setprecision(0)<<fixed; 
    for(Years=1985;Years<=2014;Years++) 
    { 
     UsPop=243000000.0*(pow(1.007,(Years-1984))); 
     MxPop=78000000*pow(1.033,(Years-1984)); 
     cout<<setw(7)<<Years<<setw(13)<<UsPop<<setw(13)<<MxPop<<endl; 
    } 
} 
+0

什麼是「總結聲明」?它是使用「+」的計算還是使用「+」而沒有其他操作的計算? – Beta

回答

1

我不知道你所說的「總結髮言」的意思,因爲它不看起來像你的問題需要一個「+」操作。

在任何情況下,你不必使用pow,你可以覆蓋美國和墨西哥人口數量在每次迭代,你只需乘以指標。

下面的例子:

int main() 
{ 
    int Years; 
    double UsPop=243000000.0; 
    double MxPop=78000000; 
    cout<<setw(7)<<"Year"<<setw(13)<<"US Pop"<<setw(13)<<"Mex Pop"<<endl; 
    cout<<setprecision(0)<<fixed; 
    double usIndicator = 1.007; 
    double mexIndicator = 1.033; 

    for(Years=1985;Years<=2014;Years++) 
    { 
     UsPop=UsPop*usIndicator; 
     MxPop=MxPop*mexIndicator; 
     cout<<setw(7)<<Years<<setw(13)<<UsPop<<setw(13)<<MxPop<<endl; 
    } 
} 
1

也許你的教授要你寫你自己的戰俘功能,而不是使用一個數學庫?你可以嘗試更換您:

for(Years=1985;Years<=2014;Years++) 
{ 
    UsPop=243000000.0*(pow(1.007,(Years-1984))); 
    MxPop=78000000*pow(1.033,(Years-1984)); 
    cout<<setw(7)<<Years<<setw(13)<<UsPop<<setw(13)<<MxPop<<endl; 
} 

for (Years = 1985; Years <= 2014; Years++) 
{ 
      UsPop = 243000000.0 * (myPow((Years - 1985), 1.007)); 
      MxPop = 78000000 * (myPow((Years - 1985), 1.033)); 
} 

你的戰俘功能將是這樣的:

double myPow(double flag, double number) 
    { 
     double pow = number; 
     //You can edit my formula to match "summing statement" as your professor require . 
     for (int i = 0; i < flag; i++) 
     { 
      pow *= number; //If I'm not mistaken, you should split it up to "Plus" then you will get your "summing statement". 
     } 
     return pow; 
    } 

結果是相同的兩個:

1) 299564530.595318/206587599.027265 
--------------------------- 
2) 299564530.595318/206587599.027265