如何使用数组制作随机分数生成器

how to make random score generator using array?

本文关键字:随机 何使用 数组      更新时间:2023-10-16

我正在尝试制作一个程序来计算班级的平均分数并计算 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::beginstd::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。这应该足够了。

顺便说一句

,在您的代码中,我只看到分数数组的分配。记得删除!更好的是,始终使用矢量。