如何改用求和语句来更改我的程序
How can I change my program by using a summing statement instead?
我的 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;
}
}
我不确定您所说的"求和语句"是什么意思,因为看起来您的问题不需要"+"操作。
在任何情况下,您都不必使用 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;
}
}
也许你的教授希望你编写自己的Pow函数,而不是使用数学库中的函数?您可以尝试替换您的:
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));
}
您的 Pow 函数将类似于:
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
相关文章:
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 我的程序没有从文件中读取并输入数据
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 为什么我的程序不能显示斐波那契级数?
- 为什么我的程序在使用预留后没有加速?
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- 为什么我的 c++ 程序检查不是初始化的变量?
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- C++ - 为什么我的程序在再次调用函数后关闭?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 我需要如何更改我的程序以使用打开/关闭原则?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较