查找输入值的平均值,最大值和最小值 [英] Find average, maximum and minimum values of values entered

查看:200
本文介绍了查找输入值的平均值,最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的第一部分是编写一个程序,该程序:

The first part of the question is to write a program that:

提示用户输入整数值并保持累计.

prompts user to enter integer values and keep a running total.

#include<iostream>
using namespace std;

int main()
{
    int n, sum = 0;
    while(n != 0)
    {
        cout << "Enter number :";
        cin >> n;

        if(n <= 0)
            break;

        sum += n;
    }

第二部分是:

修改以前的程序,以打印出最大和最小的读取数以及平均值.还要更改提示以显示仍要输入的号码数量.

Modify the previous program to print out the largest and smallest number read in as well as the average. Also change the prompt to show the number of numbers still to be entered.

#include<iostream>
using namespace std;

int main()
{
    float n, sum=0.0, minimum=1.0, maximum=0.0, average;
    int i = 0, x;

    while(n != 0)
    {
        cout << "Enter number" << (i+1) << " :";
        cin >> n;

        if(n <= 0)
            break;

        sum += n;
        i++;

        if(n > maximum)
        {
            maximum = n;
        }

        if(n <= minimum)
        {
            minimum = n;
        }

        x = x + (i + 1);
    }

cout << "Total=" << sum << endl;

average = sum / x;

cout << "Average=" << average << endl;
cout << "Maximum=" << maximum << endl;
cout << "Minimum=" << minimum << endl;
return 0;
}

我的问题是我无法计算平均值,也无法显示仍需输入的数字.有人可以帮忙吗?

My problem is that I'm not able to compute the average and show the number of numbers still to be entered. Can anyone help please?

推荐答案

问题是您将sum除以x.为什么需要这个x? i是您输入的对象的数量,只需将总和除以i即可;
还有一个错误,您定义了变量n且未设置值,并且想将其与0进行比较.它可能无法正常工作.

Problem is that you divide sum to x. Why you need this x? i is your count of inputed object, just divide sum to i;
One more bug, you define variable n and dont set value, and you want to compare it with 0. It can work incorrect.

    #include<iostream>
    using namespace std;
    int main()
    {
        float n = 1,sum=0.0,minimum=1.0,maximum=0.0,average;
        int i=0;

        while(n!=0)
        {
            cout<<"Enter number"<<(i+1)<<" :";
            cin>>n;
            if(n<=0)
                break;
            sum+=n;
            i++;
            if(n>maximum)
            {
                maximum=n;
            }

            if(n<=minimum)
            {
                minimum=n;
            }
        }

        cout<<"Total="<<sum<<endl;

        average=sum/i;

        cout<<"Average="<<average<<endl;
        cout<<"Maximum="<<maximum<<endl;
        cout<<"Minimum="<<minimum<<endl;
        return 0;
    }

这篇关于查找输入值的平均值,最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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