需要帮助找到一组数字的数量最多和最小数量
Need help finding the largest number and the smallest number of a group of numbers
正如标题所示,我需要帮助找到一组数字中的最大数字和最小数字,然后在最后显示。每次都会随机生成一组数字。如果有人能够解释如何制作它,也将非常感谢它,以显示所选的随机数,而不是总数,同时仍在计算总数(平均)。谢谢:)
P.S抱歉,如果凹痕很奇怪,这是我在这里的第一篇文章。
#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;
}
在生成随机数时分配它们并打印临时号码以向用户显示数字组。初始化到变量,存储该组的最小数量和最大数量,到该临时数字。
将其生成数字随机数进行比较,以查找其小于"最小"或大于"最大"的数字,并相应地将它们分配给变量。
#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;
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 获取数字小数部分的最佳方法
- C 如何在小数系统中添加两个数字
- 如何设置数字格式以显示C 中的所有小数位数
- 如何解析小数分离器为结肠的数字的字符串表示:
- 获取存储为字符串的数字的小数部分
- 将十进制数字截断为最接近的4个小数和断言
- C++不显示小数位数的数字
- C++ 将数字的小数部分转换为整数
- 显示带小数位数的数字,而不是整数
- 你如何在数字上添加 2 位小数
- 在小数秒内仅用 3 位数字格式化 posix 时间
- 如何从 9 和 90 中提取 2.9 和 2.09,即获取数字等于最大编号的小数部分.二分位数之后的数字
- 两个数字相减时的小数
- 如何在C++中存储数字而不截断小数
- 使用'int'数据类型时出错,即使数字不是小数
- 用两个小数求3个数字的平均值(不舍入!)
- 为什么小数后面的数字都是零
- 由正整数或正小数组成的字符串,转换为I数字格式
- 如何使用std::cout停止输出像5.0这样的数字的小数和零?