需要帮助查找一组数字的最大数量和最小数量

时间:2017-09-12 07:50:09

标签: c++

正如标题所说,我需要帮助找到一组数字中最大的数字和最小的数字,然后在最后显示它。每次都会随机生成这组数字。如果有人可以解释如何制作它以便它显示所选择的随机数而不是总数同时仍然计算总数(平均值),也会非常感激。谢谢:))

抱歉,如果缩进很奇怪,这是我在这里的第一篇文章。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int Total = 0; // total of all numbers
    int AOT; //amount of times
    int i;

    cout << "How many random numbers should this machine make?" << endl;
    cin >> AOT;
    cout << endl;

    srand(time(0));

    for(i=1;i<=AOT;i++)
    {
        //makes a random number and sets it to the total
        Total = Total + (rand()%10);
        //just some fancy text
        cout << "The total after " << i << " random number/s is ";
        cout << Total << endl;
    }

    cout << endl;
    cout << endl;

    // ALL THE DATA ON THOSE NUMBERS
    cout << "The amount of numbers there were is " << AOT << endl;
    cout << "The average for the random numbers is " << Total / AOT << endl;
}

1 个答案:

答案 0 :(得分:0)

在生成随机数时分配它们,并打印临时数字以向用户显示该组号码。 初始化为变量,以存储该组的最小和最大数量到该临时数。

比较生成它们的数字随机数,以查找它是小于“最小”还是大于“最大”,并相应地将它们分配给变量。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    int smallest, largest, temp;
    int AOT; //amount of times
    int i;

    cout << "How many random numbers should this machine make?" << endl;
    cin >> AOT;
    cout << endl;

    srand(time(0));
    cout<<" Random Numbers are:";
    smallest = rand()%10;
    cout<<smallest<<'\t';
    largest = smallest;
    for(i=1;i<AOT;i++)
    {
        temp = (rand()%10);
        cout<<temp<<'\t';
        if(temp < smallest)
        {
            smallest =  temp;
        }
        else if(temp > largest)
        {
            largest = temp;
        }

    }

    cout << endl;
    cout << endl;

    // ALL THE DATA ON THOSE NUMBERS
    cout << "The smalles number is " << smallest << endl;
    cout << "The largest number is " << largest << endl;
}