gpt4 book ai didi

C++ 最大随机数生成器

转载 作者:太空宇宙 更新时间:2023-11-04 15:43:58 33 4
gpt4 key购买 nike

我正在尝试为 3 个团队生成 3 个随机分数并确定哪个分数最高。我的做法是使用Predfined函数,一个程序员定义的函数,声明并定义该函数。我对此很陌生,我买的这本书并没有真正帮助我。

以下是代码端的目标大纲:

  • 调用预定义函数生成随机数序列

  • 声明并定义一个返回值的函数

  • 调用程序员定义的函数。

最终目标(摘自书中):

  • 编写一个名为 max 的函数,它接受三个 int 类型的参数并返回参数的最大值。您的程序必须同时具有此函数的声明和定义。函数声明必须放在 main 函数之上。

  • 编写执行以下操作的函数 main():

    一个。生成一个介于 10 和 40 之间的随机整数作为三个团队中每个团队的分数Hoosier、Boilermakers 和 Fighting Irish,并打印出这些乐谱。你的程序必须能够在不同时间运行时生成不同的分数序列。

    调用任务 1 中定义的函数 max 来查找所有团队中的最大分数并打印出找到的最大分数。

    将最大的分数与 Hoosier 的分数进行比较,并打印出“Go Hoosier!!!”如果 Hoosier 队的得分等于所有队中得分最高的。

这是代码

/*

Author: Dan Wingeart
Assignment: Lab 9

*/

#include <iostream>

#include <cmath>

#include <cstdlib>

using namespace std;

int max(int Hscore, int Pscore, int Fscore);

int main()
{

int Fscore, Pscore, Hscore, highestScore;

Fscore = 10 + rand() % 40;
Pscore = 10 + rand() % 40;
Hscore = 10 + rand() % 40;

cout << "Prediction performance of sport teams:" << endl;
cout << "Team Hoosier's score is " << Hscore << endl;
cout << "Team Boilermakers' score is " << Pscore << endl;
cout << "Team Fighting Irish's score is " << Fscore << endl;

highestScore = max(Hscore, Pscore, Fscore)

if (max>Pscore&&max>Fscore){
cout << "The largest score is " << max << endl;
cout << "GO HOOSIER!!!" << endl;}
else
cout << "The largest score is " << max << endl;


return 0;
}

int max(int Hscore, int Pscore, int Fscore)
{

if (Hscore>Pscore&&Hscore>Fscore){
cout << Hscore;}

else if (Pscore>Hscore&&Pscore>Fscore){
cout << Pscore;}

else{
cout << Fscore;}

return 0;

}

产生的错误:

ClCompile:
1> Lab9.cpp
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2143: syntax error : missing ';' before 'if'
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(35): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(38): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1

最佳答案

    //don't forget to like if the solution is working
//ask ban

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int max(int Hscore, int Pscore, int Fscore);

int main()
{
srand ( time(NULL) );//(1)
int Fscore, Pscore, Hscore, highestScore;

Fscore = 10 + rand() % 40;
Pscore = 10 + rand() % 40;
Hscore = 10 + rand() % 40;

cout << "Prediction performance of sport teams:" << endl;
cout << "Team Hoosrand ( time(NULL) );sier's score is " << Hscore << endl;
cout << "Team Boilermakers' score is " << Pscore << endl;
cout << "Team Fighting Irish's score is " << Fscore << endl;

highestScore = max(Hscore, Pscore, Fscore);

cout<<highestScore;


return 0;
}

int max(int a, int b, int c)
{
if(a>b && a>c)//we assume that a is the maximum. This means that a > b and a > c
{
return a;
}
else // if a is not maximum then we assume that b is maximum
{
if(b>c)
{
return b;
}
}
return c;// if a and b are not maximum then c is maximum
}

//(1)这个函数会帮助你在每次运行程序时得到不同的随机数,更多信息在这里:http://www.cplusplus.com/reference/cstdlib/srand/

关于C++ 最大随机数生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19691918/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com