如何添加此部分:保持有效通货膨胀率的总计和计算平均值的计算费率 [英] How do I add this part: keep a running total of the valid inflation rates and the number of computed rates to calculate average

查看:119
本文介绍了如何添加此部分:保持有效通货膨胀率的总计和计算平均值的计算费率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   iostream  >  
使用 命名空间标准;

double inflationRate( float old_cpi, float new_cpi);

int main()
{
float old_cpi,new_cpi;
再次 char ;

do
{
cout<< 输入旧的和新的消费者价格指数:;
cin>> old_cpi>> new_cpi;

cout<< 通货膨胀率为<< inflationRate(new_cpi,old_cpi)<< ENDL;

cout<< 再试一次?(y或Y):;
cin>>再次;

} while ((再次== ' Y')||(再次== ' y')) ;

return 0 ;
}

double inflationRate( float old_cpi, float new_cpi)
{
return ((old_cpi - new_cpi)/ new_cpi * 100 );
}





我的尝试:



如何添加此部分:保持有效通货膨胀率和计算费率的计算总数以计算平均值

解决方案

您必须将通货膨胀率存储在范围更大的变量中

  int  main()
{
float old_cpi,new_cpi;
再次 char ;

float totalInflation = 0 ; // 主要范围


{
cout<< 输入旧的和新的消费者价格指数:;
cin>> old_cpi>> new_cpi;

float inflationRate = inflationRate(new_cpi,old_cpi); // 局部变量
cout<< 通货膨胀率为<< inflationRate<< ENDL;
totalInflation + = inflationRate; // 总计
cout<< ; 再试一次?(y或Y):;
cin>>再次;

} while ((再次== ' Y')||(再次== ' y')) ;

cout<< 总通胀率为<< totalInflation<< ENDL;


#include <iostream>
using namespace std;

double inflationRate (float old_cpi, float new_cpi);

int main()
    {
        float old_cpi, new_cpi;
        char again;
        
        do
        {
            cout << "Enter the old and new consumer price indices: ";
            cin  >> old_cpi >> new_cpi;
        
            cout << "Inflation rate is " << inflationRate(new_cpi, old_cpi) << endl;
            
            cout << "Try again? (y or Y): ";
            cin  >> again;
            
        } while((again == 'Y') || (again == 'y'));
        
    return 0;
}

double inflationRate(float old_cpi, float new_cpi)
{
    return ((old_cpi - new_cpi) / new_cpi * 100);
}



What I have tried:

How do I add this part: Keep a running total of the valid inflation rates and the number of computed rates to calculate average

解决方案

you must store the inflationrate in a variable with a bigger scope

int main()
    {
        float old_cpi, new_cpi;
        char again;

        float totalInflation = 0;//scope of main
        
        do
        {
            cout << "Enter the old and new consumer price indices: ";
            cin  >> old_cpi >> new_cpi;

            float inflationRate = inflationRate(new_cpi, old_cpi);//local variable      
            cout << "Inflation rate is " << inflationRate << endl;
            totalInflation += inflationRate;//sum up total
           cout << "Try again? (y or Y): ";
           cin  >> again;
            
        } while((again == 'Y') || (again == 'y'));

        cout << "Total inflation rate is " << totalInflation << endl;


这篇关于如何添加此部分:保持有效通货膨胀率的总计和计算平均值的计算费率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆