如何使用数组制作随机分数生成器
how to make random score generator using array?
我正在尝试制作一个程序来计算班级的平均分数并计算 A、B、C、D 学生的数量。现在我正在尝试另一个想法:如果考试成绩是随机给学生的(在 60 到 100 之间(,我如何将这些随机分数输入到数组中?我的要求用户一一输入所有分数。我想知道如何自动填写数组。
#include <iostream>
#include <cstdlib>
using namespace std;
double Average(double *scores, int N)
{
int i;
double total = 0;
for(i = 0; i < N; i++)
{
total = total+scores[i];
}
return total / N;
}
int Agrade(double *scores, int N)
{
int i;
int count = 0;
for(i = 0; i < N; i++)
{
if(scores[i] >= 90 && scores[i] <= 100) count++;
}
return count;
}
int Bgrade(double *scores, int N)
{
int i;
int count = 0;
for(i=0; i < N; i++)
{
if(scores[i] >=80 && scores[i] < 90) count ++;
}
return count;
}
int Cgrade(double *scores, int N)
{
int i;
int count = 0;
for(i=0; i < N; i++)
{
if(scores[i] >=70 && scores[i] < 80) count ++;
}
return count;
}
int Dgrade(double *scores, int N)
{
int i;
int count = 0;
for(i=0; i < N; i++)
{
if(scores[i] >=60 && scores[i] < 70) count ++;
}
return count;
}
int Fgrade(double *scores, int N)
{
int i;
int count = 0;
for(i=0; i < N; i++)
{
if(scores[i] < 60) count ++;
}
return count;
}
int main(){
int i;
int N;
double *scores;
std::cout<<"How many test scores? "<<endl;
cin>>N;
if(N<1){
std::cout<<"Invalid input. try again"<<endl;
}
else if(N>25)
{
std::cout<<"1-25 only."<<endl;
}
else if(N>0 && N<25){
std::cout<<"Total number of test is: "<< N << endl;
}
scores = new double[N];
for(i = 0; i < N; i++)
{
cout << "Randomly generating score of students" << i + 1 << ": ";
cout << (rand() % 40 + 60) << endl; //Trying to give random scores between 60-100
if(!(cin >> scores[i]) || scores[i] < 0 || scores[i] > 100)
{
if(!cin)
{
cin.clear();
cin.ignore(1000, 'n');
}
cout << "Score must be between 0 to 100.nn"; i--; continue;
}
}
double averagescore = Average(scores, N);
int scoreAcount = Agrade(scores, N);
int scoreBcount = Bgrade(scores, N);
int scoreCcount = Cgrade(scores, N);
int scoreDcount = Dgrade(scores, N);
int scoreFcount = Fgrade(scores, N);
cout << "The average test score : " << averagescore << endl;
cout << "The number of A grades : " << scoreAcount << endl;
cout << "The number of B grades : " << scoreBcount << endl;
cout << "The number of C grades : " << scoreCcount << endl;
cout << "The number of D grades : " << scoreDcount << endl;
cout << "The number of F grades : " << scoreFcount <<endl;
cout << endl;
return 0;
}
我可以修改此部分以使其工作吗?
for(i = 0; i < N; i++)
{
cout << "Randomly generating score of students" << i + 1 << ": ";
cout << (rand() % 40 + 60) << endl; //Trying to give random scores between 60-100
if(!(cin >> scores[i]) || scores[i] < 0 || scores[i] > 100)
{
if(!cin)
{
cin.clear();
cin.ignore(1000, 'n');
}
cout << "Score must be between 0 to 100.nn"; i--; continue;
}
}
还是我需要修改所有功能?
如果你想要的是将std::rand()
函数的结果分配给数组中的所有元素,你应该从<algorithm>
标题中查看std::generate
函数,它完全符合你的要求。看看这个简单的例子:
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
int main() {
std::srand(0);
const int N = 5;
std::vector<int> scores(N);
std::generate(scores.begin(), scores.end(), [](){return std::rand() % 40 + 60; });
for(auto s : scores) {
std::cout << s << ' ';
}
}
这将使用 std::generate 函数和 lambda 函数作为返回随机数的第三个参数,用指定间隔的随机生成的数字填充向量scores
。
在您的情况下,如果您不被允许使用 std::vector,您仍然可以使用普通的 C 数组来执行此操作,如下所示:
int scores[5];
std::generate(std::begin(scores), std::end(scores), [](){return std::rand() % 40 + 60; });
不幸的是,std::begin
和std::end
函数不能直接应用于动态分配的数组,但这仍然适用于您的情况:
const int N = 5;
double* scores = new double[N];
std::generate(scores, scores+N, [](){return std::rand() % 40 + 60; });
for(int i=0; i<N; ++i) {
std::cout << scores[i] << ' ';
}
// remember to release memory allocated using new
您需要为随机数生成器设定种子,以确保随机数具有合理的随机种子。因此,在您提到的 for 循环之前,您需要调用类似 srand(time(NULL))
的内容并在代码中包含 time.h。这应该足够了。
,在您的代码中,我只看到分数数组的分配。记得删除!更好的是,始终使用矢量。
相关文章:
- 在c++构造函数中使用随机字符串生成器
- 使用std::mt19937从字符串中返回一个随机单词
- 在类中使用随机生成器时出现性能问题
- 如何使用要传递给 mt19937 的可选随机种子参数设计函数
- 如何使用 SML 随机生成八进制元组
- 当我尝试使用它时,Scanf 会抛出一个随机异常(scanf_s 也是如此)
- 如何使用兰德随机化模拟点击
- 使用指针在分区函数中实现随机分区点
- 如何使用可选的随机种子参数创建 roll_die() 函数
- 尝试使用C++中的模板进行BST排序,但在使用随机字符串进行测试时不起作用
- 随机自动点击器C++使用一个键进行开/关
- 如何使用按位运算将随机uint64_t转换为范围 (0, 1) 的随机双精度
- 使用指针编译代码后,.cpp文件将变为随机字符
- 如果我在每个平台上使用相同的种子,随机结果会相同吗?
- 随机读取使用MapViewOffile的文件
- 在哪里初始化随机种子以通过多个随机模块使用
- 指定的随机分布使用哪种类型的随机数引擎
- 随机参数使用System()
- 如何操作在随机地址使用指针
- 标准多元正态随机向量使用Boost::random