求平均值及最大值

1.问题描述

输入若干个数,设输入的第一个数为后面要输入的数的个数,求平均值及最大值

2.问题分析

设计个数组进行储存,输入个数,后计算总和,然后除以个数

3.算法设计

把数字存放在数组中,定义for循环,找出最大值,然后for循环求出和,除以个数的总数

4.代码

#include<iostream>
#include<cmath>
using namespace std; 
int main()
{
    cout << "please enter a numbers:";
    double a;
    cin >> a;
    int arr[] = {0};
    cout << "please enter " << a << " number" << endl;
    for (int i = 0; i < a; i++)
    {
        cin >> arr[i];
    }
    int max = arr[0];
    for (int j = 0; j < a; j++)
    {
        if (arr[j] > max)
        {
            max = arr[j];
        }
    }
    int  sum = 0;
    for (int t = 0; t < a; t++)
    {
        sum = sum + arr[t];
    }
    double ave = sum / a;
    cout << "The maximum value of these twenty numbers is:" << max << endl;
    cout << "The average value of these twenty numbers is:" << ave << endl;
 
    return 0;
 
}

 

posted @ 2023-05-18 23:43  酥饼馅红豆沙  阅读(91)  评论(0编辑  收藏  举报